use of graphql.schema.GraphQLTypeReference 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.GraphQLTypeReference in project graphql-java by graphql-java.
the class SchemaGenerator method buildUnionType.
private GraphQLUnionType buildUnionType(BuildContext buildCtx, UnionTypeDefinition typeDefinition) {
GraphQLUnionType.Builder builder = GraphQLUnionType.newUnionType();
builder.definition(typeDefinition);
builder.name(typeDefinition.getName());
builder.description(schemaGeneratorHelper.buildDescription(typeDefinition, typeDefinition.getDescription()));
builder.typeResolver(getTypeResolverForUnion(buildCtx, typeDefinition));
List<UnionTypeExtensionDefinition> extensions = unionTypeExtensions(typeDefinition, buildCtx);
typeDefinition.getMemberTypes().forEach(mt -> {
GraphQLOutputType outputType = buildOutputType(buildCtx, mt);
if (outputType instanceof GraphQLTypeReference) {
builder.possibleType((GraphQLTypeReference) outputType);
} else {
builder.possibleType((GraphQLObjectType) outputType);
}
});
builder.withDirectives(buildDirectives(typeDefinition.getDirectives(), directivesOf(extensions), UNION));
extensions.forEach(extension -> extension.getMemberTypes().forEach(mt -> {
GraphQLOutputType outputType = buildOutputType(buildCtx, mt);
if (!builder.containType(outputType.getName())) {
if (outputType instanceof GraphQLTypeReference) {
builder.possibleType((GraphQLTypeReference) outputType);
} else {
builder.possibleType((GraphQLObjectType) outputType);
}
}
}));
return builder.build();
}
use of graphql.schema.GraphQLTypeReference in project admin-console-beta by connexta.
the class GraphQLTransformOutput method fieldToGraphQLObjectType.
public GraphQLOutputType fieldToGraphQLObjectType(ObjectField field) {
// Field provider names should be unique and looks pretty without "Payload" added to the name
String typeName = field instanceof FieldProvider ? field.getFieldType() : createOutputObjectFieldTypeName(field.getFieldType());
// Check if the objectField is recursive, if so bail early
if (referenceTypeProvider.isTypePresent(typeName)) {
return referenceTypeProvider.getType(typeName);
} else {
// Add a GraphQLTypeReference to support recursion
referenceTypeProvider.addType(typeName, new GraphQLTypeReference(typeName));
}
List<GraphQLFieldDefinition> innerFields = fieldsToGraphQLFieldDefinition(field.getFields());
// Skip mutations on field provider
if (field instanceof FieldProvider) {
innerFields.addAll(functionsToGraphQLFieldDefinition(((FieldProvider) field).getDiscoveryFunctions()));
}
return GraphQLObjectType.newObject().name(typeName).description(field.getDescription()).fields(innerFields).build();
}
Aggregations