Search in sources :

Example 1 with GraphQLEnumType

use of graphql.schema.GraphQLEnumType in project graphql-java by graphql-java.

the class FunWithStringsSchemaFactory method createSchema.

GraphQLSchema createSchema() {
    GraphQLObjectType stringObjectType = newObject().name("StringObject").field(newFieldDefinition().name("value").type(GraphQLString).dataFetcher(stringObjectValueFetcher)).field(newFieldDefinition().name("nonNullValue").type(new GraphQLNonNull(GraphQLString)).dataFetcher(stringObjectValueFetcher)).field(newFieldDefinition().name("veryNonNullValue").type(new GraphQLNonNull(new GraphQLNonNull(GraphQLString))).dataFetcher(stringObjectValueFetcher)).field(newFieldDefinition().name("throwException").type(GraphQLString).dataFetcher(throwExceptionFetcher)).field(newFieldDefinition().name("returnBadList").type(GraphQLString).dataFetcher(returnBadListFetcher)).field(newFieldDefinition().name("anyIterable").type(new GraphQLList(GraphQLString)).dataFetcher(anyIterableFetcher)).field(newFieldDefinition().name("shatter").type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(new GraphQLTypeReference("StringObject"))))).dataFetcher(shatterFetcher)).field(newFieldDefinition().name("wordsAndLetters").type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(new GraphQLNonNull(new GraphQLTypeReference("StringObject")))))))).dataFetcher(wordsAndLettersFetcher)).field(newFieldDefinition().name("split").description("String#split(regex) but replace empty strings with nulls to help us test null behavior in lists").type(new GraphQLList(new GraphQLTypeReference("StringObject"))).argument(GraphQLArgument.newArgument().name("regex").type(GraphQLString)).dataFetcher(splitFetcher)).field(newFieldDefinition().name("splitNotNull").description("String#split(regex) but replace empty strings with nulls to help us test null behavior in lists").type(new GraphQLList(new GraphQLNonNull(new GraphQLTypeReference("StringObject")))).argument(GraphQLArgument.newArgument().name("regex").type(GraphQLString)).dataFetcher(splitFetcher)).field(newFieldDefinition().name("append").type(new GraphQLTypeReference("StringObject")).argument(GraphQLArgument.newArgument().name("text").type(GraphQLString)).dataFetcher(appendFetcher)).field(newFieldDefinition().name("emptyOptional").type(GraphQLString).argument(GraphQLArgument.newArgument().name("text").type(GraphQLString)).dataFetcher(emptyOptionalFetcher)).field(newFieldDefinition().name("optional").type(GraphQLString).argument(GraphQLArgument.newArgument().name("text").type(GraphQLString)).dataFetcher(optionalFetcher)).field(newFieldDefinition().name("completableFuture").type(GraphQLString).argument(GraphQLArgument.newArgument().name("text").type(GraphQLString)).dataFetcher(completableFutureFetcher)).build();
    GraphQLEnumType enumDayType = newEnum().name("Day").value("MONDAY").value("TUESDAY").description("Day of the week").build();
    GraphQLObjectType simpleObjectType = newObject().name("SimpleObject").field(newFieldDefinition().name("value").type(GraphQLString)).build();
    GraphQLInterfaceType interfaceType = newInterface().name("InterfaceType").field(newFieldDefinition().name("value").type(GraphQLString)).typeResolver(env -> {
        // always this for testing
        return simpleObjectType;
    }).build();
    GraphQLObjectType queryType = newObject().name("StringQuery").field(newFieldDefinition().name("string").type(stringObjectType).argument(GraphQLArgument.newArgument().name("value").type(GraphQLString)).dataFetcher(env -> env.getArgument("value"))).field(newFieldDefinition().name("interface").type(interfaceType).argument(GraphQLArgument.newArgument().name("value").type(GraphQLString)).dataFetcher(env -> CompletableFuture.completedFuture(new SimpleObject()))).field(newFieldDefinition().name("nullEnum").type(enumDayType).dataFetcher(env -> null)).build();
    return GraphQLSchema.newSchema().query(queryType).build();
}
Also used : GraphQLList(graphql.schema.GraphQLList) GraphQLObjectType(graphql.schema.GraphQLObjectType) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Arrays(java.util.Arrays) GraphQLString(graphql.Scalars.GraphQLString) GraphQLNonNull(graphql.schema.GraphQLNonNull) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) CompletableFuture(java.util.concurrent.CompletableFuture) GraphQLInterfaceType.newInterface(graphql.schema.GraphQLInterfaceType.newInterface) GraphQLArgument(graphql.schema.GraphQLArgument) ArrayList(java.util.ArrayList) GraphQLFieldDefinition.newFieldDefinition(graphql.schema.GraphQLFieldDefinition.newFieldDefinition) List(java.util.List) GraphQLList(graphql.schema.GraphQLList) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GraphQLEnumType.newEnum(graphql.schema.GraphQLEnumType.newEnum) Map(java.util.Map) GraphQLObjectType.newObject(graphql.schema.GraphQLObjectType.newObject) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) Optional(java.util.Optional) GraphQLEnumType(graphql.schema.GraphQLEnumType) ArrayDeque(java.util.ArrayDeque) LinkedHashSet(java.util.LinkedHashSet) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) GraphQLEnumType(graphql.schema.GraphQLEnumType) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLNonNull(graphql.schema.GraphQLNonNull) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType)

Example 2 with GraphQLEnumType

use of graphql.schema.GraphQLEnumType in project graphql-java by graphql-java.

the class SchemaGenerator method buildEnumType.

private GraphQLEnumType buildEnumType(BuildContext buildCtx, EnumTypeDefinition typeDefinition) {
    GraphQLEnumType.Builder builder = GraphQLEnumType.newEnum();
    builder.definition(typeDefinition);
    builder.name(typeDefinition.getName());
    builder.description(schemaGeneratorHelper.buildDescription(typeDefinition, typeDefinition.getDescription()));
    List<EnumTypeExtensionDefinition> extensions = enumTypeExtensions(typeDefinition, buildCtx);
    EnumValuesProvider enumValuesProvider = buildCtx.getWiring().getEnumValuesProviders().get(typeDefinition.getName());
    typeDefinition.getEnumValueDefinitions().forEach(evd -> {
        GraphQLEnumValueDefinition enumValueDefinition = buildEnumValue(buildCtx, typeDefinition, enumValuesProvider, evd);
        builder.value(enumValueDefinition);
    });
    extensions.forEach(extension -> extension.getEnumValueDefinitions().forEach(evd -> {
        GraphQLEnumValueDefinition enumValueDefinition = buildEnumValue(buildCtx, typeDefinition, enumValuesProvider, evd);
        if (!builder.hasValue(enumValueDefinition.getName())) {
            builder.value(enumValueDefinition);
        }
    }));
    builder.withDirectives(buildDirectives(typeDefinition.getDirectives(), directivesOf(extensions), ENUM));
    return builder.build();
}
Also used : Arrays(java.util.Arrays) Value(graphql.language.Value) INPUT_FIELD_DEFINITION(graphql.introspection.Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) InputValueDefinition(graphql.language.InputValueDefinition) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLUnionType(graphql.schema.GraphQLUnionType) SchemaDefinition(graphql.language.SchemaDefinition) GraphQLEnumValueDefinition(graphql.schema.GraphQLEnumValueDefinition) UNION(graphql.introspection.Introspection.DirectiveLocation.UNION) EnumTypeDefinition(graphql.language.EnumTypeDefinition) Type(graphql.language.Type) INPUT_OBJECT(graphql.introspection.Introspection.DirectiveLocation.INPUT_OBJECT) TypeResolverProxy(graphql.schema.TypeResolverProxy) GraphQLEnumValueDefinition.newEnumValueDefinition(graphql.schema.GraphQLEnumValueDefinition.newEnumValueDefinition) DataFetcherFactory(graphql.schema.DataFetcherFactory) EnumValueDefinition(graphql.language.EnumValueDefinition) ObjectTypeExtensionDefinition(graphql.language.ObjectTypeExtensionDefinition) OBJECT(graphql.introspection.Introspection.DirectiveLocation.OBJECT) GraphQLError(graphql.GraphQLError) Map(java.util.Map) TypeName(graphql.language.TypeName) TypeResolver(graphql.schema.TypeResolver) OperationTypeDefinition(graphql.language.OperationTypeDefinition) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLDirective(graphql.schema.GraphQLDirective) NotAnInputTypeError(graphql.schema.idl.errors.NotAnInputTypeError) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) UnionTypeExtensionDefinition(graphql.language.UnionTypeExtensionDefinition) Collections.emptyList(java.util.Collections.emptyList) FieldDefinition(graphql.language.FieldDefinition) GraphQLInputType(graphql.schema.GraphQLInputType) Set(java.util.Set) TypeDefinition(graphql.language.TypeDefinition) GraphQLArgument(graphql.schema.GraphQLArgument) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) NotAnOutputTypeError(graphql.schema.idl.errors.NotAnOutputTypeError) List(java.util.List) Stream(java.util.stream.Stream) ENUM_VALUE(graphql.introspection.Introspection.DirectiveLocation.ENUM_VALUE) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) Optional(java.util.Optional) GraphQLEnumType(graphql.schema.GraphQLEnumType) InterfaceTypeExtensionDefinition(graphql.language.InterfaceTypeExtensionDefinition) DirectiveLocation(graphql.introspection.Introspection.DirectiveLocation) GraphQLScalarType(graphql.schema.GraphQLScalarType) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) SchemaProblem(graphql.schema.idl.errors.SchemaProblem) HashMap(java.util.HashMap) GraphQLType(graphql.schema.GraphQLType) Stack(java.util.Stack) ArrayList(java.util.ArrayList) ENUM(graphql.introspection.Introspection.DirectiveLocation.ENUM) Introspection(graphql.introspection.Introspection) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) UnionTypeDefinition(graphql.language.UnionTypeDefinition) DataFetcherFactories(graphql.schema.DataFetcherFactories) InputObjectTypeExtensionDefinition(graphql.language.InputObjectTypeExtensionDefinition) ScalarTypeExtensionDefinition(graphql.language.ScalarTypeExtensionDefinition) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) EnumTypeExtensionDefinition(graphql.language.EnumTypeExtensionDefinition) GraphQLOutputType(graphql.schema.GraphQLOutputType) Directive(graphql.language.Directive) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) PublicApi(graphql.PublicApi) Assert.assertNotNull(graphql.Assert.assertNotNull) GraphQLTypeReference.typeRef(graphql.schema.GraphQLTypeReference.typeRef) Collections(java.util.Collections) GraphQLEnumType(graphql.schema.GraphQLEnumType) GraphQLEnumValueDefinition(graphql.schema.GraphQLEnumValueDefinition) EnumTypeExtensionDefinition(graphql.language.EnumTypeExtensionDefinition)

Example 3 with GraphQLEnumType

use of graphql.schema.GraphQLEnumType in project graphql-java by graphql-java.

the class ExecutionStrategy method completeValue.

/**
 * Called to complete a value for a field based on the type of the field.
 * <p>
 * If the field is a scalar type, then it will be coerced  and returned.  However if the field type is an complex object type, then
 * the execution strategy will be called recursively again to execute the fields of that type before returning.
 * <p>
 * Graphql fragments mean that for any give logical field can have one or more {@link Field} values associated with it
 * in the query, hence the fieldList.  However the first entry is representative of the field for most purposes.
 *
 * @param executionContext contains the top level execution parameters
 * @param parameters       contains the parameters holding the fields to be executed and source object
 *
 * @return an {@link ExecutionResult}
 *
 * @throws NonNullableFieldWasNullException if a non null field resolves to a null value
 */
protected CompletableFuture<ExecutionResult> completeValue(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
    ExecutionTypeInfo typeInfo = parameters.getTypeInfo();
    Object result = unboxPossibleOptional(parameters.getSource());
    GraphQLType fieldType = typeInfo.getType();
    if (result == null) {
        return completeValueForNull(parameters);
    } else if (fieldType instanceof GraphQLList) {
        return completeValueForList(executionContext, parameters, result);
    } else if (fieldType instanceof GraphQLScalarType) {
        return completeValueForScalar(executionContext, parameters, (GraphQLScalarType) fieldType, result);
    } else if (fieldType instanceof GraphQLEnumType) {
        return completeValueForEnum(executionContext, parameters, (GraphQLEnumType) fieldType, result);
    }
    // 
    // when we are here, we have a complex type: Interface, Union or Object
    // and we must go deeper
    // 
    GraphQLObjectType resolvedObjectType = resolveType(executionContext, parameters, fieldType);
    return completeValueForObject(executionContext, parameters, resolvedObjectType, result);
}
Also used : GraphQLList(graphql.schema.GraphQLList) GraphQLEnumType(graphql.schema.GraphQLEnumType) GraphQLType(graphql.schema.GraphQLType) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLScalarType(graphql.schema.GraphQLScalarType)

Example 4 with GraphQLEnumType

use of graphql.schema.GraphQLEnumType in project graphql-java by graphql-java.

the class SchemaGeneratorHelper method buildValue.

public Object buildValue(Value value, GraphQLType requiredType) {
    Object result = null;
    if (requiredType instanceof GraphQLNonNull) {
        requiredType = ((GraphQLNonNull) requiredType).getWrappedType();
    }
    if (requiredType instanceof GraphQLScalarType) {
        result = parseLiteral(value, (GraphQLScalarType) requiredType);
    } else if (value instanceof EnumValue && requiredType instanceof GraphQLEnumType) {
        result = ((EnumValue) value).getName();
    } else if (value instanceof ArrayValue && requiredType instanceof GraphQLList) {
        ArrayValue arrayValue = (ArrayValue) value;
        GraphQLType wrappedType = ((GraphQLList) requiredType).getWrappedType();
        result = arrayValue.getValues().stream().map(item -> this.buildValue(item, wrappedType)).collect(Collectors.toList());
    } else if (value instanceof ObjectValue && requiredType instanceof GraphQLInputObjectType) {
        result = buildObjectValue((ObjectValue) value, (GraphQLInputObjectType) requiredType);
    } else if (value != null && !(value instanceof NullValue)) {
        Assert.assertShouldNeverHappen("cannot build value of %s from %s", requiredType.getName(), String.valueOf(value));
    }
    return result;
}
Also used : GraphQLList(graphql.schema.GraphQLList) ObjectValue(graphql.language.ObjectValue) NullValue(graphql.language.NullValue) GraphQLEnumType(graphql.schema.GraphQLEnumType) EnumValue(graphql.language.EnumValue) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLNonNull(graphql.schema.GraphQLNonNull) GraphQLType(graphql.schema.GraphQLType) ArrayValue(graphql.language.ArrayValue) GraphQLScalarType(graphql.schema.GraphQLScalarType)

Example 5 with GraphQLEnumType

use of graphql.schema.GraphQLEnumType in project graphql-java by graphql-java.

the class AstValueHelper method astFromValue.

/**
 * Produces a GraphQL Value AST given a Java value.
 *
 * A GraphQL type must be provided, which will be used to interpret different
 * Java values.
 *
 * <pre>
 * |      Value    | GraphQL Value        |
 * | ------------- | -------------------- |
 * | Object        | Input Object         |
 * | Array         | List                 |
 * | Boolean       | Boolean              |
 * | String        | String / Enum Value  |
 * | Number        | Int / Float          |
 * | Mixed         | Enum Value           |
 * </pre>
 *
 * @param value - the java value to be converted into graphql ast
 * @param type  the graphql type of the object
 *
 * @return a grapql language ast {@link Value}
 */
public static Value astFromValue(Object value, GraphQLType type) {
    if (value == null) {
        return null;
    }
    if (type instanceof GraphQLNonNull) {
        return handleNonNull(value, (GraphQLNonNull) type);
    }
    // the value is not an array, convert the value using the list's item type.
    if (type instanceof GraphQLList) {
        return handleList(value, (GraphQLList) type);
    }
    // in the JavaScript object according to the fields in the input type.
    if (type instanceof GraphQLInputObjectType) {
        return handleInputObject(value, (GraphQLInputObjectType) type);
    }
    if (!(type instanceof GraphQLScalarType || type instanceof GraphQLEnumType)) {
        throw new AssertException("Must provide Input Type, cannot use: " + type.getClass());
    }
    // Since value is an internally represented value, it must be serialized
    // to an externally represented value before converting into an AST.
    final Object serialized = serialize(type, value);
    if (isNullish(serialized)) {
        return null;
    }
    // Others serialize based on their corresponding JavaScript scalar types.
    if (serialized instanceof Boolean) {
        return new BooleanValue((Boolean) serialized);
    }
    String stringValue = serialized.toString();
    // numbers can be Int or Float values.
    if (serialized instanceof Number) {
        return handleNumber(stringValue);
    }
    if (serialized instanceof String) {
        // Enum types use Enum literals.
        if (type instanceof GraphQLEnumType) {
            return new EnumValue(stringValue);
        }
        // ID types can use Int literals.
        if (type == Scalars.GraphQLID && stringValue.matches("^[0-9]+$")) {
            return new IntValue(new BigInteger(stringValue));
        }
        // String types are just strings but JSON'ised
        return new StringValue(jsonStringify(stringValue));
    }
    throw new AssertException("'Cannot convert value to AST: " + serialized);
}
Also used : GraphQLList(graphql.schema.GraphQLList) AssertException(graphql.AssertException) GraphQLEnumType(graphql.schema.GraphQLEnumType) GraphQLScalarType(graphql.schema.GraphQLScalarType) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLNonNull(graphql.schema.GraphQLNonNull) BigInteger(java.math.BigInteger)

Aggregations

GraphQLEnumType (graphql.schema.GraphQLEnumType)5 GraphQLList (graphql.schema.GraphQLList)3 GraphQLObjectType (graphql.schema.GraphQLObjectType)3 GraphQLScalarType (graphql.schema.GraphQLScalarType)3 DataFetcher (graphql.schema.DataFetcher)2 GraphQLArgument (graphql.schema.GraphQLArgument)2 GraphQLInputObjectType (graphql.schema.GraphQLInputObjectType)2 GraphQLInterfaceType (graphql.schema.GraphQLInterfaceType)2 GraphQLNonNull (graphql.schema.GraphQLNonNull)2 GraphQLSchema (graphql.schema.GraphQLSchema)2 GraphQLType (graphql.schema.GraphQLType)2 GraphQLTypeReference (graphql.schema.GraphQLTypeReference)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Assert.assertNotNull (graphql.Assert.assertNotNull)1 AssertException (graphql.AssertException)1 GraphQLError (graphql.GraphQLError)1