Skip to content

Commit 54d0352

Browse files
authored
update mutations guide to show extending existing mutations
1 parent e8262a9 commit 54d0352

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
81.1 KB
Loading
173 KB
Loading

docs/wpgraphql-mutations.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,79 @@ WPGraphQL provides support for Mutations, or the ability to use GraphQL to creat
88
## Register a new Mutation
99

1010
To register a mutation to the GraphQL Schema, you can use the `register_graphql_mutation` function [documented here](/functions/register_graphql_mutation/).
11+
12+
## Extending existing mutations
13+
14+
To extend existing mutations, there are typically 2 things that need to happen:
15+
16+
- add the new input field(s) to the schema
17+
- hook into the mutation and do something with the input (i.e. save it as meta)
18+
19+
Below are some examples that showcase this in action:
20+
21+
### Extending the "Create Post" mutation
22+
23+
Let's say we wanted to extend the "Create Post" mutation to store a "color" as post_meta on a post.
24+
25+
We could do this by adding a "color" input field to the CreatePostInput type in the schema, then hooking into the create post mutation lifecycle and using the input and save it as post_meta.
26+
27+
#### Adding the Mutation Input Field
28+
29+
Below is a snippet that registers a new "color" input field to the `CreatePostInput` type in the Schema.
30+
31+
```php
32+
<?php
33+
add_action( 'graphql_register_types', function() {
34+
register_graphql_field( 'CreatePostInput', 'color', [
35+
'type' => 'String',
36+
'description' => __( 'The color of the post', 'your-textdomain' )
37+
] );
38+
} );
39+
?>
40+
```
41+
42+
This adds the field to the Schema and we can see it in the GraphiQL IDE:
43+
44+
![Screenshot of the GraphiQL IDE showing the color field int the Schema](./images/mutations-custom-input-field-example.png)
45+
46+
However, if we execute the mutation at this point, the color that we input would do nothing.
47+
48+
#### Extending the Mutation to save the data
49+
50+
We need to hook into the mutation lifecycle to save the data that was passed into the mutation.
51+
52+
```php
53+
add_action( 'graphql_post_object_mutation_update_additional_data', function( $post_id, $input, $post_type_object, $mutation_name, $context, $info, $default_post_status, $intended_post_status ) {
54+
55+
// The input comes in as $input and we can use the input to save the post meta. If the input for this field doesn't exist, bail early
56+
if ( ! isset( $input['color'] ) ) {
57+
return;
58+
}
59+
60+
$sanitized_color = sanitize_text_field( $input['color'] );
61+
update_post_meta( $post_id, 'color', $sanitized_color );
62+
63+
64+
}, 10, 8 );
65+
```
66+
67+
#### Adding the queryable "color" field
68+
69+
If you already have the "color" field in your Schema on the "Post" type using something like WPGraphQL for Advanced Custom Fields, you might be able to skip this step. But if you need to expose the "color" field to be queryable, you can do it like so:
70+
71+
```php
72+
add_action( 'graphql_register_types', function() {
73+
register_graphql_field( 'Post', 'color', [
74+
'type' => 'String',
75+
'description' => __( 'The color of the post', 'your-textdomain' ),
76+
'resolve' => function( $post, $args, $context, $info ) {
77+
$color = get_post_meta( $post->databaseId, 'color', true );
78+
return $color ?? null;
79+
}
80+
] );
81+
});
82+
```
83+
84+
And now, with all 3 snippets in place we can successfully execute a `createPost` mutation, inputting a "color" value that will save to the post_meta and will be queryable as "color" on the post.
85+
86+
![Screenshot of the GraphiQL IDE a createPost mutation with a color field input and the color value queried in response to the mutation](./images/mutations-create-post-with-custom-input-example.png)

0 commit comments

Comments
 (0)