Search in sources :

Example 11 with GraphQLCodeRegistry

use of graphql.schema.GraphQLCodeRegistry in project graphql-java by graphql-java.

the class ReadmeExamples method dataFetching.

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
            // Perhaps getting from a DB or whatever
            Foo value = perhapsFromDatabase();
            return value;
        }
    };
    GraphQLObjectType objectType = newObject().name("ObjectType").field(newFieldDefinition().name("foo").type(GraphQLString)).build();
    GraphQLCodeRegistry codeRegistry = newCodeRegistry().dataFetcher(coordinates("ObjectType", "foo"), fooDataFetcher).build();
}
Also used : GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry) StaticDataFetcher(graphql.schema.StaticDataFetcher) DataFetcher(graphql.schema.DataFetcher) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment)

Example 12 with GraphQLCodeRegistry

use of graphql.schema.GraphQLCodeRegistry in project graphql-java by graphql-java.

the class ReadmeExamples method mutationExample.

void mutationExample() {
    GraphQLInputObjectType episodeType = newInputObject().name("Episode").field(newInputObjectField().name("episodeNumber").type(Scalars.GraphQLInt)).build();
    GraphQLInputObjectType reviewInputType = newInputObject().name("ReviewInput").field(newInputObjectField().name("stars").type(Scalars.GraphQLString).name("commentary").type(Scalars.GraphQLString)).build();
    GraphQLObjectType reviewType = newObject().name("Review").field(newFieldDefinition().name("stars").type(GraphQLString)).field(newFieldDefinition().name("commentary").type(GraphQLString)).build();
    GraphQLObjectType createReviewForEpisodeMutation = newObject().name("CreateReviewForEpisodeMutation").field(newFieldDefinition().name("createReview").type(reviewType).argument(newArgument().name("episode").type(episodeType)).argument(newArgument().name("review").type(reviewInputType))).build();
    GraphQLCodeRegistry codeRegistry = newCodeRegistry().dataFetcher(coordinates("CreateReviewForEpisodeMutation", "createReview"), mutationDataFetcher()).build();
    GraphQLSchema schema = GraphQLSchema.newSchema().query(queryType).mutation(createReviewForEpisodeMutation).codeRegistry(codeRegistry).build();
}
Also used : GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry) GraphQLSchema(graphql.schema.GraphQLSchema)

Example 13 with GraphQLCodeRegistry

use of graphql.schema.GraphQLCodeRegistry in project graphql-java by graphql-java.

the class SchemaGenerator method makeExecutableSchemaImpl.

private GraphQLSchema makeExecutableSchemaImpl(TypeDefinitionRegistry typeRegistry, RuntimeWiring wiring, Map<String, OperationTypeDefinition> operationTypeDefinitions, Options options) {
    SchemaGeneratorHelper.BuildContext buildCtx = new SchemaGeneratorHelper.BuildContext(typeRegistry, wiring, operationTypeDefinitions, options);
    GraphQLSchema.Builder schemaBuilder = GraphQLSchema.newSchema();
    Set<GraphQLDirective> additionalDirectives = schemaGeneratorHelper.buildAdditionalDirectiveDefinitions(buildCtx);
    schemaBuilder.additionalDirectives(additionalDirectives);
    schemaGeneratorHelper.buildSchemaDirectivesAndExtensions(buildCtx, schemaBuilder);
    schemaGeneratorHelper.buildOperations(buildCtx, schemaBuilder);
    Set<GraphQLType> additionalTypes = schemaGeneratorHelper.buildAdditionalTypes(buildCtx);
    schemaBuilder.additionalTypes(additionalTypes);
    buildCtx.getCodeRegistry().fieldVisibility(buildCtx.getWiring().getFieldVisibility());
    GraphQLCodeRegistry codeRegistry = buildCtx.getCodeRegistry().build();
    schemaBuilder.codeRegistry(codeRegistry);
    buildCtx.getTypeRegistry().schemaDefinition().ifPresent(schemaDefinition -> {
        String description = buildDescription(buildCtx, schemaDefinition, schemaDefinition.getDescription());
        schemaBuilder.description(description);
    });
    GraphQLSchema graphQLSchema = schemaBuilder.build();
    List<SchemaGeneratorPostProcessing> schemaTransformers = new ArrayList<>();
    // schema build traversals
    if (buildCtx.isDirectiveWiringRequired()) {
        // handle directive wiring AFTER the schema has been built and hence type references are resolved at callback time
        schemaTransformers.add(new SchemaDirectiveWiringSchemaGeneratorPostProcessing(buildCtx.getTypeRegistry(), buildCtx.getWiring(), buildCtx.getCodeRegistry()));
    }
    schemaTransformers.addAll(buildCtx.getWiring().getSchemaGeneratorPostProcessings());
    for (SchemaGeneratorPostProcessing postProcessing : schemaTransformers) {
        graphQLSchema = postProcessing.process(graphQLSchema);
    }
    return graphQLSchema;
}
Also used : GraphQLType(graphql.schema.GraphQLType) ArrayList(java.util.ArrayList) GraphQLDirective(graphql.schema.GraphQLDirective) GraphQLSchema(graphql.schema.GraphQLSchema) GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry)

Aggregations

GraphQLCodeRegistry (graphql.schema.GraphQLCodeRegistry)13 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)5 GraphQLObjectType (graphql.schema.GraphQLObjectType)5 GraphQLOutputType (graphql.schema.GraphQLOutputType)4 GraphQLSchema (graphql.schema.GraphQLSchema)4 Map (java.util.Map)4 DataFetcher (graphql.schema.DataFetcher)3 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)3 ExecutionStepInfo.newExecutionStepInfo (graphql.execution.ExecutionStepInfo.newExecutionStepInfo)2 ValuesResolver (graphql.execution.ValuesResolver)2 QueryDirectivesImpl (graphql.execution.directives.QueryDirectivesImpl)2 Argument (graphql.language.Argument)2 ExecutableNormalizedField (graphql.normalized.ExecutableNormalizedField)2 DataFetchingEnvironmentImpl.newDataFetchingEnvironment (graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment)2 DataFetchingFieldSelectionSet (graphql.schema.DataFetchingFieldSelectionSet)2 GraphQLDirective (graphql.schema.GraphQLDirective)2 GraphQLFieldsContainer (graphql.schema.GraphQLFieldsContainer)2 ArrayList (java.util.ArrayList)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ImmutableList (com.google.common.collect.ImmutableList)1