/** * This helps you transform the current GraphQLObjectType into another one by starting a builder with all * the current values and allows you to transform it how you want. * * @param builderConsumer the consumer code that will be given a builder to transform * * @return a new object based on calling build on that builder */ public GraphQLObjectType transform(Consumer<Builder> builderConsumer) { Builder builder = newObject(this); builderConsumer.accept(builder); return builder.build(); }
GraphQLObjectType objectType = builder.build();
.name("filter") .type(filterType()))) .build();
@Override public GraphQLObjectType createType(GQLTypeCache cache) { return newObject() .name(ACCOUNT_GROUP_MAPPING) .field(GraphqlUtils.idField()) .field(GraphqlUtils.stringField("name", "Name of the mapping")) .field(GraphqlUtils.stringField("type", "Type of the mapping")) .field(f -> f.name("group") .description("Associated group") .type(new GraphQLTypeReference(GQLTypeAccountGroup.ACCOUNT_GROUP))) .build(); }
private GraphQLList buildEdgesObject(String relationName, GraphQLOutputType entityType) { return new GraphQLList(newObject() .name("__edges__" + relationName) .field(newFieldDefinition() .name("node") .dataFetcher(dataFetcher) .type(entityType)) .build()); }
.dataFetcher(environment -> ((GraphIndex.StopAndDistance) environment.getSource()).distance) .build()) .build(); .dataFetcher(environment -> ((StopTimesInPattern) environment.getSource()).times) .build()) .build(); .dataFetcher(environment -> ((StopCluster) environment.getSource()).children) .build()) .build(); .collect(Collectors.toList())) .build()) .build(); .dataFetcher(environment -> ((TripTimeShort) environment.getSource()).headsign) .build()) .build(); .get((Trip) environment.getSource()).geometry.getCoordinateSequence()) .build()) .build(); environment -> (float) ((Coordinate) environment.getSource()).x) .build()) .build(); ((TripPattern) environment.getSource()).semanticHashString(null))
public GraphQLFieldDefinition mutation(String name, String fieldName, List<GraphQLInputObjectField> inputFields, List<GraphQLFieldDefinition> outputFields, DataFetcher dataFetcher) { GraphQLInputObjectType inputObjectType = newInputObject() .name(name + "Input") .fields(inputFields) .build(); GraphQLObjectType outputType = newObject() .name(name + "Payload") .fields(outputFields) .build(); return newFieldDefinition() .name(fieldName) .type(outputType) .argument(newArgument() .name("input") .type(nonNull(inputObjectType))) .dataFetcher(dataFetcher) .build(); }
void dataFetching() { DataFetcher<Foo> fooDataFetcher = new DataFetcher<Foo>() { @Override public Foo get(DataFetchingEnvironment environment) { // environment.getSource() is the value of the surrounding // object. In this case described by objectType Foo value = perhapsFromDatabase(); // Perhaps getting from a DB or whatever return value; } }; GraphQLObjectType objectType = newObject() .name("ObjectType") .field(newFieldDefinition() .name("foo") .type(GraphQLString) .dataFetcher(fooDataFetcher)) .build(); }
@Test public void helloWorldTest() { GraphQLObjectType queryType = newObject() .name("helloWorldQuery") .field(newFieldDefinition() .type(GraphQLString) .name("hello") .staticValue("world")) .build(); GraphQLSchema schema = GraphQLSchema.newSchema() .query(queryType) .build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); Map<String, Object> result = graphQL.execute("{hello}").getData(); assertEquals("world", result.get("hello")); } }
public static void main(String[] args) { GraphQLObjectType queryType = newObject() .name("helloWorldQuery") .field(newFieldDefinition() .type(GraphQLString) .name("hello") .staticValue("world")) .build(); GraphQLSchema schema = GraphQLSchema.newSchema() .query(queryType) .build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); Map<String, Object> result = graphQL.execute("{hello}").getData(); System.out.println(result); // Prints: {hello=world} }
public GraphQLObjectType connectionType(String name, GraphQLObjectType edgeType, List<GraphQLFieldDefinition> connectionFields) { return newObject() .name(name + "Connection") .description("A connection to a list of items.") .field(newFieldDefinition() .name("edges") .description("a list of edges") .type(list(edgeType))) .field(newFieldDefinition() .name("pageInfo") .description("details about this specific page") .type(nonNull(pageInfoType))) .fields(connectionFields) .build(); }
public GraphQLObjectType edgeType(String name, GraphQLOutputType nodeType, GraphQLInterfaceType nodeInterface, List<GraphQLFieldDefinition> edgeFields) { return newObject() .name(name + "Edge") .description("An edge in a connection") .field(newFieldDefinition() .name("node") .type(nodeType) .description("The item at the end of the edge")) .field(newFieldDefinition() .name("cursor") .type(nonNull(GraphQLString)) .description("cursor marks a unique position or index into the connection")) .fields(edgeFields) .build(); }
public Builder subscription(GraphQLObjectType.Builder builder) { return subscription(builder.build()); }
public Builder mutation(GraphQLObjectType.Builder builder) { return mutation(builder.build()); }
public Builder query(GraphQLObjectType.Builder builder) { return query(builder.build()); }
void newType() { GraphQLObjectType simpsonCharacter = newObject() .name("SimpsonCharacter") .description("A Simpson character") .field(newFieldDefinition() .name("name") .description("The name of the character.") .type(GraphQLString)) .field(newFieldDefinition() .name("mainCharacter") .description("One of the main Simpson characters?") .type(GraphQLBoolean)) .build(); }
public Builder type(GraphQLObjectType.Builder builder) { return type(builder.build()); }
void recursiveTypes() { GraphQLObjectType person = newObject() .name("Person") .field(newFieldDefinition() .name("friends") .type(GraphQLList.list(GraphQLTypeReference.typeRef("Person")))) .build(); }