Skip to content

Commit 398cfe3

Browse files
committed
graphql-java#273 - merged master and now accounts for mutation strategy. Also updated the readme
1 parent d1dd1bd commit 398cfe3

6 files changed

Lines changed: 39 additions & 26 deletions

File tree

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ This is the famous "hello world" in graphql-java:
7474
```java
7575
import graphql.schema.GraphQLObjectType;
7676
import graphql.schema.GraphQLSchema;
77+
import java.util.Map;
7778

7879
import static graphql.Scalars.GraphQLString;
7980
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
@@ -82,20 +83,21 @@ import static graphql.schema.GraphQLObjectType.newObject;
8283
public class HelloWorld {
8384

8485
public static void main(String[] args) {
85-
8686
GraphQLObjectType queryType = newObject()
87-
.name("helloWorldQuery")
88-
.field(newFieldDefinition()
89-
.type(GraphQLString)
90-
.name("hello")
91-
.staticValue("world"))
92-
.build();
93-
87+
.name("helloWorldQuery")
88+
.field(newFieldDefinition()
89+
.type(GraphQLString)
90+
.name("hello")
91+
.staticValue("world"))
92+
.build();
93+
9494
GraphQLSchema schema = GraphQLSchema.newSchema()
95-
.query(queryType)
96-
.build();
97-
Map<String, Object> result = new GraphQL(schema).execute("{hello}").getData();
95+
.query(queryType)
96+
.build();
97+
98+
GraphQL graphQL = GraphQL.newObject(schema).build();
9899

100+
Map<String, Object> result = graphQL.execute("{hello}").getData();
99101
System.out.println(result);
100102
// Prints: {hello=world}
101103
}

src/main/java/graphql/GraphQL.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public GraphQL(GraphQLSchema graphQLSchema) {
4646
/**
4747
* A GraphQL object ready to execute queries
4848
*
49-
* @param graphQLSchema the schema to use
50-
* @param queryStrategy the execution strategy to use
49+
* @param graphQLSchema the schema to use
50+
* @param queryStrategy the query execution strategy to use
5151
*
5252
* @deprecated use the {@link #newObject(GraphQLSchema)} builder instead. This will be removed in a future version.
5353
*/
@@ -59,11 +59,13 @@ public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy) {
5959
/**
6060
* A GraphQL object ready to execute queries
6161
*
62-
* @param graphQLSchema the schema to use
63-
* @param queryStrategy the execution strategy to use
62+
* @param graphQLSchema the schema to use
63+
* @param queryStrategy the query execution strategy to use
64+
* @param mutationStrategy the mutation execution strategy to use
6465
*
6566
* @deprecated use the {@link #newObject(GraphQLSchema)} builder instead. This will be removed in a future version.
6667
*/
68+
@SuppressWarnings("DeprecatedIsStillUsed")
6769
public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy, ExecutionStrategy mutationStrategy) {
6870
this.graphQLSchema = graphQLSchema;
6971
this.queryStrategy = queryStrategy;
@@ -84,7 +86,8 @@ public static Builder newObject(GraphQLSchema graphQLSchema) {
8486

8587
public static class Builder {
8688
private GraphQLSchema graphQLSchema;
87-
private ExecutionStrategy executionStrategy = new SimpleExecutionStrategy();
89+
private ExecutionStrategy queryExecutionStrategy = new SimpleExecutionStrategy();
90+
private ExecutionStrategy mutationExecutionStrategy = null;
8891

8992
public Builder(GraphQLSchema graphQLSchema) {
9093
this.graphQLSchema = graphQLSchema;
@@ -95,14 +98,19 @@ public Builder schema(GraphQLSchema graphQLSchema) {
9598
return this;
9699
}
97100

98-
public Builder executionStrategy(ExecutionStrategy executionStrategy) {
99-
this.executionStrategy = assertNotNull(executionStrategy, "ExecutionStrategy must be non null");
101+
public Builder queryExecutionStrategy(ExecutionStrategy executionStrategy) {
102+
this.queryExecutionStrategy = assertNotNull(executionStrategy, "Query ExecutionStrategy must be non null");
103+
return this;
104+
}
105+
106+
public Builder mutationExecutionStrategy(ExecutionStrategy executionStrategy) {
107+
this.mutationExecutionStrategy = assertNotNull(executionStrategy, "Mutation ExecutionStrategy must be non null");
100108
return this;
101109
}
102110

103111
public GraphQL build() {
104112
//noinspection deprecation
105-
return new GraphQL(graphQLSchema, executionStrategy);
113+
return new GraphQL(graphQLSchema, queryExecutionStrategy, mutationExecutionStrategy);
106114
}
107115
}
108116

src/test/groovy/graphql/HelloWorld.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ public static void main(String[] args) {
2626
GraphQLSchema schema = GraphQLSchema.newSchema()
2727
.query(queryType)
2828
.build();
29+
2930
GraphQL graphQL = GraphQL.newObject(schema).build();
30-
Map<String, Object> result = (Map<String, Object>) graphQL.execute("{hello}").getData();
31+
32+
Map<String, Object> result = graphQL.execute("{hello}").getData();
3133
System.out.println(result);
34+
// Prints: {hello=world}
3235
}
3336

3437
@Test
@@ -45,7 +48,7 @@ public void helloWorldTest() {
4548
.query(queryType)
4649
.build();
4750
GraphQL graphQL = GraphQL.newObject(schema).build();
48-
Map<String, Object> result = (Map<String, Object>) graphQL.execute("{hello}").getData();
51+
Map<String, Object> result = graphQL.execute("{hello}").getData();
4952
assertEquals("world", result.get("hello"));
5053
}
5154
}

src/test/groovy/graphql/ScalarsQueryTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class ScalarsQueryTest extends Specification {
7171
def result = GraphQL.newObject(ScalarsQuerySchema.scalarsQuerySchema)
7272
.build().execute(query)
7373
def resultBatched = GraphQL.newObject(ScalarsQuerySchema.scalarsQuerySchema)
74-
.executionStrategy(new BatchedExecutionStrategy())
74+
.queryExecutionStrategy(new BatchedExecutionStrategy())
7575
.build().execute(query)
7676

7777
then:

src/test/groovy/graphql/execution/ExecutorServiceExecutionStrategyTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class ExecutorServiceExecutionStrategyTest extends Specification {
6464
new ThreadPoolExecutor.CallerRunsPolicy());
6565

6666
def graphQL = GraphQL.newObject(StarWarsSchema.starWarsSchema)
67-
.executionStrategy(new ExecutorServiceExecutionStrategy(threadPoolExecutor))
67+
.queryExecutionStrategy(new ExecutorServiceExecutionStrategy(threadPoolExecutor))
6868
.build()
6969
def result = graphQL.execute(query).data
7070

src/test/groovy/graphql/execution/batched/GraphqlExecutionSpec.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ class GraphqlExecutionSpec extends Specification {
2020
private GraphQLSchema schema = new FunWithStringsSchemaFactory().createSchema();
2121

2222
private GraphQL graphQLSimple = GraphQL.newObject(schema)
23-
.executionStrategy(new SimpleExecutionStrategy())
23+
.queryExecutionStrategy(new SimpleExecutionStrategy())
2424
.build()
2525

2626
private GraphQL graphQLBatchedButUnbatched = GraphQL.newObject(this.schema)
27-
.executionStrategy(new BatchedExecutionStrategy())
27+
.queryExecutionStrategy(new BatchedExecutionStrategy())
2828
.build()
2929

3030
private Map<FunWithStringsSchemaFactory.CallType, AtomicInteger> countMap = new HashMap<>();
3131
private GraphQL graphQLBatchedValue = GraphQL.newObject(FunWithStringsSchemaFactory.createBatched(countMap).createSchema())
32-
.executionStrategy(new BatchedExecutionStrategy())
32+
.queryExecutionStrategy(new BatchedExecutionStrategy())
3333
.build()
3434

3535
private Map<String, Object> nullValueMap = new HashMap<>();

0 commit comments

Comments
 (0)