Skip to content

Commit a98b58a

Browse files
sergehuberapottere
authored andcommitted
Root type name changes & query provider API changes (#21)
* Fix OSGi service registration. * Breaking changes: - Renaming the root query and mutation types from "query" and "mutation" to "Query" and "Mutation" to follow object type naming conventions - Modified the GraphQLQueryProvider implementation to make the old API deprecated and replace with a collection of field definitions, making this aligned with the GraphQLMutationProvider interface. This makes it possible to define data fetcher for the root object type fields. - Added JavaDoc documentation to the GraphQLQueryProvider interface * Improve migration documentation * Make new method optional as this breaks automated tests. * - Remove all deprecated methods - Modify tests to use new field definition method * Rename the method, for consistency with the mutation provider
1 parent 8a32afc commit a98b58a

File tree

4 files changed

+45
-36
lines changed

4 files changed

+45
-36
lines changed

src/main/java/graphql/servlet/GraphQLQueryProvider.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@
1414
*/
1515
package graphql.servlet;
1616

17-
import graphql.schema.GraphQLObjectType;
17+
import graphql.schema.GraphQLFieldDefinition;
1818

19+
import java.util.Collection;
20+
21+
/**
22+
* This interface is used by OSGi bundles to plugin new field into the root query type
23+
*/
1924
public interface GraphQLQueryProvider {
20-
GraphQLObjectType getQuery();
21-
Object context();
22-
default String getName() {
23-
return getQuery().getName();
24-
}
25+
26+
/**
27+
* @return a collection of field definitions that will be added to the root query type.
28+
*/
29+
Collection<GraphQLFieldDefinition> getQueries();
30+
2531
}

src/main/java/graphql/servlet/OsgiGraphQLServlet.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package graphql.servlet;
1616

1717
import graphql.execution.ExecutionStrategy;
18+
import graphql.schema.GraphQLFieldDefinition;
1819
import graphql.schema.GraphQLObjectType;
1920
import graphql.schema.GraphQLSchema;
2021
import graphql.schema.GraphQLType;
@@ -33,7 +34,6 @@
3334
import java.util.Optional;
3435
import java.util.Set;
3536

36-
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
3737
import static graphql.schema.GraphQLObjectType.newObject;
3838
import static graphql.schema.GraphQLSchema.newSchema;
3939

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

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

5757
for (GraphQLQueryProvider provider : queryProviders) {
58-
GraphQLObjectType query = provider.getQuery();
59-
object.field(newFieldDefinition().
60-
type(query).
61-
staticValue(provider.context()).
62-
name(provider.getName()).
63-
description(query.getDescription()).
64-
build());
58+
if (provider.getQueries() != null && provider.getQueries().size() > 0) {
59+
for (GraphQLFieldDefinition graphQLFieldDefinition : provider.getQueries()) {
60+
object.field(graphQLFieldDefinition);
61+
}
62+
}
6563
}
6664

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

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

src/test/groovy/graphql/servlet/GraphQLVariablesSpec.groovy

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,29 @@ package graphql.servlet
1717
import graphql.annotations.GraphQLAnnotations
1818
import graphql.annotations.GraphQLField
1919
import graphql.annotations.GraphQLName
20+
import graphql.schema.GraphQLFieldDefinition
2021
import graphql.schema.GraphQLObjectType
2122
import graphql.schema.GraphQLSchema
2223
import lombok.SneakyThrows
2324
import spock.lang.Specification
2425

26+
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition
27+
2528
class GraphQLVariablesSpec extends Specification {
2629

2730
static class ComplexQueryProvider implements GraphQLQueryProvider {
2831

32+
@Override
33+
Collection<GraphQLFieldDefinition> getQueries() {
34+
List<GraphQLFieldDefinition> fieldDefinitions = new ArrayList<>();
35+
fieldDefinitions.add(newFieldDefinition()
36+
.name("data")
37+
.type(GraphQLAnnotations.object(DataQuery.class))
38+
.staticValue(new DataQuery())
39+
.build());
40+
return fieldDefinitions;
41+
}
42+
2943
static class Data {
3044
@GraphQLField
3145
String field1
@@ -48,16 +62,6 @@ class GraphQLVariablesSpec extends Specification {
4862
}
4963
}
5064

51-
@Override
52-
@SneakyThrows
53-
GraphQLObjectType getQuery() {
54-
return GraphQLAnnotations.object(DataQuery.class)
55-
}
56-
57-
@Override
58-
Object context() {
59-
return new DataQuery()
60-
}
6165
}
6266

6367
GraphQLSchema schema

src/test/groovy/graphql/servlet/OsgiGraphQLServletSpec.groovy

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,23 @@ class OsgiGraphQLServletSpec extends Specification {
2929

3030
static class TestQueryProvider implements GraphQLQueryProvider {
3131

32+
@Override
33+
Collection<GraphQLFieldDefinition> getQueries() {
34+
List<GraphQLFieldDefinition> fieldDefinitions = new ArrayList<>();
35+
fieldDefinitions.add(newFieldDefinition()
36+
.name("query")
37+
.type(GraphQLAnnotations.object(Query.class))
38+
.staticValue(new Query())
39+
.build());
40+
return fieldDefinitions;
41+
}
42+
3243
@GraphQLName("query")
3344
static class Query {
3445
@GraphQLField
3546
public String field;
3647
}
3748

38-
@Override
39-
@SneakyThrows
40-
GraphQLObjectType getQuery() {
41-
return GraphQLAnnotations.object(Query.class);
42-
}
43-
44-
@Override
45-
Object context() {
46-
return new Query();
47-
}
4849
}
4950

5051
def "query provider adds query objects"() {

0 commit comments

Comments
 (0)