Using nested mutations
Nested mutations enable to perform mutations on a type other than the root type in GraphQL.
The query below executes a standard mutation, using the mutation field updatePost
from the root type:
The query from above can also be executed through a nested mutation, where the post object is first queried through field post
, and then mutation field update
, which belongs to type Post
, is applied on the post object:
Mutations can also be nested, modifying data on the result from another mutation:
Simplified root type
Nested mutations change the root type, from QueryRoot
and MutationRoot
, to a single Root
type handling both queries and mutations:
Visualizing mutation fields
Use the Voyager client to visualize which are the mutation fields.
With nested mutations, every type in the schema can contain both query and mutation fields. To differentiate them, the mutation field's description is prepended with label "[Mutation] "
.
For instance, these are the fields for type Root
:
Using nested mutations in the endpoints
There are 2 levels in which we can define if the schema will use nested mutations or not. In order of priority:
1. On the schema configuration
Making a custom endpoint or persisted query use nested mutations, can be defined through the corresponding schema configuration:
2. Default mode, defined in the Settings
If the schema configuration has value "Default"
, it will use the mode defined in the Settings:
Configuring nested mutations
There are three behaviors we can choose for the schema:
1. Do no enable nested mutations
This option disables nested mutations (using the standard behavior instead) for the schema.
2. Enable nested mutations, keeping all mutation fields in the root
When nested mutations are enabled, mutation fields may be added two times to the schema:
- once under the
Root
type - once under the specific type
For instance:
Root.updatePost
Post.update
With this option, the "duplicated" mutation fields from the root type are kept.
3. Enable nested mutations, removing the redundant mutation fields from the root
Same option as above, but removing the "duplicated" mutation fields from the root type.
For instance:
Root.updatePost
is removedPost.update
is available
GraphQL spec
This functionality is currently not part of the GraphQL spec, but it has been requested: