Skip to content

Commit d1dd1bd

Browse files
committed
merged master
2 parents d4097e9 + a339c65 commit d1dd1bd

40 files changed

Lines changed: 842 additions & 115 deletions

‎README.md‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This is a GraphQL Java implementation based on the [specification](https://githu
1010
and the JavaScript [reference implementation](https://github.com/graphql/graphql-js).
1111

1212

13-
**Status**: Version `2.2.0` is released.
13+
**Status**: Version `2.3.0` is released.
1414

1515
The versioning follows [Semantic Versioning](http://semver.org) since `2.0.0`.
1616

@@ -94,9 +94,7 @@ public class HelloWorld {
9494
GraphQLSchema schema = GraphQLSchema.newSchema()
9595
.query(queryType)
9696
.build();
97-
98-
GraphQL graphQL = GraphQL.newObject(schema).build();
99-
Map<String, Object> result = (Map<String, Object>) graphQL.execute("{hello}").getData();
97+
Map<String, Object> result = new GraphQL(schema).execute("{hello}").getData();
10098

10199
System.out.println(result);
102100
// Prints: {hello=world}
@@ -120,7 +118,7 @@ Dependency:
120118

121119
```groovy
122120
dependencies {
123-
compile 'com.graphql-java:graphql-java:2.2.0'
121+
compile 'com.graphql-java:graphql-java:2.3.0'
124122
}
125123
126124
```
@@ -133,7 +131,7 @@ Dependency:
133131
<dependency>
134132
<groupId>com.graphql-java</groupId>
135133
<artifactId>graphql-java</artifactId>
136-
<version>2.2.0</version>
134+
<version>2.3.0</version>
137135
</dependency>
138136

139137
```
@@ -307,22 +305,22 @@ the property name of the source Object, no `DataFetcher` is needed.
307305
Example of configuring a custom `DataFetcher`:
308306
```java
309307

310-
DataFetcher calculateComplicatedValue = new DataFetcher() {
308+
DataFetcher<Foo> fooDataFetcher = new DataFetcher<Foo>() {
311309
@Override
312-
Object get(DataFetchingEnvironment environment) {
310+
Foo get(DataFetchingEnvironment environment) {
313311
// environment.getSource() is the value of the surrounding
314312
// object. In this case described by objectType
315-
Object value = ... // Perhaps getting from a DB or whatever
313+
Foo value = ... // Perhaps getting from a DB or whatever
316314
return value;
317315
}
318316
};
319317

320318
GraphQLObjectType objectType = newObject()
321319
.name("ObjectType")
322320
.field(newFieldDefinition()
323-
.name("someComplicatedValue")
321+
.name("foo")
324322
.type(GraphQLString)
325-
.dataFetcher(calculateComplicatedValue))
323+
.dataFetcher(fooDataFetcher))
326324
.build();
327325

328326
```

‎src/main/java/graphql/ExceptionWhileDataFetching.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,19 @@ public String toString() {
3939
"exception=" + exception +
4040
'}';
4141
}
42+
43+
@Override
44+
public boolean equals(Object o) {
45+
if (this == o) return true;
46+
if (o == null || getClass() != o.getClass()) return false;
47+
48+
ExceptionWhileDataFetching that = (ExceptionWhileDataFetching) o;
49+
50+
return Helper.equals(this, that);
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
return Helper.hashCode(this);
56+
}
4257
}

‎src/main/java/graphql/ExecutionResult.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33

44
import java.util.List;
5+
import java.util.Map;
56

67
public interface ExecutionResult {
78

8-
Object getData();
9+
<T> T getData();
910

1011
List<GraphQLError> getErrors();
12+
13+
Map<Object,Object> getExtensions();
14+
1115
}

‎src/main/java/graphql/ExecutionResultImpl.java‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,49 @@
22

33

44
import java.util.ArrayList;
5+
import java.util.HashMap;
56
import java.util.List;
7+
import java.util.Map;
68

79
public class ExecutionResultImpl implements ExecutionResult {
810

911
private final List<GraphQLError> errors = new ArrayList<GraphQLError>();
1012
private Object data;
13+
private Map<Object,Object> extensions = null;
1114

1215
public ExecutionResultImpl(List<? extends GraphQLError> errors) {
13-
this.errors.addAll(errors);
16+
this(null,errors,null);
1417
}
1518

1619
public ExecutionResultImpl(Object data, List<? extends GraphQLError> errors) {
20+
this(data,errors,null);
21+
}
22+
23+
public ExecutionResultImpl(Object data, List<? extends GraphQLError> errors, Map<Object,Object> extensions) {
1724
this.data = data;
1825

19-
if (errors != null) {
26+
if (errors != null && !errors.isEmpty()) {
2027
this.errors.addAll(errors);
2128
}
29+
30+
if (extensions != null && !extensions.isEmpty()) {
31+
this.extensions = new HashMap<Object,Object>(extensions);
32+
}
2233
}
2334

2435
public void addErrors(List<? extends GraphQLError> errors) {
2536
this.errors.addAll(errors);
2637
}
2738

39+
public void addExtensions(Map<Object, Object> extensions) {
40+
this.extensions.putAll(extensions);
41+
}
42+
43+
2844
@Override
29-
public Object getData() {
30-
return data;
45+
public <T> T getData() {
46+
//noinspection unchecked
47+
return (T) data;
3148
}
3249

3350
public void setData(Object result) {
@@ -39,5 +56,8 @@ public List<GraphQLError> getErrors() {
3956
return new ArrayList<GraphQLError>(errors);
4057
}
4158

42-
59+
@Override
60+
public Map<Object, Object> getExtensions() {
61+
return extensions == null ? null : new HashMap<Object,Object>(extensions);
62+
}
4363
}

‎src/main/java/graphql/GraphQL.java‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public class GraphQL {
2525

2626

2727
private final GraphQLSchema graphQLSchema;
28-
private final ExecutionStrategy executionStrategy;
28+
private final ExecutionStrategy queryStrategy;
29+
private final ExecutionStrategy mutationStrategy;
2930

3031
private static final Logger log = LoggerFactory.getLogger(GraphQL.class);
3132

@@ -38,22 +39,35 @@ public class GraphQL {
3839
*/
3940
public GraphQL(GraphQLSchema graphQLSchema) {
4041
//noinspection deprecation
41-
this(graphQLSchema, null);
42+
this(graphQLSchema, null, null);
4243
}
4344

4445

4546
/**
4647
* A GraphQL object ready to execute queries
4748
*
4849
* @param graphQLSchema the schema to use
49-
* @param executionStrategy the execution strategy to use
50+
* @param queryStrategy the execution strategy to use
5051
*
5152
* @deprecated use the {@link #newObject(GraphQLSchema)} builder instead. This will be removed in a future version.
5253
*/
53-
@SuppressWarnings("DeprecatedIsStillUsed")
54-
public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy executionStrategy) {
54+
public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy) {
55+
//noinspection deprecation
56+
this(graphQLSchema, queryStrategy, null);
57+
}
58+
59+
/**
60+
* A GraphQL object ready to execute queries
61+
*
62+
* @param graphQLSchema the schema to use
63+
* @param queryStrategy the execution strategy to use
64+
*
65+
* @deprecated use the {@link #newObject(GraphQLSchema)} builder instead. This will be removed in a future version.
66+
*/
67+
public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy, ExecutionStrategy mutationStrategy) {
5568
this.graphQLSchema = graphQLSchema;
56-
this.executionStrategy = executionStrategy;
69+
this.queryStrategy = queryStrategy;
70+
this.mutationStrategy = mutationStrategy;
5771
}
5872

5973
/**
@@ -127,7 +141,7 @@ public ExecutionResult execute(String requestString, String operationName, Objec
127141
if (validationErrors.size() > 0) {
128142
return new ExecutionResultImpl(validationErrors);
129143
}
130-
Execution execution = new Execution(executionStrategy);
144+
Execution execution = new Execution(queryStrategy, mutationStrategy);
131145
return execution.execute(graphQLSchema, context, document, operationName, arguments);
132146
}
133147

‎src/main/java/graphql/GraphQLError.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,31 @@ public interface GraphQLError {
1313

1414
ErrorType getErrorType();
1515

16+
17+
/**
18+
* This little helper allows GraphQlErrors to implement
19+
* common things (hashcode/ equals) more easily
20+
*/
21+
@SuppressWarnings("SimplifiableIfStatement")
22+
class Helper {
23+
24+
public static int hashCode(GraphQLError dis) {
25+
int result = dis.getMessage() != null ? dis.getMessage().hashCode() : 0;
26+
result = 31 * result + (dis.getLocations() != null ? dis.getLocations().hashCode() : 0);
27+
result = 31 * result + dis.getErrorType().hashCode();
28+
return result;
29+
}
30+
31+
public static boolean equals(GraphQLError dis, GraphQLError dat) {
32+
if (dis == dat) {
33+
return true;
34+
}
35+
if (dis.getMessage() != null ? !dis.getMessage().equals(dat.getMessage()) : dat.getMessage() != null)
36+
return false;
37+
if (dis.getLocations() != null ? !dis.getLocations().equals(dat.getLocations()) : dat.getLocations() != null)
38+
return false;
39+
return dis.getErrorType() == dat.getErrorType();
40+
}
41+
}
42+
1643
}

‎src/main/java/graphql/InvalidSyntaxError.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,18 @@ public String toString() {
4444
'}';
4545
}
4646

47+
@Override
48+
public boolean equals(Object o) {
49+
if (this == o) return true;
50+
if (o == null || getClass() != o.getClass()) return false;
51+
52+
InvalidSyntaxError that = (InvalidSyntaxError) o;
53+
54+
return Helper.equals(this,that);
55+
}
56+
57+
@Override
58+
public int hashCode() {
59+
return Helper.hashCode(this);
60+
}
4761
}

0 commit comments

Comments
 (0)