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

graphql.kickstart.execution.GraphQLQueryInvoker Maven / Gradle / Ivy

package graphql.kickstart.execution;

import graphql.execution.instrumentation.ChainedInstrumentation;
import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.SimplePerformantInstrumentation;
import graphql.execution.preparsed.NoOpPreparsedDocumentProvider;
import graphql.execution.preparsed.PreparsedDocumentProvider;
import graphql.kickstart.execution.config.DefaultExecutionStrategyProvider;
import graphql.kickstart.execution.config.ExecutionStrategyProvider;
import graphql.kickstart.execution.config.GraphQLBuilder;
import java.util.List;
import java.util.function.Supplier;

/**
 * @author Andrew Potter
 */
public class GraphQLQueryInvoker {

  private final Supplier getExecutionStrategyProvider;
  private final Supplier getInstrumentation;
  private final Supplier getPreparsedDocumentProvider;

  protected GraphQLQueryInvoker(
      Supplier getExecutionStrategyProvider,
      Supplier getInstrumentation,
      Supplier getPreparsedDocumentProvider) {
    this.getExecutionStrategyProvider = getExecutionStrategyProvider;
    this.getInstrumentation = getInstrumentation;
    this.getPreparsedDocumentProvider = getPreparsedDocumentProvider;
  }

  public static Builder newBuilder() {
    return new Builder();
  }

  public GraphQLInvoker toGraphQLInvoker() {
    GraphQLBuilder graphQLBuilder =
        new GraphQLBuilder()
            .executionStrategyProvider(getExecutionStrategyProvider)
            .instrumentation(getInstrumentation)
            .preparsedDocumentProvider(getPreparsedDocumentProvider);
    return new GraphQLInvoker(graphQLBuilder, new BatchedDataLoaderGraphQLBuilder());
  }

  public static class Builder {

    private Supplier getExecutionStrategyProvider =
        DefaultExecutionStrategyProvider::new;
    private Supplier getInstrumentation =
        () -> SimplePerformantInstrumentation.INSTANCE;
    private Supplier getPreparsedDocumentProvider =
        () -> NoOpPreparsedDocumentProvider.INSTANCE;

    public Builder withExecutionStrategyProvider(ExecutionStrategyProvider provider) {
      return withExecutionStrategyProvider(() -> provider);
    }

    public Builder withExecutionStrategyProvider(Supplier supplier) {
      this.getExecutionStrategyProvider = supplier;
      return this;
    }

    public Builder withInstrumentation(Instrumentation instrumentation) {
      return withInstrumentation(() -> instrumentation);
    }

    public Builder withInstrumentation(Supplier supplier) {
      this.getInstrumentation = supplier;
      return this;
    }

    public Builder with(List instrumentations) {
      if (instrumentations.isEmpty()) {
        return this;
      }
      if (instrumentations.size() == 1) {
        withInstrumentation(instrumentations.get(0));
      } else {
        withInstrumentation(new ChainedInstrumentation(instrumentations));
      }
      return this;
    }

    public Builder withPreparsedDocumentProvider(PreparsedDocumentProvider provider) {
      return withPreparsedDocumentProvider(() -> provider);
    }

    public Builder withPreparsedDocumentProvider(Supplier supplier) {
      this.getPreparsedDocumentProvider = supplier;
      return this;
    }

    public GraphQLQueryInvoker build() {
      return new GraphQLQueryInvoker(
          getExecutionStrategyProvider,
          getInstrumentation,
          getPreparsedDocumentProvider);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy