.name("stopAtDistance") .field(GraphQLFieldDefinition.newFieldDefinition() .name("stop") .type(stopType) .dataFetcher(environment -> ((GraphIndex.StopAndDistance) environment.getSource()).stop) .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("distance") .type(Scalars.GraphQLInt) .dataFetcher(environment -> ((GraphIndex.StopAndDistance) environment.getSource()).distance) .build()) .build(); .name("StoptimesInPattern") .field(GraphQLFieldDefinition.newFieldDefinition() .name("pattern") .type(patternType) .dataFetcher(environment -> index.patternForId .get(((StopTimesInPattern) environment.getSource()).pattern.id)) .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("stoptimes") .type(new GraphQLList(stoptimeType)) .dataFetcher(environment -> ((StopTimesInPattern) environment.getSource()).times) .build()) .build(); .withInterface(nodeInterface) .field(GraphQLFieldDefinition.newFieldDefinition()
@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")); } }
private FieldDefAndDirectiveParams buildField(BuildContext buildCtx, TypeDefinition parentType, FieldDefinition fieldDef) { buildCtx.enterNode(fieldDef); GraphQLFieldDefinition.Builder builder = GraphQLFieldDefinition.newFieldDefinition(); builder.definition(fieldDef); builder.name(fieldDef.getName()); builder.description(schemaGeneratorHelper.buildDescription(fieldDef, fieldDef.getDescription())); builder.deprecate(schemaGeneratorHelper.buildDeprecationReason(fieldDef.getDirectives())); GraphQLDirective[] directives = buildDirectives(fieldDef.getDirectives(), Collections.emptyList(), DirectiveLocation.FIELD_DEFINITION, buildCtx.getDirectiveDefinitions()); builder.withDirectives( directives ); fieldDef.getInputValueDefinitions().forEach(inputValueDefinition -> builder.argument(buildArgument(buildCtx, inputValueDefinition))); GraphQLOutputType fieldType = buildOutputType(buildCtx, fieldDef.getType()); builder.type(fieldType); GraphQLFieldDefinition fieldDefinition = builder.build(); // if they have already wired in a fetcher - then leave it alone FieldCoordinates coordinates = FieldCoordinates.coordinates(parentType.getName(), fieldDefinition.getName()); if (!buildCtx.codeRegistry.hasDataFetcher(coordinates)) { DataFetcherFactory dataFetcherFactory = buildDataFetcherFactory(buildCtx, parentType, fieldDef, fieldType, Arrays.asList(directives)); buildCtx.getCodeRegistry().dataFetcher(coordinates, dataFetcherFactory); } return buildCtx.exitNode(new FieldDefAndDirectiveParams(fieldDefinition, buildCtx.mkBehaviourParams())); }
.description("Fare for transit") .field(GraphQLFieldDefinition.newFieldDefinition() .name("type") .type(rideTypeEnum) .dataFetcher(environment -> ((Fare) environment.getSource()).type) .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("low") .type(Scalars.GraphQLFloat) .dataFetcher(environment -> ((Fare) environment.getSource()).low) .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("peak") .type(Scalars.GraphQLFloat) .dataFetcher(environment -> ((Fare) environment.getSource()).peak) .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("senior") .type(Scalars.GraphQLFloat) .dataFetcher(environment -> ((Fare) environment.getSource()).senior) .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("transferReduction") .type(Scalars.GraphQLBoolean) .dataFetcher(environment -> ((Fare) environment.getSource()).transferReduction) .build()) .name("currency") .description("In which currency is the fare in ISO 4217 code")
.description("Fare for transit") .field(GraphQLFieldDefinition.newFieldDefinition() .name("type") .type(rideTypeEnum) .dataFetcher(environment -> ((Fare) environment.getSource()).type) .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("low") .type(Scalars.GraphQLFloat) .dataFetcher(environment -> ((Fare) environment.getSource()).low) .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("peak") .type(Scalars.GraphQLFloat) .dataFetcher(environment -> ((Fare) environment.getSource()).peak) .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("senior") .type(Scalars.GraphQLFloat) .dataFetcher(environment -> ((Fare) environment.getSource()).senior) .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("transferReduction") .type(Scalars.GraphQLBoolean) .dataFetcher(environment -> ((Fare) environment.getSource()).transferReduction) .build()) .name("currency") .description("In which currency is the fare in ISO 4217 code")
name = Character.toLowerCase(name.charAt(0)) + name.substring(1); GraphQLName nameAnn = method.getAnnotation(GraphQLName.class); builder.name(toGraphqlName(nameAnn == null ? name : nameAnn.value())); outputType = getGraphQLConnection(isConnection, method, type, outputType, builder); builder.type(outputType); map(t -> newInputObjectField().name(t.getName()).type(t.getType()).description(t.getDescription()).build()). collect(Collectors.toList()), fieldDefinitions, null); builder.argument(relay.getArguments()); builder.type(relay.getType()); } else { builder.argument(args); builder.description(description.value()); builder.deprecate(deprecate.value()); builder.deprecate("Deprecated"); builder.dataFetcher(actualDataFetcher); return new GraphQLFieldDefinitionWrapper(builder.build());
GraphQLFieldDefinition.Builder rootViewField = GraphQLFieldDefinition.newFieldDefinition().name(graphQLController.rootQueriesObjectName()).staticValue(queryHandler); rootViewField.type(viewerObject.build()); rootViewFieldsBuilder.add(rootViewField.build()); GraphQLFieldDefinition.Builder rootMutationField = GraphQLFieldDefinition.newFieldDefinition().name(graphQLController.rootMutationsObjectName()).staticValue(queryHandler); rootMutationField.type(mutObject.build()); rootMutationFieldsBuilder.add(rootMutationField.build());
protected GraphQLFieldDefinition getField(Field field) throws GraphQLAnnotationsException { GraphQLFieldDefinition.Builder builder = newFieldDefinition(); GraphQLName name = field.getAnnotation(GraphQLName.class); builder.name(toGraphqlName(name == null ? field.getName() : name.value())); GraphQLType annotation = field.getAnnotation(GraphQLType.class); outputType = getGraphQLConnection(isConnection, field, type, outputType, builder); builder.type(outputType); builder.description(description.value()); builder.deprecate(deprecate.value()); builder.deprecate("Deprecated"); builder.dataFetcher(actualDataFetcher); return new GraphQLFieldDefinitionWrapper(builder.build());
.name("Root") .field(newFieldDefinition() .name("viewer") .type(userType) .staticValue(theOnlyUser) .build()) .field(relay.nodeField(nodeInterface, todoDataFetcher)) .build();
.name(fieldName) .type(fieldType) .description(fieldDescription) .deprecate(fieldDeprecation); if (fieldArguments != null) { fieldBuilder.argument(fieldArguments); fieldBuilder.dataFetcher((DataFetcher)fieldFetcher); fieldBuilder.staticValue(fieldFetcher);
@Test public void testArrayStartsWith() throws Exception { GraphQLObjectType innerType = newObject() .name("Inner") .field(newStringField("b").staticValue("foo")) .build(); GraphQLSchema schema = newQuerySchema(it -> it .field( newField("a", new GraphQLList(innerType)) .dataFetcher(env -> Flowable .just(asList(true, true, true)) .delay(1, SECONDS, scheduler) .startWith(new ArrayList<Boolean>()) ) ) ); ExecutionResult executionResult = new GraphQL(schema, strategy).execute("{ a { b } }"); Flowable.fromPublisher((Publisher<Change>) executionResult.getData()).timestamp(scheduler).subscribe(subscriber); scheduler.advanceTimeBy(2, SECONDS); subscriber .assertChanges(it -> it.containsExactly( tuple("00:000", "", ImmutableMap.of("a", emptyList())), tuple("01:000", "a", asList(ImmutableMap.of("b", "foo"), ImmutableMap.of("b", "foo"), ImmutableMap.of("b", "foo"))), // FIXME this values are duplicated. For now, let's call it "at least once" delivery :D tuple("01:000", "a[1]", ImmutableMap.of("b", "foo")), tuple("01:000", "a[2]", ImmutableMap.of("b", "foo")) )) .assertComplete(); }
.name(mutationName) .description(methodReturnTypeResolvableTypeAccessor.getDescription()) .deprecate(methodReturnTypeResolvableTypeAccessor.getGraphQLDeprecationReason()) .type(graphQLOutputObjectType) .dataFetcher(new ReflectionGraphQLDataMutator(graphQLSchemaConfig, object, method)); fieldBuilder.argument(newArgument() .name(getGraphQLSchemaConfig().getMutationInputArgumentName()) .type(graphQLInputType) .build()); GraphQLFieldDefinition mutationField = fieldBuilder.build(); addToFieldDefinitionResolverMap(dfsContext, mutationField, methodReturnTypeResolvableTypeAccessor.getGraphQLComplexitySpelExpression()); graphQLFieldDefinitions.add(mutationField);
GraphQLOutputType type = toGraphQLType(operation.getJavaType(), buildContext); GraphQLFieldDefinition.Builder fieldBuilder = newFieldDefinition() .name(operation.getName()) .description(operation.getDescription()) .deprecate(operation.getDeprecationReason()) .type(type) .withDirective(Directives.mappedOperation(operation)) .withDirectives(toGraphQLDirectives(operation.getTypedElement(), buildContext.directiveBuilder::buildFieldDefinitionDirectives, buildContext)); .map(argument -> toGraphQLArgument(argument, buildContext)) .collect(Collectors.toList()); fieldBuilder.argument(arguments); if (GraphQLUtils.isRelayConnectionType(type) && buildContext.relayMappingConfig.strictConnectionSpec) { validateConnectionSpecCompliance(operation.getName(), arguments, buildContext.relay); .map(OperationArgument::getJavaType); ValueMapper valueMapper = buildContext.createValueMapper(inputTypes); fieldBuilder.dataFetcher(createResolver(operation, valueMapper, buildContext.globalEnvironment, buildContext.interceptorFactory)); return buildContext.transformers.transform(fieldBuilder.build(), operation, this, buildContext);
private GraphQLFieldDefinition.Builder processDeprecated(GraphQLFieldDefinition.Builder builder, Optional<Method> method, Optional<Field> field) { GraphQLDeprecated deprecated = null; Deprecated javaDeprecreated = null; if (method.isPresent()) { deprecated = method.get().getAnnotation(GraphQLDeprecated.class); javaDeprecreated = method.get().getAnnotation(Deprecated.class); } if (field.isPresent()) { if (deprecated == null) { deprecated = field.get().getAnnotation(GraphQLDeprecated.class); } if (javaDeprecreated == null) { javaDeprecreated = field.get().getAnnotation(Deprecated.class); } } if (deprecated != null) { builder.deprecate(deprecated.value()); } else if (javaDeprecreated != null) { builder.deprecate(""); } return builder; } private GraphQLFieldDefinition.Builder processDescription(GraphQLFieldDefinition.Builder builder, Optional<Method> method, Optional<Field> field) {
.name("helloWorldQuery") .field(newFieldDefinition() .type(GraphQLString) .name("hello") .staticValue(Observable.just("world")) .build()) .build();
public GraphQLObjectType createGraphQLOutputObjectType(DfsContext dfsContext, String mutationName, ResolvableTypeAccessor resolvableTypeAccessor) { Class<?> cls = resolvableTypeAccessor.resolve(); GraphQLType graphQLObjectType = objectTypeResolverMap.get(cls); if (graphQLObjectType == null) graphQLObjectType = createGraphQLFieldType(dfsContext, resolvableTypeAccessor, true); GraphQLOutputType clientMutationIdType = getGraphQLSchemaConfig().isAllowEmptyClientMutationId() ? GraphQLString : new GraphQLNonNull(GraphQLString); addToMutationReturnTypeResolverMap(dfsContext, mutationName, cls, (GraphQLOutputType) graphQLObjectType); GraphQLFieldDefinition graphQLFieldDefinition = newFieldDefinition() .name(resolvableTypeAccessor.getGraphQLOutName()) .deprecate(resolvableTypeAccessor.getGraphQLDeprecationReason()) .description(resolvableTypeAccessor.getDescription()) .type((GraphQLOutputType) graphQLObjectType) .build(); addToFieldDefinitionResolverMap(dfsContext, graphQLFieldDefinition, resolvableTypeAccessor.getGraphQLComplexitySpelExpression()); return newObject() .name(resolvableTypeAccessor.getName() + getGraphQLSchemaConfig().getOutputObjectNamePrefix()) .field(newFieldDefinition() .name(getGraphQLSchemaConfig().getClientMutationIdName()) .type(clientMutationIdType) .build()) .field(graphQLFieldDefinition) .build(); }
public GraphQLFieldDefinition getMethodDefinition(DfsContext dfsContext, Class<?> objectClass, Method method) { GraphQLFieldDefinition graphQLFieldDefinition = null; ResolvableTypeAccessor resolvableTypeAccessor = ResolvableTypeAccessor.forMethodReturnType(method, objectClass); if (resolvableTypeAccessor.isGraphQLIdOrGraphQLField()) { GraphQLOutputType graphQLOutputType = (GraphQLOutputType) createGraphQLFieldType(dfsContext, resolvableTypeAccessor, true); List<GraphQLArgument> graphQLArguments = buildGraphQLArgumentsFromMethodParams(dfsContext, method, objectClass); GraphQLFieldDefinition.Builder builder = GraphQLFieldDefinition .newFieldDefinition() .name(resolvableTypeAccessor.getName()) .deprecate(resolvableTypeAccessor.getGraphQLDeprecationReason()) .argument(graphQLArguments) .type(graphQLOutputType) .description(resolvableTypeAccessor.getDescription()); if (!objectClass.isInterface()) { String beanName = objectClass.getName() + resolvableTypeAccessor.getName(); Object object = getGraphQLSchemaBeanFactory().getBeanByType(objectClass); builder.dataFetcher(new ReflectionGraphQLDataFetcher(getGraphQLSchemaConfig(), object, method)); } graphQLFieldDefinition = builder.build(); addToFieldDefinitionResolverMap(dfsContext, graphQLFieldDefinition, resolvableTypeAccessor.getGraphQLComplexitySpelExpression()); } return graphQLFieldDefinition; }
private FieldDefAndDirectiveParams buildField(BuildContext buildCtx, TypeDefinition parentType, FieldDefinition fieldDef) { buildCtx.enterNode(fieldDef); GraphQLFieldDefinition.Builder builder = GraphQLFieldDefinition.newFieldDefinition(); builder.definition(fieldDef); builder.name(fieldDef.getName()); builder.description(schemaGeneratorHelper.buildDescription(fieldDef, fieldDef.getDescription())); builder.deprecate(schemaGeneratorHelper.buildDeprecationReason(fieldDef.getDirectives())); GraphQLDirective[] directives = buildDirectives(fieldDef.getDirectives(), Collections.emptyList(), DirectiveLocation.FIELD_DEFINITION, buildCtx.getDirectiveDefinitions()); builder.withDirectives( directives ); fieldDef.getInputValueDefinitions().forEach(inputValueDefinition -> builder.argument(buildArgument(buildCtx, inputValueDefinition))); GraphQLOutputType fieldType = buildOutputType(buildCtx, fieldDef.getType()); builder.type(fieldType); GraphQLFieldDefinition fieldDefinition = builder.build(); // if they have already wired in a fetcher - then leave it alone FieldCoordinates coordinates = FieldCoordinates.coordinates(parentType.getName(), fieldDefinition.getName()); if (!buildCtx.codeRegistry.hasDataFetcher(coordinates)) { DataFetcherFactory dataFetcherFactory = buildDataFetcherFactory(buildCtx, parentType, fieldDef, fieldType, Arrays.asList(directives)); buildCtx.getCodeRegistry().dataFetcher(coordinates, dataFetcherFactory); } return buildCtx.exitNode(new FieldDefAndDirectiveParams(fieldDefinition, buildCtx.mkBehaviourParams())); }
public GraphQLType createType(GraphQLContext context) { Map<String, GraphQLObjectType> types = generateMicroschemaFieldType(context); // No microschemas have been found - We need to add a dummy type in order to keep the type system working if (types.isEmpty()) { types.put("dummy", newObject().name("dummy").field(newFieldDefinition().name("dummy").type(GraphQLString).staticValue(null).build()).description("Placeholder dummy microschema type").build()); } GraphQLObjectType[] typeArray = types.values().toArray(new GraphQLObjectType[types.values().size()]); GraphQLUnionType fieldType = newUnionType().name(MICRONODE_TYPE_NAME).possibleTypes(typeArray).description("Fields of the micronode.") .typeResolver(env -> { Object object = env.getObject(); if (object instanceof Micronode) { Micronode fieldContainer = (Micronode) object; MicroschemaContainerVersion micronodeFieldSchema = fieldContainer.getSchemaContainerVersion(); String schemaName = micronodeFieldSchema.getName(); GraphQLObjectType foundType = types.get(schemaName); return foundType; } return null; }).build(); return fieldType; }
public GraphQLFieldDefinition getFieldDefinition(DfsContext dfsContext, Class<?> implClass, Field field) { GraphQLFieldDefinition graphQLFieldDefinition = null; ResolvableTypeAccessor resolvableTypeAccessor = ResolvableTypeAccessor.forField(field, implClass); if (resolvableTypeAccessor.isNotIgnorable()) { GraphQLOutputType graphQLOutputType = (GraphQLOutputType) createGraphQLFieldType(dfsContext, resolvableTypeAccessor, true); GraphQLFieldDefinition.Builder graphQLFieldDefinitionBuilder = GraphQLFieldDefinition.newFieldDefinition() .name(resolvableTypeAccessor.getName()) .type(graphQLOutputType) .deprecate(resolvableTypeAccessor.getGraphQLDeprecationReason()) .description(resolvableTypeAccessor.getDescription()); boolean isConstant = Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers()); if (isConstant) { graphQLFieldDefinitionBuilder.staticValue(org.springframework.util.ReflectionUtils.getField(field, null)); } graphQLFieldDefinition = graphQLFieldDefinitionBuilder.build(); addToFieldDefinitionResolverMap(dfsContext, graphQLFieldDefinition, resolvableTypeAccessor.getGraphQLComplexitySpelExpression()); } return graphQLFieldDefinition; }