|
| 1 | +Execution |
| 2 | +============ |
| 3 | + |
| 4 | + |
| 5 | +To execute a Query/Mutation against a Schema build a new ``GraphQL`` Object with the appropriate arguments and then call ``execute()``. |
| 6 | + |
| 7 | +The result of a Query is a ``ExecutionResult`` Object with the result and/or a list of Errors. |
| 8 | + |
| 9 | +Example: [GraphQL Test](src/test/groovy/graphql/GraphQLTest.groovy) |
| 10 | + |
| 11 | +More complex examples: [StarWars query tests](src/test/groovy/graphql/StarWarsQueryTest.groovy) |
| 12 | + |
| 13 | + |
| 14 | +Mutations |
| 15 | +---------- |
| 16 | + |
| 17 | +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>`_. |
| 18 | + |
| 19 | +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 |
| 20 | +via the data fetcher invoked. |
| 21 | + |
| 22 | +The mutation is invoked via a query like : |
| 23 | + |
| 24 | +.. code-block:: graphql |
| 25 | +
|
| 26 | + mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) { |
| 27 | + createReview(episode: $ep, review: $review) { |
| 28 | + stars |
| 29 | + commentary |
| 30 | + } |
| 31 | + } |
| 32 | +
|
| 33 | +You need to send in arguments during that mutation operation, in this case for the variables for `$ep` and `$review` |
| 34 | + |
| 35 | +You would create types like this to handle this mutation : |
| 36 | + |
| 37 | +.. code-block:: java |
| 38 | + GraphQLInputObjectType episodeType = GraphQLInputObjectType.newInputObject() |
| 39 | + .name("Episode") |
| 40 | + .field(newInputObjectField() |
| 41 | + .name("episodeNumber") |
| 42 | + .type(Scalars.GraphQLInt)) |
| 43 | + .build(); |
| 44 | +
|
| 45 | + GraphQLInputObjectType reviewInputType = GraphQLInputObjectType.newInputObject() |
| 46 | + .name("ReviewInput") |
| 47 | + .field(newInputObjectField() |
| 48 | + .name("stars") |
| 49 | + .type(Scalars.GraphQLString) |
| 50 | + .name("commentary") |
| 51 | + .type(Scalars.GraphQLString)) |
| 52 | + .build(); |
| 53 | +
|
| 54 | + GraphQLObjectType reviewType = newObject() |
| 55 | + .name("Review") |
| 56 | + .field(newFieldDefinition() |
| 57 | + .name("stars") |
| 58 | + .type(GraphQLString)) |
| 59 | + .field(newFieldDefinition() |
| 60 | + .name("commentary") |
| 61 | + .type(GraphQLString)) |
| 62 | + .build(); |
| 63 | +
|
| 64 | + GraphQLObjectType createReviewForEpisodeMutation = newObject() |
| 65 | + .name("CreateReviewForEpisodeMutation") |
| 66 | + .field(newFieldDefinition() |
| 67 | + .name("createReview") |
| 68 | + .type(reviewType) |
| 69 | + .argument(newArgument() |
| 70 | + .name("episode") |
| 71 | + .type(episodeType) |
| 72 | + ) |
| 73 | + .argument(newArgument() |
| 74 | + .name("review") |
| 75 | + .type(reviewInputType) |
| 76 | + ) |
| 77 | + .dataFetcher(mutationDataFetcher()) |
| 78 | + ) |
| 79 | + .build(); |
| 80 | +
|
| 81 | + GraphQLSchema schema = GraphQLSchema.newSchema() |
| 82 | + .query(queryType) |
| 83 | + .mutation(createReviewForEpisodeMutation) |
| 84 | + .build(); |
| 85 | +
|
| 86 | +
|
| 87 | +Notice that the input arguments are of type ``GraphQLInputObjectType``. This is important. Input arguments can ONLY be of that type |
| 88 | +and you cannot use output types such as ``GraphQLObjectType``. Scalars types are consider both input and output types. |
| 89 | + |
| 90 | +The data fetcher here is responsible for executing the mutation and returning some sensible output values. |
| 91 | + |
| 92 | +.. code-block:: java |
| 93 | +
|
| 94 | + private DataFetcher mutationDataFetcher() { |
| 95 | + return new DataFetcher() { |
| 96 | + @Override |
| 97 | + public Review get(DataFetchingEnvironment environment) { |
| 98 | + Episode episode = environment.getArgument("episode"); |
| 99 | + ReviewInput review = environment.getArgument("review"); |
| 100 | +
|
| 101 | + // make a call to your store to mutate your database |
| 102 | + Review updatedReview = reviewStore().update(episode, review); |
| 103 | +
|
| 104 | + // this returns a new view of the data |
| 105 | + return updatedReview; |
| 106 | + } |
| 107 | + }; |
| 108 | + } |
| 109 | +
|
| 110 | +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 |
| 111 | +to the caller. |
| 112 | + |
| 113 | +Execution strategies |
| 114 | +-------------------- |
| 115 | + |
| 116 | +All fields in a SelectionSet are executed serially per default. |
| 117 | + |
| 118 | +You can however provide your own execution strategies, one to use while querying data and one |
| 119 | +to use when mutating data. |
| 120 | + |
| 121 | +.. code-block:: java |
| 122 | +
|
| 123 | + ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor( |
| 124 | + 2, /* core pool size 2 thread */ |
| 125 | + 2, /* max pool size 2 thread */ |
| 126 | + 30, TimeUnit.SECONDS, |
| 127 | + new LinkedBlockingQueue<Runnable>(), |
| 128 | + new ThreadPoolExecutor.CallerRunsPolicy()); |
| 129 | +
|
| 130 | + GraphQL graphQL = GraphQL.newObject(StarWarsSchema.starWarsSchema) |
| 131 | + .queryExecutionStrategy(new ExecutorServiceExecutionStrategy(threadPoolExecutor)) |
| 132 | + .mutationExecutionStrategy(new SimpleExecutionStrategy()) |
| 133 | + .build(); |
| 134 | +
|
| 135 | +
|
| 136 | +
|
| 137 | +When provided fields will be executed parallel, except the first level of a mutation operation. |
| 138 | + |
| 139 | +See `specification <http://facebook.github.io/graphql/#sec-Normal-evaluation>`_ for details. |
| 140 | + |
| 141 | +Alternatively, schemas with nested lists may benefit from using a BatchedExecutionStrategy and creating batched DataFetchers with get() methods annotated @Batched. |
0 commit comments