All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutorContext Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017 IntroPro Ventures Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.introproventures.graphql.jpa.query.schema.impl;

import com.introproventures.graphql.jpa.query.schema.GraphQLExecutionInputFactory;
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutorContext;
import graphql.ExecutionInput;
import graphql.GraphQL;
import graphql.GraphQLContext;
import graphql.execution.ExecutionStrategy;
import graphql.execution.instrumentation.ChainedInstrumentation;
import graphql.execution.instrumentation.Instrumentation;
import graphql.schema.GraphQLCodeRegistry;
import graphql.schema.GraphQLSchema;
import graphql.schema.visibility.GraphqlFieldVisibility;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import org.dataloader.DataLoaderRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GraphQLJpaExecutorContext implements GraphQLExecutorContext {

    private static final Logger logger = LoggerFactory.getLogger(GraphQLJpaExecutorContext.class);

    private final GraphQLSchema graphQLSchema;
    private final GraphQLExecutionInputFactory executionInputFactory;
    private final Supplier graphqlFieldVisibility;
    private final Supplier instrumentation;
    private final Supplier graphqlContext;
    private final Supplier dataLoaderRegistry;
    private final Supplier queryExecutionStrategy;
    private final Supplier mutationExecutionStrategy;
    private final Supplier subscriptionExecutionStrategy;

    private GraphQLJpaExecutorContext(Builder builder) {
        this.graphQLSchema = builder.graphQLSchema;
        this.executionInputFactory = builder.executionInputFactory;
        this.graphqlFieldVisibility = builder.graphqlFieldVisibility;
        this.instrumentation = builder.instrumentation;
        this.graphqlContext = builder.graphqlContext;
        this.dataLoaderRegistry = builder.dataLoaderRegistry;
        this.queryExecutionStrategy = builder.queryExecutionStrategy;
        this.mutationExecutionStrategy = builder.mutationExecutionStrategy;
        this.subscriptionExecutionStrategy = builder.subscriptionExecutionStrategy;
    }

    @Override
    public ExecutionInput.Builder newExecutionInput() {
        DataLoaderRegistry dataLoaderRegistry = newDataLoaderRegistry();
        GraphQLContext context = newGraphQLContext();

        return executionInputFactory.create().dataLoaderRegistry(dataLoaderRegistry).context(context);
    }

    @Override
    public GraphQL.Builder newGraphQL() {
        Instrumentation instrumentation = newIstrumentation();

        return GraphQL
            .newGraphQL(getGraphQLSchema())
            .instrumentation(instrumentation)
            .queryExecutionStrategy(queryExecutionStrategy.get())
            .mutationExecutionStrategy(mutationExecutionStrategy.get())
            .subscriptionExecutionStrategy(subscriptionExecutionStrategy.get());
    }

    public GraphQLContext newGraphQLContext() {
        return graphqlContext.get();
    }

    public DataLoaderRegistry newDataLoaderRegistry() {
        return dataLoaderRegistry.get();
    }

    public Instrumentation newIstrumentation() {
        List list = Arrays.asList(instrumentation.get());

        return new ChainedInstrumentation(list);
    }

    @Override
    public GraphQLSchema getGraphQLSchema() {
        GraphQLCodeRegistry codeRegistry = graphQLSchema
            .getCodeRegistry()
            .transform(builder -> builder.fieldVisibility(graphqlFieldVisibility.get()));

        return graphQLSchema.transform(builder -> builder.codeRegistry(codeRegistry));
    }

    /**
     * Creates builder to build {@link GraphQLJpaExecutorContext}.
     * @return created builder
     */
    public static IGraphQLSchemaStage builder() {
        return new Builder();
    }

    public interface IGraphQLSchemaStage {
        public IBuildStage graphQLSchema(GraphQLSchema graphQLSchema);
    }

    public interface IBuildStage {
        IBuildStage executionInputFactory(GraphQLExecutionInputFactory executionInputFactory);

        IBuildStage graphqlFieldVisibility(Supplier graphqlFieldVisibility);

        IBuildStage instrumentation(Supplier instrumentation);

        IBuildStage graphqlContext(Supplier graphqlContext);

        IBuildStage dataLoaderRegistry(Supplier dataLoaderRegistry);

        IBuildStage queryExecutionStrategy(Supplier queryExecutionStrategy);

        IBuildStage mutationExecutionStrategy(Supplier mutationExecutionStrategy);

        IBuildStage subscriptionExecutionStrategy(Supplier subscriptionExecutionStrategy);

        GraphQLJpaExecutorContext build();
    }

    /**
     * Builder to build {@link GraphQLJpaExecutorContext}.
     */
    public static final class Builder implements IGraphQLSchemaStage, IBuildStage {

        private GraphQLSchema graphQLSchema;
        private GraphQLExecutionInputFactory executionInputFactory;
        private Supplier graphqlFieldVisibility;
        private Supplier instrumentation;
        private Supplier graphqlContext;
        private Supplier dataLoaderRegistry;
        private Supplier queryExecutionStrategy;
        private Supplier mutationExecutionStrategy;
        private Supplier subscriptionExecutionStrategy;

        private Builder() {}

        @Override
        public IBuildStage graphQLSchema(GraphQLSchema graphQLSchema) {
            this.graphQLSchema = graphQLSchema;
            return this;
        }

        @Override
        public IBuildStage executionInputFactory(GraphQLExecutionInputFactory executionInputFactory) {
            this.executionInputFactory = executionInputFactory;
            return this;
        }

        @Override
        public IBuildStage graphqlFieldVisibility(Supplier graphqlFieldVisibility) {
            this.graphqlFieldVisibility = graphqlFieldVisibility;
            return this;
        }

        @Override
        public IBuildStage instrumentation(Supplier instrumentation) {
            this.instrumentation = instrumentation;
            return this;
        }

        @Override
        public IBuildStage graphqlContext(Supplier graphqlContext) {
            this.graphqlContext = graphqlContext;
            return this;
        }

        @Override
        public IBuildStage dataLoaderRegistry(Supplier dataLoaderRegistry) {
            this.dataLoaderRegistry = dataLoaderRegistry;

            return this;
        }

        @Override
        public IBuildStage queryExecutionStrategy(Supplier queryExecutionStrategy) {
            this.queryExecutionStrategy = queryExecutionStrategy;

            return this;
        }

        @Override
        public IBuildStage mutationExecutionStrategy(Supplier mutationExecutionStrategy) {
            this.mutationExecutionStrategy = mutationExecutionStrategy;

            return this;
        }

        @Override
        public IBuildStage subscriptionExecutionStrategy(Supplier subscriptionExecutionStrategy) {
            this.subscriptionExecutionStrategy = subscriptionExecutionStrategy;

            return this;
        }

        @Override
        public GraphQLJpaExecutorContext build() {
            return new GraphQLJpaExecutorContext(this);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy