You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/wpgraphql-mutations.md
+76Lines changed: 76 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,3 +8,79 @@ WPGraphQL provides support for Mutations, or the ability to use GraphQL to creat
8
8
## Register a new Mutation
9
9
10
10
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.
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:
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
+

0 commit comments