Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,105 @@ Example: [GraphQL Test](src/test/groovy/graphql/GraphQLTest.groovy)
More complex examples: [StarWars query tests](src/test/groovy/graphql/StarWarsQueryTest.groovy)


#### Causing mutation during execution

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)

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
via the data fetcher invoked.

The mutation is invoked via a query like :

```graphql
mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
createReview(episode: $ep, review: $review) {
stars
commentary
}
}
```

You need to send in arguments during that mutation operation, in this case for the variables for `$ep` and `$review`

You would create types like this to handle this mutation :

```java
GraphQLInputObjectType episodeType = GraphQLInputObjectType.newInputObject()
.name("Episode")
.field(newInputObjectField()
.name("episodeNumber")
.type(Scalars.GraphQLInt))
.build();

GraphQLInputObjectType reviewInputType = GraphQLInputObjectType.newInputObject()
.name("ReviewInput")
.field(newInputObjectField()
.name("stars")
.type(Scalars.GraphQLString)
.name("commentary")
.type(Scalars.GraphQLString))
.build();

GraphQLObjectType reviewType = newObject()
.name("Review")
.field(newFieldDefinition()
.name("stars")
.type(GraphQLString))
.field(newFieldDefinition()
.name("commentary")
.type(GraphQLString))
.build();

GraphQLObjectType createReviewForEpisodeMutation = newObject()
.name("CreateReviewForEpisodeMutation")
.field(newFieldDefinition()
.name("createReview")
.type(reviewType)
.argument(newArgument()
.name("episode")
.type(episodeType)
)
.argument(newArgument()
.name("review")
.type(reviewInputType)
)
.dataFetcher(mutationDataFetcher())
)
.build();

GraphQLSchema schema = GraphQLSchema.newSchema()
.query(queryType)
.mutation(createReviewForEpisodeMutation)
.build();

```

Notice that the input arguments are of type `GraphQLInputObjectType`. This is important. Input arguments can ONLY be of that type
and you cannot use output types such as `GraphQLObjectType`. Scalars types are consider both input and output types.

The data fetcher here is responsible for executing the mutation and returning some sensible output values.

```java
private DataFetcher mutationDataFetcher() {
return new DataFetcher() {
@Override
public Review get(DataFetchingEnvironment environment) {
Episode episode = environment.getArgument("episode");
ReviewInput review = environment.getArgument("review");

// make a call to your store to mutate your database
Review updatedReview = reviewStore().update(episode, review);

// this returns a new view of the data
return updatedReview;
}
};
}
```

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
to the caller.

#### Execution strategies

All fields in a SelectionSet are executed serially per default.
Expand Down
103 changes: 102 additions & 1 deletion src/test/groovy/readme/ReadmeExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import graphql.GarfieldSchema;
import graphql.GraphQL;
import graphql.StarWarsData;
import graphql.Scalars;
import graphql.StarWarsSchema;
import graphql.execution.ExecutorServiceExecutionStrategy;
import graphql.execution.SimpleExecutionStrategy;
Expand All @@ -27,6 +28,8 @@
import graphql.schema.idl.TypeDefinitionRegistry;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand All @@ -53,9 +56,14 @@
* This class holds readme examples so they stay correct and can be compiled. If this
* does not compile, chances are the readme examples are now wrong.
*/
@SuppressWarnings({"unused", "Convert2Lambda", "ConstantConditions", "Anonymous2MethodRef", "UnnecessaryLocalVariable"})
@SuppressWarnings({"unused", "Convert2Lambda", "UnnecessaryLocalVariable"})
public class ReadmeExamples {


public Map<String, Object> getInputFromJSON() {
return new HashMap<>();
}

class Foo {
}

Expand Down Expand Up @@ -191,6 +199,99 @@ public Foo get(DataFetchingEnvironment environment) {

}

class Review {

}

class Episode {

}

class ReviewInput {

}

class ReviewStore {
Review update(Episode episode, ReviewInput reviewInput) {
return null;
}
}

private ReviewStore reviewStore() {
return new ReviewStore();
}

void mutationExample() {

GraphQLInputObjectType episodeType = GraphQLInputObjectType.newInputObject()
.name("Episode")
.field(newInputObjectField()
.name("episodeNumber")
.type(Scalars.GraphQLInt))
.build();

GraphQLInputObjectType reviewInputType = GraphQLInputObjectType.newInputObject()
.name("ReviewInput")
.field(newInputObjectField()
.name("stars")
.type(Scalars.GraphQLString)
.name("commentary")
.type(Scalars.GraphQLString))
.build();

GraphQLObjectType reviewType = newObject()
.name("Review")
.field(newFieldDefinition()
.name("stars")
.type(GraphQLString))
.field(newFieldDefinition()
.name("commentary")
.type(GraphQLString))
.build();

GraphQLObjectType createReviewForEpisodeMutation = newObject()
.name("CreateReviewForEpisodeMutation")
.field(newFieldDefinition()
.name("createReview")
.type(reviewType)
.argument(newArgument()
.name("episode")
.type(episodeType)
)
.argument(newArgument()
.name("review")
.type(reviewInputType)
)
.dataFetcher(mutationDataFetcher())
)
.build();

GraphQLSchema schema = GraphQLSchema.newSchema()
.query(queryType)
.mutation(createReviewForEpisodeMutation)
.build();
}

private DataFetcher mutationDataFetcher() {
return new DataFetcher() {
@Override
public Review get(DataFetchingEnvironment environment) {
Episode episode = environment.getArgument("episode");
ReviewInput review = environment.getArgument("review");

// make a call to your store to mutate your database
Review updatedReview = reviewStore().update(episode, review);

// this returns a new view of the data
return updatedReview;
}
};
}

private Object context() {
return null;
}

private Foo perhapsFromDatabase() {
return new Foo();
}
Expand Down