Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.gradle
build
*.iml
.idea/
*.ipr
*.iws
18 changes: 18 additions & 0 deletions src/main/java/graphql/servlet/EmptySchemaProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright 2016 Yurii Rashkovskii
*
* 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
*/
package graphql.servlet;

public class EmptySchemaProvider extends GraphQLSchemaProvider {
}
24 changes: 21 additions & 3 deletions src/main/java/graphql/servlet/GraphQLSchemaProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,27 @@
*/
package graphql.servlet;

import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import lombok.Getter;

public interface GraphQLSchemaProvider {
GraphQLSchema getSchema();
GraphQLSchema getReadOnlySchema();
import javax.validation.constraints.NotNull;
import java.util.HashSet;

public abstract class GraphQLSchemaProvider {

// Initialize with empty schema so schema and readOnlySchema are never null
private final GraphQLSchema emptySchema = GraphQLSchema.newSchema().query(GraphQLObjectType.newObject().name("query")).build(new HashSet<>());

@Getter
@NotNull
private GraphQLSchema schema = emptySchema;
@Getter
@NotNull
private GraphQLSchema readOnlySchema = emptySchema;

protected void updateSchema(@NotNull GraphQLSchema schema, @NotNull GraphQLSchema readOnlySchema) {
this.schema = schema;
this.readOnlySchema = readOnlySchema;
}
}
108 changes: 19 additions & 89 deletions src/main/java/graphql/servlet/GraphQLServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import com.google.common.io.CharStreams;
import graphql.*;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLType;
import graphql.validation.ValidationError;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -55,107 +53,41 @@
import java.util.*;
import java.util.stream.Collectors;

import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLObjectType.newObject;
import static graphql.schema.GraphQLSchema.newSchema;

@Slf4j
@Component(property = {"alias=/graphql", "jmx.objectname=graphql.servlet:type=graphql"})
public class GraphQLServlet extends HttpServlet implements Servlet, GraphQLMBean, GraphQLSchemaProvider {

private List<GraphQLQueryProvider> queryProviders = new ArrayList<>();
private List<GraphQLMutationProvider> mutationProviders = new ArrayList<>();
private List<GraphQLTypesProvider> typesProviders = new ArrayList<>();

@Getter
GraphQLSchema schema;
@Getter
GraphQLSchema readOnlySchema;

protected void updateSchema() {
GraphQLObjectType.Builder object = newObject().name("query");

for (GraphQLQueryProvider provider : queryProviders) {
GraphQLObjectType query = provider.getQuery();
object.field(newFieldDefinition().
type(query).
staticValue(provider.context()).
name(provider.getName()).
description(query.getDescription()).
build());
}

Set<GraphQLType> types = new HashSet<>();
for (GraphQLTypesProvider typesProvider : typesProviders) {
types.addAll(typesProvider.getTypes());
}

readOnlySchema = newSchema().query(object.build()).build(types);
public class GraphQLServlet extends HttpServlet implements Servlet, GraphQLMBean {

if (mutationProviders.isEmpty()) {
schema = readOnlySchema;
} else {
GraphQLObjectType.Builder mutationObject = newObject().name("mutation");
private GraphQLSchemaProvider schemaProvider = new EmptySchemaProvider();

for (GraphQLMutationProvider provider : mutationProviders) {
provider.getMutations().forEach(mutationObject::field);
}

GraphQLObjectType mutationType = mutationObject.build();
if (mutationType.getFieldDefinitions().size() == 0) {
schema = readOnlySchema;
} else {
schema = newSchema().query(object.build()).mutation(mutationType).build();
}
}
public GraphQLSchema getSchema() {
return schemaProvider.getSchema();
}

public GraphQLServlet() {
updateSchema();
public GraphQLSchema getReadOnlySchema() {
return schemaProvider.getReadOnlySchema();
}

@Reference(cardinality = ReferenceCardinality.MULTIPLE, policyOption = ReferencePolicyOption.GREEDY)
public void bindQueryProvider(GraphQLQueryProvider queryProvider) {
queryProviders.add(queryProvider);
updateSchema();
@Reference(cardinality = ReferenceCardinality.MANDATORY, policyOption = ReferencePolicyOption.GREEDY)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct?

public void setSchemaProvider(GraphQLSchemaProvider schemaProvider) {
this.schemaProvider = schemaProvider;
}
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();
public void unsetSchemaProvider(GraphQLSchemaProvider schemaProvider) {
this.schemaProvider = new EmptySchemaProvider();
}

@Override
public String[] getQueries() {
return schema.getQueryType().getFieldDefinitions().stream().map(GraphQLFieldDefinition::getName).toArray(String[]::new);
return getSchema().getQueryType().getFieldDefinitions().stream().map(GraphQLFieldDefinition::getName).toArray(String[]::new);
}

@Override
public String[] getMutations() {
return schema.getMutationType().getFieldDefinitions().stream().map(GraphQLFieldDefinition::getName).toArray(String[]::new);
return getSchema().getMutationType().getFieldDefinitions().stream().map(GraphQLFieldDefinition::getName).toArray(String[]::new);
}

private GraphQLContextBuilder contextBuilder = new DefaultGraphQLContextBuilder();
private ExecutionStrategyProvider executionStrategyProvider = new EnhancedExecutionStrategyProvider();
private List<GraphQLOperationListener> operationListeners = new ArrayList<>();

@Reference(cardinality = ReferenceCardinality.OPTIONAL, policyOption = ReferencePolicyOption.GREEDY)
public void setContextProvider(GraphQLContextBuilder contextBuilder) {
Expand All @@ -177,8 +109,6 @@ protected GraphQLContext createContext(Optional<HttpServletRequest> req, Optiona
return contextBuilder.build(req, resp);
}

private List<GraphQLOperationListener> operationListeners = new ArrayList<>();

@Reference(cardinality = ReferenceCardinality.MULTIPLE, policyOption = ReferencePolicyOption.GREEDY)
public void bindOperationListener(GraphQLOperationListener listener) {
operationListeners.add(listener);
Expand All @@ -191,7 +121,7 @@ public void unbindOperationListener(GraphQLOperationListener listener) {
@Override @SneakyThrows
public String executeQuery(String query) {
try {
ExecutionResult result = new GraphQL(schema).execute(query, createContext(Optional.empty(), Optional.empty()), new HashMap<>());
ExecutionResult result = new GraphQL(getSchema()).execute(query, createContext(Optional.empty(), Optional.empty()), new HashMap<>());
if (result.getErrors().isEmpty()) {
return new ObjectMapper().writeValueAsString(result.getData());
} else {
Expand Down Expand Up @@ -235,10 +165,10 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
path = req.getServletPath();
}
if (path.contentEquals("/schema.json")) {
query(CharStreams.toString(new InputStreamReader(GraphQLServlet.class.getResourceAsStream("introspectionQuery"))), null, new HashMap<>(), schema, req, resp, context);
query(CharStreams.toString(new InputStreamReader(GraphQLServlet.class.getResourceAsStream("introspectionQuery"))), null, new HashMap<>(), getSchema(), req, resp, context);
} else {
if (req.getParameter("q") != null) {
query(req.getParameter("q"), null, new HashMap<>(), readOnlySchema, req, resp, context);
query(req.getParameter("q"), null, new HashMap<>(), getReadOnlySchema(), req, resp, context);
} else if (req.getParameter("query") != null) {
Map<String,Object> variables = new HashMap<>();
if (req.getParameter("variables") != null) {
Expand All @@ -248,7 +178,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
if (req.getParameter("operationName") != null) {
operationName = req.getParameter("operationName");
}
query(req.getParameter("query"), operationName, variables, readOnlySchema, req, resp, context);
query(req.getParameter("query"), operationName, variables, getReadOnlySchema(), req, resp, context);
}
}
}
Expand Down Expand Up @@ -283,7 +213,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
if (variables == null) {
variables = new HashMap<>();
}
query(request.query, request.operationName, variables, schema, req, resp, context);
query(request.query, request.operationName, variables, getSchema(), req, resp, context);
}

private void query(String query, String operationName, Map<String, Object> variables, GraphQLSchema schema, HttpServletRequest req, HttpServletResponse resp, GraphQLContext context) throws IOException {
Expand Down
110 changes: 110 additions & 0 deletions src/main/java/graphql/servlet/OsgiSchemaProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Copyright 2016 Yurii Rashkovskii
*
* 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
*/
package graphql.servlet;

import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLType;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicyOption;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class OsgiSchemaProvider extends GraphQLSchemaProvider implements Serializable {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I need any OSGI annotations here?

private List<GraphQLQueryProvider> queryProviders = new ArrayList<GraphQLQueryProvider>();
private List<GraphQLMutationProvider> mutationProviders = new ArrayList<GraphQLMutationProvider>();
private List<GraphQLTypesProvider> typesProviders = new ArrayList<GraphQLTypesProvider>();

private void updateSchema() {
GraphQLObjectType.Builder object = GraphQLObjectType.newObject().name("query");

for (GraphQLQueryProvider provider : queryProviders) {
GraphQLObjectType query = provider.getQuery();
object.field(GraphQLFieldDefinition.newFieldDefinition().
type(query).
staticValue(provider.context()).
name(provider.getName()).
description(query.getDescription()).
build());
}

Set<GraphQLType> types = new HashSet<GraphQLType>();
for (GraphQLTypesProvider typesProvider : typesProviders) {
types.addAll(typesProvider.getTypes());
}

GraphQLSchema readOnlySchema = GraphQLSchema.newSchema().query(object.build()).build(types);
GraphQLSchema schema;

if (mutationProviders.isEmpty()) {
schema = readOnlySchema;
} else {
GraphQLObjectType.Builder mutationObject = GraphQLObjectType.newObject().name("mutation");

for (GraphQLMutationProvider provider : mutationProviders) {
provider.getMutations().forEach(mutationObject::field);
}

GraphQLObjectType mutationType = mutationObject.build();
if (mutationType.getFieldDefinitions().size() == 0) {
schema = readOnlySchema;
} else {
updateSchema(readOnlySchema, readOnlySchema);
schema = GraphQLSchema.newSchema().query(object.build()).mutation(mutationType).build();
}
}

super.updateSchema(schema, readOnlySchema);
}

@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();
}
}
23 changes: 23 additions & 0 deletions src/main/java/graphql/servlet/SimpleSchemaProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright 2016 Yurii Rashkovskii
*
* 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
*/
package graphql.servlet;

import graphql.schema.GraphQLSchema;

public class SimpleSchemaProvider extends GraphQLSchemaProvider {
public SimpleSchemaProvider(GraphQLSchema schema, GraphQLSchema readOnlySchema) {
super.updateSchema(schema, readOnlySchema);
}
}
Loading