Skip to content
Merged
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
18 changes: 12 additions & 6 deletions src/main/java/graphql/servlet/GraphQLQueryProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@
*/
package graphql.servlet;

import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLFieldDefinition;

import java.util.Collection;

/**
* This interface is used by OSGi bundles to plugin new field into the root query type
*/
public interface GraphQLQueryProvider {
GraphQLObjectType getQuery();
Object context();
default String getName() {
return getQuery().getName();
}

/**
* @return a collection of field definitions that will be added to the root query type.
*/
Collection<GraphQLFieldDefinition> getQueries();

}
18 changes: 8 additions & 10 deletions src/main/java/graphql/servlet/OsgiGraphQLServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package graphql.servlet;

import graphql.execution.ExecutionStrategy;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLType;
Expand All @@ -33,7 +34,6 @@
import java.util.Optional;
import java.util.Set;

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

Expand All @@ -52,16 +52,14 @@ public class OsgiGraphQLServlet extends GraphQLServlet {
private GraphQLSchema readOnlySchema;

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

for (GraphQLQueryProvider provider : queryProviders) {
GraphQLObjectType query = provider.getQuery();
object.field(newFieldDefinition().
type(query).
staticValue(provider.context()).
name(provider.getName()).
description(query.getDescription()).
build());
if (provider.getQueries() != null && provider.getQueries().size() > 0) {
for (GraphQLFieldDefinition graphQLFieldDefinition : provider.getQueries()) {
object.field(graphQLFieldDefinition);
}
}
}

Set<GraphQLType> types = new HashSet<>();
Expand All @@ -74,7 +72,7 @@ protected void updateSchema() {
if (mutationProviders.isEmpty()) {
schema = readOnlySchema;
} else {
GraphQLObjectType.Builder mutationObject = newObject().name("mutation");
GraphQLObjectType.Builder mutationObject = newObject().name("Mutation").description("Root mutation type");

for (GraphQLMutationProvider provider : mutationProviders) {
provider.getMutations().forEach(mutationObject::field);
Expand Down
24 changes: 14 additions & 10 deletions src/test/groovy/graphql/servlet/GraphQLVariablesSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,29 @@ package graphql.servlet
import graphql.annotations.GraphQLAnnotations
import graphql.annotations.GraphQLField
import graphql.annotations.GraphQLName
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLSchema
import lombok.SneakyThrows
import spock.lang.Specification

import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition

class GraphQLVariablesSpec extends Specification {

static class ComplexQueryProvider implements GraphQLQueryProvider {

@Override
Collection<GraphQLFieldDefinition> getQueries() {
List<GraphQLFieldDefinition> fieldDefinitions = new ArrayList<>();
fieldDefinitions.add(newFieldDefinition()
.name("data")
.type(GraphQLAnnotations.object(DataQuery.class))
.staticValue(new DataQuery())
.build());
return fieldDefinitions;
}

static class Data {
@GraphQLField
String field1
Expand All @@ -48,16 +62,6 @@ class GraphQLVariablesSpec extends Specification {
}
}

@Override
@SneakyThrows
GraphQLObjectType getQuery() {
return GraphQLAnnotations.object(DataQuery.class)
}

@Override
Object context() {
return new DataQuery()
}
}

GraphQLSchema schema
Expand Down
21 changes: 11 additions & 10 deletions src/test/groovy/graphql/servlet/OsgiGraphQLServletSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,23 @@ class OsgiGraphQLServletSpec extends Specification {

static class TestQueryProvider implements GraphQLQueryProvider {

@Override
Collection<GraphQLFieldDefinition> getQueries() {
List<GraphQLFieldDefinition> fieldDefinitions = new ArrayList<>();
fieldDefinitions.add(newFieldDefinition()
.name("query")
.type(GraphQLAnnotations.object(Query.class))
.staticValue(new Query())
.build());
return fieldDefinitions;
}

@GraphQLName("query")
static class Query {
@GraphQLField
public String field;
}

@Override
@SneakyThrows
GraphQLObjectType getQuery() {
return GraphQLAnnotations.object(Query.class);
}

@Override
Object context() {
return new Query();
}
}

def "query provider adds query objects"() {
Expand Down