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

graphql.kickstart.execution.config.GraphQLBuilder Maven / Gradle / Ivy

The newest version!
package graphql.kickstart.execution.config;

import graphql.GraphQL;
import graphql.execution.ExecutionStrategy;
import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.SimplePerformantInstrumentation;
import graphql.execution.preparsed.NoOpPreparsedDocumentProvider;
import graphql.execution.preparsed.PreparsedDocumentProvider;
import graphql.schema.GraphQLSchema;
import java.util.function.Supplier;
import lombok.Getter;

public class GraphQLBuilder {

  private Supplier executionStrategyProviderSupplier =
      DefaultExecutionStrategyProvider::new;
  private Supplier preparsedDocumentProviderSupplier =
      () -> NoOpPreparsedDocumentProvider.INSTANCE;

  @Getter
  private Supplier instrumentationSupplier =
      () -> SimplePerformantInstrumentation.INSTANCE;

  private Supplier graphQLBuilderConfigurerSupplier = () -> builder -> {};

  public GraphQLBuilder executionStrategyProvider(Supplier supplier) {
    if (supplier != null) {
      executionStrategyProviderSupplier = supplier;
    }
    return this;
  }

  public GraphQLBuilder preparsedDocumentProvider(Supplier supplier) {
    if (supplier != null) {
      preparsedDocumentProviderSupplier = supplier;
    }
    return this;
  }

  public GraphQLBuilder instrumentation(Supplier supplier) {
    if (supplier != null) {
      instrumentationSupplier = supplier;
    }
    return this;
  }

  public GraphQLBuilder graphQLBuilderConfigurer(Supplier supplier) {
    if (supplier != null) {
      graphQLBuilderConfigurerSupplier = supplier;
    }
    return this;
  }

  public GraphQL build(GraphQLSchemaProvider schemaProvider) {
    return build(schemaProvider.getSchema());
  }

  public GraphQL build(GraphQLSchema schema) {
    return build(schema, instrumentationSupplier);
  }

  public GraphQL build(
      GraphQLSchema schema, Supplier configuredInstrumentationSupplier) {
    ExecutionStrategyProvider executionStrategyProvider = executionStrategyProviderSupplier.get();
    ExecutionStrategy queryExecutionStrategy =
        executionStrategyProvider.getQueryExecutionStrategy();
    ExecutionStrategy mutationExecutionStrategy =
        executionStrategyProvider.getMutationExecutionStrategy();
    ExecutionStrategy subscriptionExecutionStrategy =
        executionStrategyProvider.getSubscriptionExecutionStrategy();

    GraphQL.Builder builder =
        GraphQL.newGraphQL(schema)
            .preparsedDocumentProvider(preparsedDocumentProviderSupplier.get());

    if (queryExecutionStrategy != null) {
      builder.queryExecutionStrategy(queryExecutionStrategy);
    }

    if (mutationExecutionStrategy != null) {
      builder.mutationExecutionStrategy(mutationExecutionStrategy);
    }

    if (subscriptionExecutionStrategy != null) {
      builder.subscriptionExecutionStrategy(subscriptionExecutionStrategy);
    }

    Instrumentation instrumentation = configuredInstrumentationSupplier.get();
    builder.instrumentation(instrumentation);
    graphQLBuilderConfigurerSupplier.get().configure(builder);
    return builder.build();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy