Skip to content

Commit bc160b5

Browse files
committed
Merge branch 'master' into vaant-master
2 parents b197d78 + bad750c commit bc160b5

178 files changed

Lines changed: 8140 additions & 1458 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
.idea
33
.gradle
44
build
5-
_site
5+
classes
6+
_site
7+
generated-src/

README.md

Lines changed: 107 additions & 4 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.3.0` is released.
13+
**Status**: Version `2.4.0` is released.
1414

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

@@ -73,6 +73,7 @@ If you have a question or want to discuss anything else related to this project:
7373
This is the famous "hello world" in graphql-java:
7474

7575
```java
76+
import graphql.GraphQL;
7677
import graphql.schema.GraphQLObjectType;
7778
import graphql.schema.GraphQLSchema;
7879
import java.util.Map;
@@ -121,7 +122,7 @@ Dependency:
121122

122123
```groovy
123124
dependencies {
124-
compile 'com.graphql-java:graphql-java:2.3.0'
125+
compile 'com.graphql-java:graphql-java:2.4.0'
125126
}
126127
127128
```
@@ -134,7 +135,7 @@ Dependency:
134135
<dependency>
135136
<groupId>com.graphql-java</groupId>
136137
<artifactId>graphql-java</artifactId>
137-
<version>2.3.0</version>
138+
<version>2.4.0</version>
138139
</dependency>
139140

140141
```
@@ -342,6 +343,105 @@ Example: [GraphQL Test](src/test/groovy/graphql/GraphQLTest.groovy)
342343
More complex examples: [StarWars query tests](src/test/groovy/graphql/StarWarsQueryTest.groovy)
343344

344345

346+
#### Causing mutation during execution
347+
348+
A good starting point to learn more about mutating data in graphql is [http://graphql.org/learn/queries/#mutations](http://graphql.org/learn/queries/#mutations)
349+
350+
In essence you need to define a `GraphQLObjectType` that takes arguments as input. Those arguments are what you can use to mutate your data store
351+
via the data fetcher invoked.
352+
353+
The mutation is invoked via a query like :
354+
355+
```graphql
356+
mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
357+
createReview(episode: $ep, review: $review) {
358+
stars
359+
commentary
360+
}
361+
}
362+
```
363+
364+
You need to send in arguments during that mutation operation, in this case for the variables for `$ep` and `$review`
365+
366+
You would create types like this to handle this mutation :
367+
368+
```java
369+
GraphQLInputObjectType episodeType = GraphQLInputObjectType.newInputObject()
370+
.name("Episode")
371+
.field(newInputObjectField()
372+
.name("episodeNumber")
373+
.type(Scalars.GraphQLInt))
374+
.build();
375+
376+
GraphQLInputObjectType reviewInputType = GraphQLInputObjectType.newInputObject()
377+
.name("ReviewInput")
378+
.field(newInputObjectField()
379+
.name("stars")
380+
.type(Scalars.GraphQLString)
381+
.name("commentary")
382+
.type(Scalars.GraphQLString))
383+
.build();
384+
385+
GraphQLObjectType reviewType = newObject()
386+
.name("Review")
387+
.field(newFieldDefinition()
388+
.name("stars")
389+
.type(GraphQLString))
390+
.field(newFieldDefinition()
391+
.name("commentary")
392+
.type(GraphQLString))
393+
.build();
394+
395+
GraphQLObjectType createReviewForEpisodeMutation = newObject()
396+
.name("CreateReviewForEpisodeMutation")
397+
.field(newFieldDefinition()
398+
.name("createReview")
399+
.type(reviewType)
400+
.argument(newArgument()
401+
.name("episode")
402+
.type(episodeType)
403+
)
404+
.argument(newArgument()
405+
.name("review")
406+
.type(reviewInputType)
407+
)
408+
.dataFetcher(mutationDataFetcher())
409+
)
410+
.build();
411+
412+
GraphQLSchema schema = GraphQLSchema.newSchema()
413+
.query(queryType)
414+
.mutation(createReviewForEpisodeMutation)
415+
.build();
416+
417+
```
418+
419+
Notice that the input arguments are of type `GraphQLInputObjectType`. This is important. Input arguments can ONLY be of that type
420+
and you cannot use output types such as `GraphQLObjectType`. Scalars types are consider both input and output types.
421+
422+
The data fetcher here is responsible for executing the mutation and returning some sensible output values.
423+
424+
```java
425+
private DataFetcher mutationDataFetcher() {
426+
return new DataFetcher() {
427+
@Override
428+
public Review get(DataFetchingEnvironment environment) {
429+
Episode episode = environment.getArgument("episode");
430+
ReviewInput review = environment.getArgument("review");
431+
432+
// make a call to your store to mutate your database
433+
Review updatedReview = reviewStore().update(episode, review);
434+
435+
// this returns a new view of the data
436+
return updatedReview;
437+
}
438+
};
439+
}
440+
```
441+
442+
Notice how it calls a data store to mutate the backing database and then returns a `Review` object that can be used as the output values
443+
to the caller.
444+
345445
#### Execution strategies
346446

347447
All fields in a SelectionSet are executed serially per default.
@@ -526,11 +626,14 @@ Installing in the local Maven repository:
526626

527627
### Details
528628

529-
The implementation is in Java 6, but the tests are in Groovy and [Spock](https://github.com/spockframework/spock).
629+
The implementation is in Java 8, but the tests are in Groovy and [Spock](https://github.com/spockframework/spock).
530630

531631
The query parsing is done with [ANTLR](http://www.antlr.org). The grammar is [here](src/main/antlr/Graphql.g4).
532632

533633
The only runtime dependencies are ANTLR and Slf4J.
634+
635+
This readme shows information on the latest released version of the library. The 'master' branch however contains the
636+
code for the upcoming version. The readme for that upcoming version can be found [here](src/test/groovy/readme/README.next.md)
534637

535638
### Acknowledgment
536639

build.gradle

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ apply plugin: 'maven-publish'
1010
apply plugin: 'antlr'
1111
apply plugin: 'jacoco'
1212

13-
sourceCompatibility = 1.6
14-
targetCompatibility = 1.6
13+
sourceCompatibility = 1.8
14+
targetCompatibility = 1.8
1515
def releaseVersion = System.properties.RELEASE_VERSION
1616
version = releaseVersion ? releaseVersion : new SimpleDateFormat('yyyy-MM-dd\'T\'HH-mm-ss').format(new Date())
1717
group = 'com.graphql-java'
@@ -33,7 +33,7 @@ dependencies {
3333
antlr "org.antlr:antlr4:4.5.1"
3434
testCompile group: 'junit', name: 'junit', version: '4.11'
3535
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
36-
testCompile 'org.codehaus.groovy:groovy-all:2.4.4'
36+
testCompile 'org.codehaus.groovy:groovy-all:2.4.10'
3737
testCompile 'cglib:cglib-nodep:3.1'
3838
testCompile 'org.objenesis:objenesis:2.1'
3939
}
@@ -67,13 +67,6 @@ allprojects {
6767
exclude('**/antlr/**')
6868
}
6969

70-
tasks.withType(JavaCompile) {
71-
doFirst {
72-
if (sourceCompatibility == '1.6' && System.env.JDK6_HOME != null) {
73-
options.bootClasspath = "$System.env.JDK6_HOME/jre/lib/rt.jar"
74-
}
75-
}
76-
}
7770

7871
}
7972

@@ -152,6 +145,6 @@ tasks.withType(PublishToMavenRepository) {
152145

153146

154147
task wrapper(type: Wrapper) {
155-
gradleVersion = '3.2'
148+
gradleVersion = '3.4'
156149
distributionUrl = "http://services.gradle.org/distributions/gradle-${gradleVersion}-all.zip"
157150
}

gradle/wrapper/gradle-wrapper.jar

1.37 KB
Binary file not shown.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Tue Nov 15 12:32:05 CET 2016
1+
#Fri Apr 07 17:23:20 CEST 2017
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=http\://services.gradle.org/distributions/gradle-3.2-all.zip
6+
distributionUrl=http\://services.gradle.org/distributions/gradle-3.4-all.zip

gradlew

Lines changed: 11 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/antlr/Graphql.g4

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,14 @@ Digit : '0'..'9';
210210

211211
StringValue: '"' (~(["\\\n\r\u2028\u2029])|EscapedChar)* '"';
212212
213+
Comment: '#' ~[\n\r\u2028\u2029]* -> channel(2);
214+
215+
Ignored: (Whitespace|Comma|LineTerminator) -> skip;
216+
213217
fragment EscapedChar : '\\' (["\\/bfnrt] | Unicode) ;
214218
fragment Unicode : 'u' Hex Hex Hex Hex ;
215219
fragment Hex : [0-9a-fA-F] ;
216220
217-
Ignored: (Whitespace|Comma|LineTerminator|Comment) -> skip;
218-
219-
fragment Comment: '#' ~[\n\r\u2028\u2029]*;
220-
221221
fragment LineTerminator: [\n\r\u2028\u2029];
222222
223223
fragment Whitespace : [\t\u000b\f\u0020\u00a0];

src/main/java/graphql/ExecutionResultImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public class ExecutionResultImpl implements ExecutionResult {
1010

11-
private final List<GraphQLError> errors = new ArrayList<GraphQLError>();
11+
private final List<GraphQLError> errors = new ArrayList<>();
1212
private Object data;
1313
private Map<Object,Object> extensions = null;
1414

@@ -28,7 +28,7 @@ public ExecutionResultImpl(Object data, List<? extends GraphQLError> errors, Map
2828
}
2929

3030
if (extensions != null && !extensions.isEmpty()) {
31-
this.extensions = new HashMap<Object,Object>(extensions);
31+
this.extensions = new HashMap<>(extensions);
3232
}
3333
}
3434

@@ -53,11 +53,11 @@ public void setData(Object result) {
5353

5454
@Override
5555
public List<GraphQLError> getErrors() {
56-
return new ArrayList<GraphQLError>(errors);
56+
return new ArrayList<>(errors);
5757
}
5858

5959
@Override
6060
public Map<Object, Object> getExtensions() {
61-
return extensions == null ? null : new HashMap<Object,Object>(extensions);
61+
return extensions == null ? null : new HashMap<>(extensions);
6262
}
6363
}

src/main/java/graphql/GraphQL.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ public class GraphQL {
3131

3232
private static final Logger log = LoggerFactory.getLogger(GraphQL.class);
3333

34-
private static final ExecutionIdProvider DEFAULT_EXECUTION_ID_PROVIDER = new ExecutionIdProvider() {
35-
@Override
36-
public ExecutionId provide(String query, String operationName, Object context) {
37-
return ExecutionId.generate();
38-
}
39-
};
34+
private static final ExecutionIdProvider DEFAULT_EXECUTION_ID_PROVIDER = (query, operationName, context) -> ExecutionId.generate();
4035

4136
private final GraphQLSchema graphQLSchema;
4237
private final ExecutionStrategy queryStrategy;
@@ -175,11 +170,11 @@ public ExecutionResult execute(String requestString) {
175170
}
176171

177172
public ExecutionResult execute(String requestString, Object context) {
178-
return execute(requestString, context, Collections.<String, Object>emptyMap());
173+
return execute(requestString, context, Collections.emptyMap());
179174
}
180175

181176
public ExecutionResult execute(String requestString, String operationName, Object context) {
182-
return execute(requestString, operationName, context, Collections.<String, Object>emptyMap());
177+
return execute(requestString, operationName, context, Collections.emptyMap());
183178
}
184179

185180
public ExecutionResult execute(String requestString, Object context, Map<String, Object> arguments) {

src/main/java/graphql/InvalidSyntaxError.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public class InvalidSyntaxError implements GraphQLError {
1010

11-
private final List<SourceLocation> sourceLocations = new ArrayList<SourceLocation>();
11+
private final List<SourceLocation> sourceLocations = new ArrayList<>();
1212

1313
public InvalidSyntaxError(SourceLocation sourceLocation) {
1414
if (sourceLocation != null)

0 commit comments

Comments
 (0)