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();
}
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();
}
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);
}
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;
}
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);
}
Aggregations