graphql.schema.GraphQLInputObjectField Java Examples

The following examples show how to use graphql.schema.GraphQLInputObjectField. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultTypeInfoGenerator.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
@Override
public GraphqlTypeComparatorRegistry generateComparatorRegistry(AnnotatedType type, MessageBundle messageBundle) {
    if (!isOrdered(type)) {
        return DEFAULT_REGISTRY;
    }
    return new GraphqlTypeComparatorRegistry() {
        @Override
        public <T extends GraphQLSchemaElement> Comparator<? super T> getComparator(GraphqlTypeComparatorEnvironment env) {
            if (env.getElementType().equals(GraphQLFieldDefinition.class)) {
                return comparator(getFieldOrder(type, messageBundle), env);
            }
            if (env.getElementType().equals(GraphQLInputObjectField.class)
                    || env.getElementType().equals(GraphQLEnumValueDefinition.class)) {
                return comparator(getInputFieldOrder(type, messageBundle), env);
            }
            return DEFAULT_REGISTRY.getComparator(env);
        }
    };
}
 
Example #2
Source File: EnumMapToObjectTypeAdapter.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
@Override
protected GraphQLInputObjectType toGraphQLInputType(String typeName, AnnotatedType javaType, TypeMappingEnvironment env) {
    BuildContext buildContext = env.buildContext;

    GraphQLInputObjectType.Builder builder = GraphQLInputObjectType.newInputObject()
            .name(typeName)
            .description(buildContext.typeInfoGenerator.generateInputTypeDescription(javaType, buildContext.messageBundle));

    @SuppressWarnings("rawtypes")
    Enum[] keys = (Enum[]) ClassUtils.getRawType(getElementType(javaType, 0).getType()).getEnumConstants();
    Arrays.stream(keys).forEach(enumValue -> {
        TypedElement element = new TypedElement(getElementType(javaType, 1), ClassUtils.getEnumConstantField(enumValue));
        builder.field(GraphQLInputObjectField.newInputObjectField()
                .name(enumMapper.getValueName(enumValue, buildContext.messageBundle))
                .description(enumMapper.getValueDescription(enumValue, buildContext.messageBundle))
                .type(env.forElement(element).toGraphQLInputType(element.getJavaType()))
                .build());
    });
    return builder.build();
}
 
Example #3
Source File: FieldsGenerator.java    From graphql-java-type-generator with MIT License 6 votes vote down vote up
/**
 * May return null should this field be disallowed
 * @param object
 * @return
 */
protected GraphQLInputObjectField.Builder getInputFieldDefinition(
        final Object object) {
    String fieldName = getFieldName(object);
    GraphQLInputType fieldType = (GraphQLInputType)
            getTypeOfField(object, TypeKind.INPUT_OBJECT);
    if (fieldName == null || fieldType == null) {
        return null;
    }
    String fieldDescription  = getFieldDescription(object);
    String fieldDefaultValue  = getFieldDefaultValue(object);
    logger.debug("GraphQL field will be of type [{}] and name [{}] with description [{}]",
            fieldType, fieldName, fieldDescription);
    
    GraphQLInputObjectField.Builder fieldBuilder = newInputObjectField()
            .name(fieldName)
            .type(fieldType)
            .description(fieldDescription)
            .defaultValue(fieldDefaultValue);
    return fieldBuilder;
}
 
Example #4
Source File: FullTypeGenerator.java    From graphql-java-type-generator with MIT License 6 votes vote down vote up
protected GraphQLInputType generateInputType(Object object) {
    //An enum is a special case in both java and graphql,
    //and must be checked for while generating other kinds of types
    GraphQLEnumType enumType = generateEnumType(object);
    if (enumType != null) {
        return enumType;
    }
    
    List<GraphQLInputObjectField> fields = getInputFieldDefinitions(object);
    if (fields == null || fields.isEmpty()) {
        return null;
    }
    String typeName = getGraphQLTypeNameOrIdentityCode(object);
    
    GraphQLInputObjectType.Builder builder = new GraphQLInputObjectType.Builder();
    builder.name(typeName);
    builder.fields(fields);
    builder.description(getTypeDescription(object));
    return builder.build();
}
 
Example #5
Source File: TypeGeneratorParameterizedTest.java    From graphql-java-type-generator with MIT License 5 votes vote down vote up
public void basicsInput() {
    if (expectedToSucceed == false) {
        try {
            generator.getInputType(clazz);
            fail("Should have caught exception");
        }
        catch (Exception e) {
            Assert.assertThat(e, instanceOf(RuntimeException.class));
        }
        return;
    }
    
    GraphQLType type = generator.getInputType(clazz);
    Assert.assertThat(type,
            instanceOf(GraphQLInputObjectType.class));
    GraphQLInputObjectType objectType = (GraphQLInputObjectType) type;
    Assert.assertThat(objectType.getName(),
            containsString(expectedName));
    Assert.assertThat(objectType.getDescription(),
            containsString("Autogenerated f"));
    Assert.assertThat(objectType.getFields(),
            notNullValue());
    for (GraphQLInputObjectField field : objectType.getFields()) {
        Assert.assertThat(field,
                notNullValue());
        Assert.assertThat(field.getDescription(),
                containsString("Autogenerated f"));
    }
    Assert.assertThat(objectType.getFields().size(),
            is(expectedNumFields));
}
 
Example #6
Source File: DirectiveTest.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Test
public void testSchemaDirectives() {
    GraphQLSchema schema = new TestSchemaGenerator()
            .withOperationsFromSingleton(new ServiceWithDirectives())
            .generate();

    GraphQLFieldDefinition scalarField = schema.getQueryType().getFieldDefinition("scalar");
    assertDirective(scalarField, "fieldDef", "fieldDef");

    GraphQLScalarType scalarResult = (GraphQLScalarType) scalarField.getType();
    assertDirective(scalarResult, "scalar", "scalar");

    graphql.schema.GraphQLArgument argument = scalarField.getArgument("in");
    assertDirective(argument, "argDef", "argument");

    GraphQLInputObjectType inputType = (GraphQLInputObjectType) argument.getType();
    assertDirective(inputType, "inputObjectType", "input");
    graphql.schema.GraphQLArgument directiveArg = DirectivesUtil.directiveWithArg(inputType.getDirectives(), "inputObjectType", "value").get();
    Optional<graphql.schema.GraphQLArgument> metaArg = DirectivesUtil.directiveWithArg(directiveArg.getDirectives(), "meta", "value");
    assertTrue(metaArg.isPresent());
    assertEquals("meta", metaArg.get().getValue());

    GraphQLInputObjectField inputField = inputType.getField("value");
    assertDirective(inputField, "inputFieldDef", "inputField");

    GraphQLFieldDefinition objField = schema.getQueryType().getFieldDefinition("obj");
    GraphQLObjectType objResult = (GraphQLObjectType) objField.getType();
    assertDirective(objResult, "objectType", "object");

    GraphQLFieldDefinition innerField = objResult.getFieldDefinition("value");
    assertDirective(innerField, "fieldDef", "field");
}
 
Example #7
Source File: OperationMapper.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
/**
 * Maps a single field/property to a GraphQL input field.
 *
 * @param inputField The field/property to map to a GraphQL input field
 * @param buildContext The shared context containing all the global information needed for mapping
 *
 * @return GraphQL input field representing the given field/property
 */
public GraphQLInputObjectField toGraphQLInputField(InputField inputField, BuildContext buildContext) {
    GraphQLInputObjectField.Builder builder = newInputObjectField()
            .name(inputField.getName())
            .description(inputField.getDescription())
            .type(toGraphQLInputType(inputField.getJavaType(), new TypeMappingEnvironment(inputField.getTypedElement(), this, buildContext)))
            .withDirective(Directives.mappedInputField(inputField))
            .withDirectives(toGraphQLDirectives(inputField.getTypedElement(), buildContext.directiveBuilder::buildInputFieldDefinitionDirectives, buildContext))
            .defaultValue(inputField.getDefaultValue());
    return buildContext.transformers.transform(builder.build(), inputField, this, buildContext);
}
 
Example #8
Source File: NonNullMapper.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #9
Source File: FederationSdlPrinter.java    From federation-jvm with MIT License 5 votes vote down vote up
private TypePrinter<GraphQLInputObjectType> inputObjectPrinter() {
    return (out, type, visibility) -> {
        if (isIntrospectionType(type)) {
            return;
        }
        if (shouldPrintAsAst(type.getDefinition())) {
            printAsAst(out, type.getDefinition(), type.getExtensionDefinitions());
        } else {
            printComments(out, type, "");
            GraphqlTypeComparatorEnvironment environment = GraphqlTypeComparatorEnvironment.newEnvironment()
                    .parentType(GraphQLInputObjectType.class)
                    .elementType(GraphQLInputObjectField.class)
                    .build();
            Comparator<? super GraphQLSchemaElement> comparator = options.comparatorRegistry.getComparator(environment);

            out.format("input %s%s", type.getName(), directivesString(GraphQLInputObjectType.class, type.getDirectives()));
            List<GraphQLInputObjectField> inputObjectFields = visibility.getFieldDefinitions(type);
            if (inputObjectFields.size() > 0) {
                out.format(" {\n");
                inputObjectFields
                        .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("}");
            }
            out.format("\n\n");
        }
    };
}
 
Example #10
Source File: FieldsGenerator.java    From graphql-java-type-generator with MIT License 5 votes vote down vote up
@Override
public List<GraphQLInputObjectField> getInputFields(Object object) {
    List<GraphQLInputObjectField> fieldDefs = new ArrayList<GraphQLInputObjectField>();
    List<Object> fieldObjects = getFieldRepresentativeObjects(object);
    if (fieldObjects == null) {
        return fieldDefs;
    }

    Set<String> fieldNames = new HashSet<String>();
    for (Object field : fieldObjects) {
        GraphQLInputObjectField.Builder fieldBuilder =
                getInputFieldDefinition(field);
        if (fieldBuilder == null) {
            continue;
        }
        
        GraphQLInputObjectField fieldDef = fieldBuilder.build();
        //check for shadowed fields, where field "item" in superclass
        //is shadowed by field "item" in subclass
        if (fieldNames.contains(fieldDef.getName())) {
            continue;
        }
        fieldNames.add(fieldDef.getName());
        fieldDefs.add(fieldDef);
    }
    return fieldDefs;
}
 
Example #11
Source File: DirectivesAndTypeWalker.java    From samples with MIT License 5 votes vote down vote up
private static boolean walkInputType(GraphQLInputType inputType, List<GraphQLDirective> directives, BiFunction<GraphQLInputType, GraphQLDirective, Boolean> isSuitable) {
  GraphQLInputType unwrappedInputType = Util.unwrapNonNull(inputType);
  for (GraphQLDirective directive : directives) {
    if (isSuitable.apply(unwrappedInputType, directive)) {
      return true;
    }
  }
  if (unwrappedInputType instanceof GraphQLInputObjectType) {
    GraphQLInputObjectType inputObjType = (GraphQLInputObjectType) unwrappedInputType;
    for (GraphQLInputObjectField inputField : inputObjType.getFieldDefinitions()) {
      inputType = inputField.getType();
      directives = inputField.getDirectives();

      if (walkInputType(inputType, directives, isSuitable)) {
        return true;
      }
    }
  }
  if (unwrappedInputType instanceof GraphQLList) {
    GraphQLInputType innerListType = Util.unwrapOneAndAllNonNull(unwrappedInputType);
    if (innerListType instanceof GraphQLDirectiveContainer) {
      directives = ((GraphQLDirectiveContainer) innerListType).getDirectives();
      if (walkInputType(innerListType, directives, isSuitable)) {
        return true;
      }
    }
  }
  return false;
}
 
Example #12
Source File: Bootstrap.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
private GraphQLInputObjectField createGraphQLInputObjectFieldFromField(Field field) {
    GraphQLInputObjectField.Builder inputFieldBuilder = GraphQLInputObjectField.newInputObjectField()
            .name(field.getName())
            .description(field.getDescription());

    // Type
    inputFieldBuilder = inputFieldBuilder.type(createGraphQLInputType(field));

    // Default value (on method)
    inputFieldBuilder = inputFieldBuilder.defaultValue(sanitizeDefaultValue(field));

    return inputFieldBuilder.build();
}
 
Example #13
Source File: Bootstrap.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
private List<GraphQLInputObjectField> createGraphQLInputObjectFieldsFromFields(Set<Field> fields) {
    List<GraphQLInputObjectField> graphQLInputObjectFields = new ArrayList<>();
    for (Field field : fields) {
        graphQLInputObjectFields.add(createGraphQLInputObjectFieldFromField(field));
    }
    return graphQLInputObjectFields;
}
 
Example #14
Source File: TypeGenerator.java    From graphql-java-type-generator with MIT License 4 votes vote down vote up
protected List<GraphQLInputObjectField> getInputFieldDefinitions(Object object) {
    List<GraphQLInputObjectField> definitions = 
            getContext().getFieldsGeneratorStrategy()
                    .getInputFields(object);
    return definitions;
}
 
Example #15
Source File: SchemaTransformerRegistry.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
public GraphQLInputObjectField transform(GraphQLInputObjectField field, InputField inputField, OperationMapper operationMapper, BuildContext buildContext) {
    for (SchemaTransformer transformer : transformers) {
        field = transformer.transformInputField(field, inputField, operationMapper, buildContext);
    }
    return field;
}
 
Example #16
Source File: SchemaTransformer.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
default GraphQLInputObjectField transformInputField(GraphQLInputObjectField field, InputField inputField, OperationMapper operationMapper, BuildContext buildContext) {
    return field;
}
 
Example #17
Source File: Directives.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
public static Optional<InputField> getMappedInputField(GraphQLInputObjectField field) {
    return DirectivesUtil.directiveWithArg(field.getDirectives(), MAPPED_INPUT_FIELD, INPUT_FIELD)
            .map(arg -> (InputField) arg.getValue());
}
 
Example #18
Source File: IFieldsGenerator.java    From graphql-java-type-generator with MIT License votes vote down vote up
List<GraphQLInputObjectField> getInputFields(Object object);