graphql.servlet.OsgiGraphQLServlet 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.execution.instrumentation.Instrumentation;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLType;
import org.osgi.service.component.annotations.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import static graphql.schema.GraphQLObjectType.newObject;
import static graphql.schema.GraphQLSchema.newSchema;
@Component(
service={javax.servlet.http.HttpServlet.class,javax.servlet.Servlet.class},
property = {"alias=/graphql", "jmx.objectname=graphql.servlet:type=graphql"}
)
public class OsgiGraphQLServlet extends GraphQLServlet {
private final List queryProviders = new ArrayList<>();
private final List mutationProviders = new ArrayList<>();
private final List typesProviders = new ArrayList<>();
private GraphQLContextBuilder contextBuilder = new DefaultGraphQLContextBuilder();
private GraphQLRootObjectBuilder rootObjectBuilder = new DefaultGraphQLRootObjectBuilder();
private ExecutionStrategyProvider executionStrategyProvider = new DefaultExecutionStrategyProvider();
private InstrumentationProvider instrumentationProvider = new NoOpInstrumentationProvider();
private GraphQLErrorHandler errorHandler = new DefaultGraphQLErrorHandler();
private GraphQLSchemaProvider schemaProvider;
protected void updateSchema() {
final GraphQLObjectType.Builder queryTypeBuilder = newObject().name("Query").description("Root query type");
for (GraphQLQueryProvider provider : queryProviders) {
if (provider.getQueries() != null && !provider.getQueries().isEmpty()) {
provider.getQueries().forEach(queryTypeBuilder::field);
}
}
final Set types = new HashSet<>();
for (GraphQLTypesProvider typesProvider : typesProviders) {
types.addAll(typesProvider.getTypes());
}
GraphQLObjectType mutationType = null;
if (!mutationProviders.isEmpty()) {
final GraphQLObjectType.Builder mutationTypeBuilder = newObject().name("Mutation").description("Root mutation type");
for (GraphQLMutationProvider provider : mutationProviders) {
provider.getMutations().forEach(mutationTypeBuilder::field);
}
if (!mutationTypeBuilder.build().getFieldDefinitions().isEmpty()) {
mutationType = mutationTypeBuilder.build();
}
}
this.schemaProvider = new DefaultGraphQLSchemaProvider(newSchema().query(queryTypeBuilder.build()).mutation(mutationType).build(types));
}
public OsgiGraphQLServlet() {
updateSchema();
}
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policyOption = ReferencePolicyOption.GREEDY)
public void bindProvider(GraphQLProvider provider) {
if (provider instanceof GraphQLQueryProvider) {
queryProviders.add((GraphQLQueryProvider) provider);
}
if (provider instanceof GraphQLMutationProvider) {
mutationProviders.add((GraphQLMutationProvider) provider);
}
if (provider instanceof GraphQLTypesProvider) {
typesProviders.add((GraphQLTypesProvider) provider);
}
updateSchema();
}
public void unbindProvider(GraphQLProvider provider) {
if (provider instanceof GraphQLQueryProvider) {
queryProviders.remove(provider);
}
if (provider instanceof GraphQLMutationProvider) {
mutationProviders.remove(provider);
}
if (provider instanceof GraphQLTypesProvider) {
typesProviders.remove(provider);
}
updateSchema();
}
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policyOption = ReferencePolicyOption.GREEDY)
public void bindQueryProvider(GraphQLQueryProvider queryProvider) {
queryProviders.add(queryProvider);
updateSchema();
}
public void unbindQueryProvider(GraphQLQueryProvider queryProvider) {
queryProviders.remove(queryProvider);
updateSchema();
}
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policyOption = ReferencePolicyOption.GREEDY)
public void bindMutationProvider(GraphQLMutationProvider mutationProvider) {
mutationProviders.add(mutationProvider);
updateSchema();
}
public void unbindMutationProvider(GraphQLMutationProvider mutationProvider) {
mutationProviders.remove(mutationProvider);
updateSchema();
}
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policyOption = ReferencePolicyOption.GREEDY)
public void typesProviders(GraphQLTypesProvider typesProvider) {
typesProviders.add(typesProvider);
updateSchema();
}
public void unbindTypesProvider(GraphQLTypesProvider typesProvider) {
typesProviders.remove(typesProvider);
updateSchema();
}
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policyOption = ReferencePolicyOption.GREEDY)
public void bindServletListener(GraphQLServletListener listener) {
this.addListener(listener);
}
public void unbindServletListener(GraphQLServletListener listener) {
this.removeListener(listener);
}
@Reference(cardinality = ReferenceCardinality.OPTIONAL, policyOption = ReferencePolicyOption.GREEDY)
public void setContextProvider(GraphQLContextBuilder contextBuilder) {
this.contextBuilder = contextBuilder;
}
public void unsetContextProvider(GraphQLContextBuilder contextBuilder) {
this.contextBuilder = new DefaultGraphQLContextBuilder();
}
@Reference(cardinality = ReferenceCardinality.OPTIONAL, policyOption = ReferencePolicyOption.GREEDY)
public void setRootObjectBuilder(GraphQLRootObjectBuilder rootObjectBuilder) {
this.rootObjectBuilder = rootObjectBuilder;
}
public void unsetRootObjectBuilder(GraphQLRootObjectBuilder rootObjectBuilder) {
this.rootObjectBuilder = new DefaultGraphQLRootObjectBuilder();
}
@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy= ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
public void setExecutionStrategyProvider(ExecutionStrategyProvider provider) {
executionStrategyProvider = provider;
}
public void unsetExecutionStrategyProvider(ExecutionStrategyProvider provider) {
executionStrategyProvider = new DefaultExecutionStrategyProvider();
}
@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy= ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
public void setInstrumentationProvider(InstrumentationProvider provider) {
instrumentationProvider = provider;
}
public void unsetInstrumentationProvider(InstrumentationProvider provider) {
instrumentationProvider = new NoOpInstrumentationProvider();
}
@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy= ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
public void setErrorHandler(GraphQLErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
public void unsetErrorHandler(GraphQLErrorHandler errorHandler) {
this.errorHandler = new DefaultGraphQLErrorHandler();
}
@Override
protected GraphQLSchemaProvider getSchemaProvider() {
return schemaProvider;
}
protected GraphQLContext createContext(Optional req, Optional resp) {
return contextBuilder.build(req, resp);
}
@Override
protected Object createRootObject(Optional request, Optional response) {
return rootObjectBuilder.build(request, response);
}
@Override
protected ExecutionStrategyProvider getExecutionStrategyProvider() {
return executionStrategyProvider;
}
@Override
protected Instrumentation getInstrumentation() {
return instrumentationProvider.getInstrumentation();
}
@Override
protected Map transformVariables(GraphQLSchema schema, String query, Map variables) {
return new GraphQLVariables(schema, query, variables);
}
@Override
protected GraphQLErrorHandler getGraphQLErrorHandler() {
return errorHandler;
}
}