Skip to content

Commit 80dfda6

Browse files
authored
Omit mutation object when there are no mutations
graphql-js, which is used by babel-relay-plugin, gives the following when the mutation object is empty: "Error: mutation fields must be an object with field names as keys or a function which returns such an object."
1 parent 37445da commit 80dfda6

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/main/java/graphql/servlet/GraphQLServlet.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public class GraphQLServlet extends HttpServlet implements Servlet, GraphQLMBean
6868

6969
protected void updateSchema() {
7070
GraphQLObjectType.Builder object = newObject().name("query");
71-
GraphQLObjectType.Builder mutationObject = newObject().name("mutation");
7271

7372
for (GraphQLQueryProvider provider : queryProviders) {
7473
GraphQLObjectType query = provider.getQuery();
@@ -80,12 +79,19 @@ protected void updateSchema() {
8079
build());
8180
}
8281

83-
for (GraphQLMutationProvider provider : mutationProviders) {
84-
provider.getMutations().forEach(mutationObject::field);
85-
}
86-
8782
readOnlySchema = newSchema().query(object.build()).build();
88-
schema = newSchema().query(object.build()).mutation(mutationObject.build()).build();
83+
84+
if (mutationProviders.isEmpty()) {
85+
schema = readOnlySchema;
86+
} else {
87+
GraphQLObjectType.Builder mutationObject = newObject().name("mutation");
88+
89+
for (GraphQLMutationProvider provider : mutationProviders) {
90+
provider.getMutations().forEach(mutationObject::field);
91+
}
92+
93+
schema = newSchema().query(object.build()).mutation(mutationObject.build()).build();
94+
}
8995
}
9096

9197
public GraphQLServlet() {

0 commit comments

Comments
 (0)