graphql.servlet.GraphQLInvocationInputFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of graphql-java-servlet Show documentation
Show all versions of graphql-java-servlet Show documentation
relay.js-compatible GraphQL servlet
package graphql.servlet;
import graphql.schema.GraphQLSchema;
import graphql.servlet.internal.GraphQLRequest;
import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.HandshakeRequest;
import java.util.List;
import java.util.function.Supplier;
/**
* @author Andrew Potter
*/
public class GraphQLInvocationInputFactory {
private final Supplier schemaProviderSupplier;
private final Supplier contextBuilderSupplier;
private final Supplier rootObjectBuilderSupplier;
protected GraphQLInvocationInputFactory(Supplier schemaProviderSupplier, Supplier contextBuilderSupplier, Supplier rootObjectBuilderSupplier) {
this.schemaProviderSupplier = schemaProviderSupplier;
this.contextBuilderSupplier = contextBuilderSupplier;
this.rootObjectBuilderSupplier = rootObjectBuilderSupplier;
}
public GraphQLSchemaProvider getSchemaProvider() {
return schemaProviderSupplier.get();
}
public GraphQLSingleInvocationInput create(GraphQLRequest graphQLRequest, HttpServletRequest request) {
return create(graphQLRequest, request, false);
}
public GraphQLBatchedInvocationInput create(List graphQLRequests, HttpServletRequest request) {
return create(graphQLRequests, request, false);
}
public GraphQLSingleInvocationInput createReadOnly(GraphQLRequest graphQLRequest, HttpServletRequest request) {
return create(graphQLRequest, request, true);
}
public GraphQLBatchedInvocationInput createReadOnly(List graphQLRequests, HttpServletRequest request) {
return create(graphQLRequests, request, true);
}
public GraphQLSingleInvocationInput create(GraphQLRequest graphQLRequest) {
return new GraphQLSingleInvocationInput(
graphQLRequest,
schemaProviderSupplier.get().getSchema(),
contextBuilderSupplier.get().build(),
rootObjectBuilderSupplier.get().build()
);
}
private GraphQLSingleInvocationInput create(GraphQLRequest graphQLRequest, HttpServletRequest request, boolean readOnly) {
return new GraphQLSingleInvocationInput(
graphQLRequest,
readOnly ? schemaProviderSupplier.get().getReadOnlySchema(request) : schemaProviderSupplier.get().getSchema(request),
contextBuilderSupplier.get().build(request),
rootObjectBuilderSupplier.get().build(request)
);
}
private GraphQLBatchedInvocationInput create(List graphQLRequests, HttpServletRequest request, boolean readOnly) {
return new GraphQLBatchedInvocationInput(
graphQLRequests,
readOnly ? schemaProviderSupplier.get().getReadOnlySchema(request) : schemaProviderSupplier.get().getSchema(request),
contextBuilderSupplier.get().build(request),
rootObjectBuilderSupplier.get().build(request)
);
}
public GraphQLSingleInvocationInput create(GraphQLRequest graphQLRequest, HandshakeRequest request) {
return new GraphQLSingleInvocationInput(
graphQLRequest,
schemaProviderSupplier.get().getSchema(request),
contextBuilderSupplier.get().build(request),
rootObjectBuilderSupplier.get().build(request)
);
}
public GraphQLBatchedInvocationInput create(List graphQLRequest, HandshakeRequest request) {
return new GraphQLBatchedInvocationInput(
graphQLRequest,
schemaProviderSupplier.get().getSchema(request),
contextBuilderSupplier.get().build(request),
rootObjectBuilderSupplier.get().build(request)
);
}
public static Builder newBuilder(GraphQLSchema schema) {
return new Builder(new DefaultGraphQLSchemaProvider(schema));
}
public static Builder newBuilder(GraphQLSchemaProvider schemaProvider) {
return new Builder(schemaProvider);
}
public static Builder newBuilder(Supplier schemaProviderSupplier) {
return new Builder(schemaProviderSupplier);
}
public static class Builder {
private final Supplier schemaProviderSupplier;
private Supplier contextBuilderSupplier = DefaultGraphQLContextBuilder::new;
private Supplier rootObjectBuilderSupplier = DefaultGraphQLRootObjectBuilder::new;
public Builder(GraphQLSchemaProvider schemaProvider) {
this(() -> schemaProvider);
}
public Builder(Supplier schemaProviderSupplier) {
this.schemaProviderSupplier = schemaProviderSupplier;
}
public Builder withGraphQLContextBuilder(GraphQLContextBuilder contextBuilder) {
return withGraphQLContextBuilder(() -> contextBuilder);
}
public Builder withGraphQLContextBuilder(Supplier contextBuilderSupplier) {
this.contextBuilderSupplier = contextBuilderSupplier;
return this;
}
public Builder withGraphQLRootObjectBuilder(GraphQLRootObjectBuilder rootObjectBuilder) {
return withGraphQLRootObjectBuilder(() -> rootObjectBuilder);
}
public Builder withGraphQLRootObjectBuilder(Supplier rootObjectBuilderSupplier) {
this.rootObjectBuilderSupplier = rootObjectBuilderSupplier;
return this;
}
public GraphQLInvocationInputFactory build() {
return new GraphQLInvocationInputFactory(schemaProviderSupplier, contextBuilderSupplier, rootObjectBuilderSupplier);
}
}
}