/** * This helps you transform the current GraphQLInputObjectField into another one by starting a builder with all * the current values and allows you to transform it how you want. * * @param builderConsumer the consumer code that will be given a builder to transform * * @return a new object based on calling build on that builder */ public GraphQLInputObjectField transform(Consumer<Builder> builderConsumer) { Builder builder = newInputObjectField(this); builderConsumer.accept(builder); return builder.build(); }
public Builder(GraphQLInputObjectField existing) { this.name = existing.getName(); this.description = existing.getDescription(); this.defaultValue = existing.getDefaultValue(); this.type = existing.getType(); this.definition = existing.getDefinition(); this.directives.putAll(getByName(existing.getDirectives(), GraphQLDirective::getName)); }
public GraphQLInputObjectField build() { return new GraphQLInputObjectField(name, description, type, defaultValue, valuesToList(directives), definition); } }
private boolean alwaysHasValue(GraphQLInputObjectField inputField) { return inputField.getDefaultValue() != null || isNonNull(inputField.getType()); }
private Object coerceValueForInputObjectType(GraphqlFieldVisibility fieldVisibility, VariableDefinition variableDefinition, GraphQLInputObjectType inputObjectType, Map<String, Object> input) { Map<String, Object> result = new LinkedHashMap<>(); List<GraphQLInputObjectField> fields = fieldVisibility.getFieldDefinitions(inputObjectType); List<String> fieldNames = fields.stream().map(GraphQLInputObjectField::getName).collect(Collectors.toList()); for (String inputFieldName : input.keySet()) { if (!fieldNames.contains(inputFieldName)) { throw new InputMapDefinesTooManyFieldsException(inputObjectType, inputFieldName); } } for (GraphQLInputObjectField inputField : fields) { if (input.containsKey(inputField.getName()) || alwaysHasValue(inputField)) { Object value = coerceValue(fieldVisibility, variableDefinition, inputField.getName(), inputField.getType(), input.get(inputField.getName())); result.put(inputField.getName(), value == null ? inputField.getDefaultValue() : value); } } return result; }
private TypePrinter<GraphQLInputObjectType> inputObjectPrinter() { return (out, type, visibility) -> { if (isIntrospectionType(type)) { return; } printComments(out, type, ""); out.format("input %s%s {\n", type.getName(), directivesString(type.getDirectives())); visibility.getFieldDefinitions(type) .stream() .sorted(Comparator.comparing(GraphQLInputObjectField::getName)) .forEach(fd -> { printComments(out, fd, " "); out.format(" %s: %s", fd.getName(), typeString(fd.getType())); Object defaultValue = fd.getDefaultValue(); if (defaultValue != null) { String astValue = printAst(defaultValue, fd.getType()); out.format(" = %s", astValue); } out.format(directivesString(fd.getDirectives())); out.format("\n"); }); out.format("}\n\n"); }; }
@Test(expectedExceptions = InvalidDirectiveLocationException.class) public void wireGraphQLInputObjectField_invalidLocations_exceptionIsThrown() throws Exception { // Arrange AnnotationsDirectiveWiring upperWiring = mock(AnnotationsDirectiveWiring.class); GraphQLInputObjectField directiveContainer = GraphQLInputObjectField.newInputObjectField().name("asdf") .type(GraphQLInputObjectType.newInputObject().name("dfdf").build()).build(); AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); when(upperWiring.onInputObjectField(upperCaseEnv)).thenReturn(directiveContainer); HashMap<GraphQLDirective, AnnotationsDirectiveWiring> map = new HashMap<>(); GraphQLDirective upperCase = newDirective().name("upperCase").build(); map.put(upperCase, upperWiring); // Act directiveWirer.wire(directiveContainer, map); }
private void buildMap(List<GraphQLInputObjectField> fields) { for (GraphQLInputObjectField field : fields) { String name = field.getName(); if (fieldMap.containsKey(name)) throw new AssertException("field " + name + " redefined"); fieldMap.put(name, field); } }
private Set<String> getMissingFields(GraphQLInputObjectType type, Map<String, ObjectField> objectFieldMap, GraphqlFieldVisibility fieldVisibility) { return fieldVisibility.getFieldDefinitions(type).stream() .filter(field -> isNonNull(field.getType())) .map(GraphQLInputObjectField::getName) .filter(((Predicate<String>) objectFieldMap::containsKey).negate()) .collect(Collectors.toSet()); }
@Override public GraphQLInputObjectField transformInputField(GraphQLInputObjectField field, InputField inputField, OperationMapper operationMapper, BuildContext buildContext) { if (field.getDefaultValue() == null && shouldWrap(field.getType(), inputField.getTypedElement())) { return field.transform(builder -> builder.type(new GraphQLNonNull(field.getType()))); } if (shouldUnwrap(field.getDefaultValue(), field.getType())) { //do not warn on primitives as their non-nullness is implicit if (!ClassUtils.getRawType(inputField.getJavaType().getType()).isPrimitive()) { log.warn("Non-null input field with a default value will be treated as nullable: " + inputField); } return field.transform(builder -> builder.type((GraphQLInputType) GraphQLUtils.unwrapNonNull(field.getType()))); } return field; }
@Test public void argMutation() { GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); GraphQLFieldDefinition doSomethingElse = object.getFieldDefinition("doSomethingElse"); assertNotNull(doSomethingElse); assertEquals(doSomethingElse.getArguments().size(), 1); GraphQLInputType input = doSomethingElse.getArgument("input").getType(); assertTrue(input instanceof GraphQLNonNull); GraphQLType inputType = ((graphql.schema.GraphQLNonNull) input).getWrappedType(); assertTrue(inputType instanceof GraphQLInputObjectType); GraphQLInputObjectType inputType_ = (GraphQLInputObjectType) inputType; assertNotNull(inputType_.getField("a")); assertNotNull(inputType_.getField("b")); assertEquals(inputType_.getField("a").getDescription(), "A"); assertTrue(doSomethingElse.getType() instanceof GraphQLObjectType); GraphQLObjectType returnType = (GraphQLObjectType) doSomethingElse.getType(); assertNotNull(returnType.getFieldDefinition("getI")); assertNotNull(returnType.getFieldDefinition("clientMutationId")); GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build(); ExecutionResult result = graphQL.execute("mutation { doSomethingElse(input: {a: 0, b: 1, clientMutationId: \"1\"}) { getI clientMutationId } }", new TestObject()); assertEquals(result.getErrors().size(), 0); Map<String, Object> returns = (Map<String, Object>) ((Map<String, Object>) result.getData()).get("doSomethingElse"); assertEquals(returns.get("getI"), -1); assertEquals(returns.get("clientMutationId"), "1"); }
if (inputValueFieldsByName.containsKey(inputTypeField.getName())) { boolean putObjectInMap = true; ObjectField field = inputValueFieldsByName.get(inputTypeField.getName()); Value fieldInputValue = field.getValue(); fieldObject = coerceValueAst(fieldVisibility, inputTypeField.getType(), fieldInputValue, variables); fieldObject = inputTypeField.getDefaultValue(); assertNonNullInputField(inputTypeField); } else if (inputTypeField.getDefaultValue() != null) { result.put(inputTypeField.getName(), inputTypeField.getDefaultValue()); } else { assertNonNullInputField(inputTypeField);
private void check(GraphQLInputObjectType type, Set<GraphQLType> seen, List<String> path, SchemaValidationErrorCollector validationErrorCollector) { if (seen.contains(type)) { validationErrorCollector.addError(new SchemaValidationError(SchemaValidationErrorType.UnbrokenInputCycle, getErrorMessage(path))); return; } seen.add(type); for (GraphQLInputObjectField field : type.getFieldDefinitions()) { if (isNonNull(field.getType())) { GraphQLType unwrapped = unwrapNonNull((GraphQLNonNull) field.getType()); if (unwrapped instanceof GraphQLInputObjectType) { path = new ArrayList<>(path); path.add(field.getName() + "!"); check((GraphQLInputObjectType) unwrapped, new LinkedHashSet<>(seen), path, validationErrorCollector); } } } }
private TypePrinter<GraphQLInputObjectType> inputObjectPrinter() { return (out, type, visibility) -> { if (isIntrospectionType(type)) { return; } printComments(out, type, ""); SchemaPrinterComparatorEnvironment environment = SchemaPrinterComparatorEnvironment.newEnvironment() .parentType(GraphQLInputObjectType.class) .elementType(GraphQLInputObjectField.class) .build(); Comparator<? super GraphQLType> comparator = options.comparatorRegistry.getComparator(environment); out.format("input %s%s {\n", type.getName(), directivesString(GraphQLInputObjectType.class, type.getDirectives())); visibility.getFieldDefinitions(type) .stream() .sorted(comparator) .forEach(fd -> { printComments(out, fd, " "); out.format(" %s: %s", fd.getName(), typeString(fd.getType())); Object defaultValue = fd.getDefaultValue(); if (defaultValue != null) { String astValue = printAst(defaultValue, fd.getType()); out.format(" = %s", astValue); } out.format(directivesString(GraphQLInputObjectField.class, fd.getDirectives())); out.format("\n"); }); out.format("}\n\n"); }; }
@Test public void wireGraphQLInputObjectField_validLocations_correctMethodIsCalled() throws Exception { // Arrange AnnotationsDirectiveWiring upperWiring = mock(AnnotationsDirectiveWiring.class); GraphQLInputObjectField directiveContainer = GraphQLInputObjectField.newInputObjectField().name("asdf") .type(GraphQLInputObjectType.newInputObject().name("dfdf").build()).build(); AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); when(upperWiring.onInputObjectField(upperCaseEnv)).thenReturn(directiveContainer); HashMap<GraphQLDirective, AnnotationsDirectiveWiring> map = new HashMap<>(); GraphQLDirective upperCase = newDirective().name("upperCase").validLocations(Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION).build(); map.put(upperCase, upperWiring); // Act directiveWirer.wire(directiveContainer, map); // Assert verify(upperWiring).onInputObjectField(upperCaseEnv); }
public Builder field(GraphQLInputObjectField field) { assertNotNull(field, "field can't be null"); fields.put(field.getName(), field); return this; }
private void assertNonNullInputField(GraphQLInputObjectField inputTypeField) { if (isNonNull(inputTypeField.getType())) { throw new NonNullableValueCoercedAsNullException(inputTypeField); } }
private boolean alwaysHasValue(GraphQLInputObjectField inputField) { return inputField.getDefaultValue() != null || isNonNull(inputField.getType()); }
/** * Take a field builder in a function definition and apply. Can be used in a jdk8 lambda * e.g.: * <pre> * {@code * field(f -> f.name("fieldName")) * } * </pre> * * @param builderFunction a supplier for the builder impl * * @return this */ public Builder field(UnaryOperator<GraphQLInputObjectField.Builder> builderFunction) { assertNotNull(builderFunction, "builderFunction should not be null"); GraphQLInputObjectField.Builder builder = GraphQLInputObjectField.newInputObjectField(); builder = builderFunction.apply(builder); return field(builder); }