/** * Take a field builder in a function definition and apply. Can be used in a jdk8 lambda * e.g.: * <pre> * {@code * field(f -> f.name("fieldName")) * } * </pre> * * @param builderFunction a supplier for the builder impl * * @return this */ public Builder field(UnaryOperator<GraphQLFieldDefinition.Builder> builderFunction) { assertNotNull(builderFunction, "builderFunction can't be null"); GraphQLFieldDefinition.Builder builder = GraphQLFieldDefinition.newFieldDefinition(); builder = builderFunction.apply(builder); return field(builder.build()); }
builder.field(fieldAndParams.fieldDefinition); encounteredFields.add(fieldAndParams); }); FieldDefAndDirectiveParams fieldAndParams = buildField(buildCtx, typeDefinition, fieldDef); if (!builder.hasField(fieldAndParams.fieldDefinition.getName())) { builder.field(fieldAndParams.fieldDefinition); encounteredFields.add(fieldAndParams); objectType = objectType.transform(objBuilder -> objBuilder.field(fieldDefinition));
.field(GraphQLFieldDefinition.newFieldDefinition() .name("value") .type(GraphQLInt)
@Override public GraphQLObjectType createType(GQLTypeCache cache) { return newObject() .name(PROJECT_ROLE) .field(GraphqlUtils.stringField("id", "ID of the role")) .field(GraphqlUtils.nameField("Unique name for the role")) .field(GraphqlUtils.stringField("description", "Description of the role")) .build(); }
/** * Same effect as the field(GraphQLFieldDefinition). Builder.build() is called * from within * * @param builder an un-built/incomplete GraphQLFieldDefinition * * @return this */ public Builder field(GraphQLFieldDefinition.Builder builder) { return field(builder.build()); }
public GraphQLObjectType createType() { GraphQLObjectType.Builder root = newObject(); root.name(PROJECT_REFERENCE_TYPE_NAME); interfaceTypeProvider.addCommonFields(root); // .name root.field(newFieldDefinition().name("name").description("The name of the project").type(GraphQLString)); return root.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(); }
@Override public GraphQLObjectType createType(GQLTypeCache cache) { return newObject() .name(PROPERTY_TYPE) .field(GraphqlUtils.stringField("typeName", "Qualified type name")) .field(GraphqlUtils.stringField("name", "Display type name")) .field(GraphqlUtils.stringField("description", "Short description for the type")) .build(); }
private GraphQLList buildEdgesObject(String relationName, GraphQLOutputType entityType) { return new GraphQLList(newObject() .name("__edges__" + relationName) .field(newFieldDefinition() .name("node") .dataFetcher(dataFetcher) .type(entityType)) .build()); }
.field(GraphQLFieldDefinition.newFieldDefinition() .name("stop") .type(stopType) .dataFetcher(environment -> ((GraphIndex.StopAndDistance) environment.getSource()).stop) .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("distance") .type(Scalars.GraphQLInt) .field(GraphQLFieldDefinition.newFieldDefinition() .name("pattern") .type(patternType) .get(((StopTimesInPattern) environment.getSource()).pattern.id)) .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("stoptimes") .type(new GraphQLList(stoptimeType)) .name("Cluster") .withInterface(nodeInterface) .field(GraphQLFieldDefinition.newFieldDefinition() .name("id") .type(new GraphQLNonNull(Scalars.GraphQLID)) .toGlobalId(clusterType.getName(), ((StopCluster) environment.getSource()).id)) .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("gtfsId")
.field(newFieldDefinition() .name("value") .type(GraphQLString) .dataFetcher(stringObjectValueFetcher)) .field(newFieldDefinition() .name("nonNullValue") .type(nonNull(GraphQLString)) .dataFetcher(stringObjectValueFetcher)) .field(newFieldDefinition() .name("throwException") .type(GraphQLString) .dataFetcher(throwExceptionFetcher)) .field(newFieldDefinition() .name("returnBadList") .type(GraphQLString) .dataFetcher(returnBadListFetcher)) .field(newFieldDefinition() .name("anyIterable") .type(list(GraphQLString)) .dataFetcher(anyIterableFetcher)) .field(newFieldDefinition() .name("shatter") .type(nonNull(list(nonNull(typeRef("StringObject"))))) .dataFetcher(shatterFetcher)) .field(newFieldDefinition() .name("wordsAndLetters") .type(nonNull(list(nonNull(list(nonNull(typeRef("StringObject"))))))) .dataFetcher(wordsAndLettersFetcher))
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(); }
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(); }
void recursiveTypes() { GraphQLObjectType person = newObject() .name("Person") .field(newFieldDefinition() .name("friends") .type(GraphQLList.list(GraphQLTypeReference.typeRef("Person")))) .build(); }
/** * Same effect as the field(GraphQLFieldDefinition). Builder.build() is called * from within * * @param builder an un-built/incomplete GraphQLFieldDefinition * * @return this */ public Builder field(GraphQLFieldDefinition.Builder builder) { return field(builder.build()); }