@@ -10,7 +10,7 @@ This is a GraphQL Java implementation based on the [specification](https://githu
1010and 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
1515The 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:
7373This is the famous "hello world" in graphql-java:
7474
7575``` java
76+ import graphql.GraphQL ;
7677import graphql.schema.GraphQLObjectType ;
7778import graphql.schema.GraphQLSchema ;
7879import java.util.Map ;
@@ -121,7 +122,7 @@ Dependency:
121122
122123``` groovy
123124dependencies {
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)
342343More 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
347447All 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
531631The query parsing is done with [ANTLR ](http: // www.antlr.org). The grammar is [here](src/main/antlr/Graphql.g4).
532632
533633The 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
0 commit comments