graphql.schema.GraphQLInputType Java Examples

The following examples show how to use graphql.schema.GraphQLInputType. 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: Bootstrap.java    From smallrye-graphql with Apache License 2.0 6 votes vote down vote up
private GraphQLInputType createGraphQLInputType(Field field) {

        GraphQLInputType graphQLInputType = referenceGraphQLInputType(field);

        // Collection
        if (field.hasArray()) {
            Array array = field.getArray();
            // Mandatory in the collection
            if (array.isNotEmpty()) {
                graphQLInputType = GraphQLNonNull.nonNull(graphQLInputType);
            }
            // Collection depth
            for (int i = 0; i < array.getDepth(); i++) {
                graphQLInputType = GraphQLList.list(graphQLInputType);
            }
        }

        // Mandatory
        if (field.isNotNull()) {
            graphQLInputType = GraphQLNonNull.nonNull(graphQLInputType);
        }

        return graphQLInputType;
    }
 
Example #2
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 #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: ArgumentType_Reflection.java    From graphql-java-type-generator with MIT License 6 votes vote down vote up
@Override
public GraphQLInputType getArgumentType(ArgContainer container) {
    if (container == null) return null;
    Object object = container.getRepresentativeObject();
    if (object == null) return null;
    
    if (object instanceof ParameterizedType
            || object instanceof WildcardType
            || object instanceof TypeVariable) {
        return (GraphQLInputType) getContext().getParameterizedType(
                object,
                (Type) object,
                TypeKind.INPUT_OBJECT);
    }
    return getContext().getInputType(object);
}
 
Example #5
Source File: ArgumentsGenerator.java    From graphql-java-type-generator with MIT License 6 votes vote down vote up
protected GraphQLArgument.Builder getArgument(ArgContainer argObject) {
    String name = getStrategies().getArgumentNameStrategy().getArgumentName(argObject);
    GraphQLInputType type = getStrategies().getArgumentTypeStrategy().getArgumentType(argObject);
    if (name == null || type == null) {
        return null;
    }
    
    String description = getStrategies().getArgumentDescriptionStrategy().getArgumentDescription(argObject);
    Object defaultValue = getStrategies().getArgumentDefaultValueStrategy().getArgumentDefaultValue(argObject);
    GraphQLArgument.Builder builder = GraphQLArgument.newArgument()
            .name(name)
            .type(type)
            .defaultValue(defaultValue)
            .description(description);
    return builder;
}
 
Example #6
Source File: ManualGraphQLMutationSchema.java    From research-graphql with MIT License 6 votes vote down vote up
@Bean
@Qualifier("InputProductType")
GraphQLInputType getInputProductType() {
    return newInputObject()
            .name("ProductDtoInput")
            .field(newInputObjectField()
                    .name("productId")
                    .type(GraphQLID))
            .field(newInputObjectField()
                    .name("name")
                    .type(GraphQLString))
            .field(newInputObjectField()
                    .name("brand")
                    .type(GraphQLString))
            .field(newInputObjectField()
                    .name("description")
                    .type(GraphQLString))
            .field(newInputObjectField()
                    .name("category")
                    .type(list(GraphQLString)))
            .field(newInputObjectField()
                    .name("price")
                    .type(GraphQLFloat))
            .build();
}
 
Example #7
Source File: RangeDirective.java    From samples with MIT License 6 votes vote down vote up
private DataFetcher createDataFetcher(DataFetcher originalFetcher) {
  return (env) -> {
    List<GraphQLError> errors = new ArrayList<>();
    env.getFieldDefinition().getArguments().stream().forEach(it -> {
      if (appliesTo(it)) {
        errors.addAll(apply(it, env, env.getArgument(it.getName())));
      }

      GraphQLInputType unwrappedInputType = Util.unwrapNonNull(it.getType());
      if (unwrappedInputType instanceof GraphQLInputObjectType) {
        GraphQLInputObjectType inputObjType = (GraphQLInputObjectType) unwrappedInputType;
        inputObjType.getFieldDefinitions().stream().filter(this::appliesTo).forEach(io -> {
          Map<String, Object> value = env.getArgument(it.getName());
          errors.addAll(apply(io, env, value.get(io.getName())));
        });
      }
    });

    Object returnValue = originalFetcher.get(env);
    if (errors.isEmpty()) {
      return returnValue;
    }
    return Util.mkDFRFromFetchedResult(errors, returnValue);
  };
}
 
Example #8
Source File: MapToListTypeAdapter.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
private GraphQLInputType mapEntry(GraphQLInputType keyType, GraphQLInputType valueType, BuildContext buildContext) {
    String typeName = "mapEntry_" + getTypeName(keyType) + "_" + getTypeName(valueType) + "_input";
    if (buildContext.typeCache.contains(typeName)) {
        return new GraphQLTypeReference(typeName);
    }
    buildContext.typeCache.register(typeName);

    return newInputObject()
            .name(typeName)
            .description("Map entry input")
            .field(newInputObjectField()
                    .name("key")
                    .description("Map key input")
                    .type(keyType)
                    .build())
            .field(newInputObjectField()
                    .name("value")
                    .description("Map value input")
                    .type(valueType)
                    .build())
            .build();
}
 
Example #9
Source File: CachingMapper.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Override
public GraphQLInputType toGraphQLInputType(AnnotatedType javaType, Set<Class<? extends TypeMapper>> mappersToSkip, TypeMappingEnvironment env) {
    String typeName = getInputTypeName(javaType, env.buildContext);
    if (env.buildContext.typeCache.contains(typeName)) {
        return new GraphQLTypeReference(typeName);
    }
    env.buildContext.typeCache.register(typeName);
    return toGraphQLInputType(typeName, javaType, env);
}
 
Example #10
Source File: CachingMapper.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
private String getTypeName(AnnotatedType javaType, AnnotatedType graphQLType, TypeInfoGenerator typeInfoGenerator, MessageBundle messageBundle) {
    if (ClassUtils.isSuperClass(GraphQLScalarType.class, graphQLType)) {
        return typeInfoGenerator.generateScalarTypeName(javaType, messageBundle);
    }
    if (ClassUtils.isSuperClass(GraphQLEnumType.class, graphQLType)) {
        return typeInfoGenerator.generateEnumTypeName(javaType, messageBundle);
    }
    if (ClassUtils.isSuperClass(GraphQLInputType.class, graphQLType)) {
        return typeInfoGenerator.generateInputTypeName(javaType, messageBundle);
    }
    return typeInfoGenerator.generateTypeName(javaType, messageBundle);
}
 
Example #11
Source File: NonNullMapper.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
private GraphQLArgument transformArgument(GraphQLArgument argument, TypedElement element, String description, OperationMapper operationMapper, BuildContext buildContext) {
    if (argument.getDefaultValue() == null && shouldWrap(argument.getType(), element)) {
        return argument.transform(builder -> builder.type(new GraphQLNonNull(argument.getType())));
    }
    if (shouldUnwrap(argument.getDefaultValue(), argument.getType())) {
        //do not warn on primitives as their non-nullness is implicit
        if (!ClassUtils.getRawType(element.getJavaType().getType()).isPrimitive()) {
            log.warn("Non-null argument with a default value will be treated as nullable: " + description);
        }
        return argument.transform(builder -> builder.type((GraphQLInputType) GraphQLUtils.unwrapNonNull(argument.getType())));
    }
    return argument;
}
 
Example #12
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 #13
Source File: MapToListTypeAdapter.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Override
public GraphQLInputType toGraphQLInputType(AnnotatedType javaType, Set<Class<? extends TypeMapper>> mappersToSkip, TypeMappingEnvironment env) {
    return new GraphQLList(new GraphQLNonNull(
            mapEntry(
                    //MapEntry fields are artificial - no Java element is backing them
                    env.forElement(null).toGraphQLInputType(getElementType(javaType, 0)),
                    env.forElement(null).toGraphQLInputType(getElementType(javaType, 1)), env.buildContext)));
}
 
Example #14
Source File: GraphQLTypeAssertions.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
public static void assertInputMapOf(GraphQLType mapType, Class<? extends GraphQLType> keyType, Class<? extends GraphQLType> valueType) {
    assertEquals(GraphQLList.class, mapType.getClass());
    GraphQLType entry = GraphQLUtils.unwrap(mapType);
    assertTrue(entry instanceof GraphQLInputObjectType);
    GraphQLInputType key = ((GraphQLInputObjectType) entry).getFieldDefinition("key").getType();
    GraphQLInputType value = ((GraphQLInputObjectType) entry).getFieldDefinition("value").getType();
    assertTrue(keyType.isAssignableFrom(key.getClass()));
    assertTrue(valueType.isAssignableFrom(value.getClass()));
}
 
Example #15
Source File: Validator.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
ValidationResult checkUniqueness(GraphQLInputType graphQLType, AnnotatedElement element, AnnotatedType javaType) {
    return checkUniqueness(graphQLType, () -> {
        AnnotatedType inputType = environment.getMappableInputType(javaType);
        if (GenericTypeReflector.equals(javaType, inputType)) {
            return mappers.getMappableType(element, javaType);
        }
        return inputType;
    });
}
 
Example #16
Source File: GraphQLTypeFactory.java    From glitr with MIT License 5 votes vote down vote up
/**
 * Creates a {@link GraphQLInputType} by inspecting the given a class
 *
 * @param clazz to be evaluated
 * @return {@link GraphQLInputType}}
 */
public GraphQLInputType createGraphQLInputType(Class clazz) {
    JavaType javaType = getJavaTypeFromClass(clazz);
    if (javaType == JavaType.ENUM) {
        return (GraphQLInputType) delegateFactories.get(JavaType.ENUM).create(clazz);
    }
    return (GraphQLInputType) delegateInputFactories.get(javaType).create(clazz);
}
 
Example #17
Source File: StaticTypeRepository.java    From graphql-java-type-generator with MIT License 5 votes vote down vote up
/**
 * Resets the internal data of the TypeRepository to empty
 */
public void clear() {
    //anyone working on the old types doesn't want to have
    //their generated*Types .clear()ed out from under them. 
    generatedOutputTypes =
            new ConcurrentHashMap<String, GraphQLOutputType>();
    generatedInputTypes =
            new ConcurrentHashMap<String, GraphQLInputType>();
}
 
Example #18
Source File: StaticTypeRepository.java    From graphql-java-type-generator with MIT License 5 votes vote down vote up
@Override
public GraphQLType registerType(String typeName, GraphQLType graphQlType, TypeKind typeKind) {
    switch (typeKind) {
    case OBJECT:
    case INTERFACE:
        return registerType(typeName, (GraphQLOutputType) graphQlType);
    case INPUT_OBJECT:
        return registerType(typeName, (GraphQLInputType) graphQlType);
    default:
        return null;
    }
}
 
Example #19
Source File: GraphQLSchemaBuilder.java    From graphql-jpa with MIT License 5 votes vote down vote up
private Stream<GraphQLArgument> getArgument(Attribute attribute) {
    return getAttributeType(attribute)
            .filter(type -> type instanceof GraphQLInputType)
            .filter(type -> attribute.getPersistentAttributeType() != Attribute.PersistentAttributeType.EMBEDDED ||
                    (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED && type instanceof GraphQLScalarType))
            .map(type -> {
                String name = attribute.getName();                   

                return GraphQLArgument.newArgument()
                        .name(name)
                        .type((GraphQLInputType) type)
                        .build();
            });
}
 
Example #20
Source File: Bootstrap.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
private GraphQLInputType referenceGraphQLInputType(Field field) {
    Reference reference = getCorrectFieldReference(field);
    ReferenceType type = reference.getType();
    String className = reference.getClassName();
    String name = reference.getName();
    switch (type) {
        case SCALAR:
            return getCorrectScalarType(reference);
        case ENUM:
            return enumMap.get(className);
        default:
            return GraphQLTypeReference.typeRef(name);
    }
}
 
Example #21
Source File: Bootstrap.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
private GraphQLArgument createGraphQLArgument(Argument argument) {
    GraphQLArgument.Builder argumentBuilder = GraphQLArgument.newArgument()
            .name(argument.getName())
            .description(argument.getDescription())
            .defaultValue(sanitizeDefaultValue(argument));

    GraphQLInputType graphQLInputType = referenceGraphQLInputType(argument);

    // Collection
    if (argument.hasArray()) {
        Array array = argument.getArray();
        // Mandatory in the collection
        if (array.isNotEmpty()) {
            graphQLInputType = GraphQLNonNull.nonNull(graphQLInputType);
        }
        // Collection depth
        for (int i = 0; i < array.getDepth(); i++) {
            graphQLInputType = GraphQLList.list(graphQLInputType);
        }
    }

    // Mandatory
    if (argument.isNotNull()) {
        graphQLInputType = GraphQLNonNull.nonNull(graphQLInputType);
    }

    argumentBuilder = argumentBuilder.type(graphQLInputType);

    return argumentBuilder.build();

}
 
Example #22
Source File: Util.java    From samples with MIT License 5 votes vote down vote up
/**
 * This will unwrap one level of List ness and ALL levels of NonNull ness.
 *
 * @param inputType the type to unwrap
 *
 * @return an input type
 */
public static GraphQLInputType unwrapOneAndAllNonNull(GraphQLInputType inputType) {
  GraphQLType type = GraphQLTypeUtil.unwrapNonNull(inputType);
  type = GraphQLTypeUtil.unwrapOne(type); // one level
  type = GraphQLTypeUtil.unwrapNonNull(type);
  if (type instanceof GraphQLInputType) {
    return (GraphQLInputType) type;
  } else {
    String argType = GraphQLTypeUtil.simplePrint(inputType);
    return Assert.assertShouldNeverHappen("You have a wrapped type that is in fact not a input type : %s", argType);
  }
}
 
Example #23
Source File: Util.java    From samples with MIT License 5 votes vote down vote up
public static GraphQLInputType unwrapNonNull(GraphQLInputType inputType) {
  GraphQLType type = GraphQLTypeUtil.unwrapNonNull(inputType);
  if (type instanceof GraphQLInputType) {
    return (GraphQLInputType) type;
  } else {
    String argType = GraphQLTypeUtil.simplePrint(inputType);
    return Assert.assertShouldNeverHappen("You have a wrapped type that is in fact not a input type : %s", argType);
  }
}
 
Example #24
Source File: RangeDirective.java    From samples with MIT License 5 votes vote down vote up
private boolean appliesTo(GraphQLFieldDefinition fieldDefinition) {
  return fieldDefinition.getArguments().stream().anyMatch(it -> {
    if (appliesTo(it)) {
      return true;
    }
    GraphQLInputType unwrappedInputType = Util.unwrapNonNull(it.getType());
    if (unwrappedInputType instanceof GraphQLInputObjectType) {
      GraphQLInputObjectType inputObjType = (GraphQLInputObjectType) unwrappedInputType;
      return inputObjType.getFieldDefinitions().stream().anyMatch(this::appliesTo);
    }
    return false;
  });
}
 
Example #25
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 #26
Source File: PageMapper.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@Override
public GraphQLInputType toGraphQLInputType(AnnotatedType javaType, Set<Class<? extends TypeMapper>> mappersToSkip, TypeMappingEnvironment env) {
    throw new UnsupportedOperationException("Replay page type can not be used as input type");
}
 
Example #27
Source File: FederationSdlPrinter.java    From federation-jvm with MIT License 4 votes vote down vote up
private static String printAst(Object value, GraphQLInputType type) {
    return AstPrinter.printAst(AstValueHelper.astFromValue(value, type));
}
 
Example #28
Source File: JsonNodeAdapter.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@Override
public GraphQLInputType toGraphQLInputType(AnnotatedType javaType, Set<Class<? extends TypeMapper>> mappersToSkip, TypeMappingEnvironment env) {
    return env.operationMapper.toGraphQLInputType(getSubstituteType(javaType), env);
}
 
Example #29
Source File: OperationMapper.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
public GraphQLInputType toGraphQLInputType(AnnotatedType javaType, TypeMappingEnvironment env) {
    return toGraphQLInputType(javaType, new HashSet<>(), env);
}
 
Example #30
Source File: DataFetcherResultMapper.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@Override
public GraphQLInputType toGraphQLInputType(AnnotatedType javaType, Set<Class<? extends TypeMapper>> mappersToSkip, TypeMappingEnvironment env) {
    throw new UnsupportedOperationException(DataFetcherResult.class.getSimpleName() + " can not be used as an input type");
}