This is an example child theme for TwentyTwenty displaying how to mutate a profile field from the user in WordPress.
It works off the core functionality implemented in WPGraphQL. In addition, it adds a couple profile fields to the user profile for SEO, therefore it relies on the WP GraphQL Yoast extension.
An ACF field can also be obtained if set inside resolve function response field for the custom field. This uses a custom field that is not registered through ACF, but can be added through ACF and likewise obtained programmatically in the resolver.
As of now, this only allows the mutations for updating, not creating posts or users. To add to the creation mutation, or for any mutation really, find the type name of the mutation input, and place that in the conditional check for the type name on line 90 or 100 of the functions file: $type_name === 'UpdateUserInput'
. In this case, it is UpdateUserInput, but you can typically find that type name by looking through the docs of WPGraphQL.
-
WordPress with TwentyTwenty theme installed and the following plugins listed
-
Frontend to run the mutations.
It adds the following profile fields to the user:
- GitHub profile link. It only adds it internally so that you could edit the profile fields through the mutation anyways, but you could always add it the WordPress admin dashboard through a filter.
It hooks into the existing fields on the user profile:
-
LinkedIn email
-
Twitter username (without the @)
It also updates a field created through ACF programmatically. This field gets created when the theme activates.
The field ID can be obtained either from the key assigned to the field or through the export function if working on the ACF gui.
-
Fields added to user may not show up when added with ACF because user profile fields contain sensitive data. It requires the configuration with ACF to configure what role may view profile fields.
-
resolution:
- Hook into the user response to check for credentials and authorizing the current user requesting the data.
-
-
If you don't create the fields initially through ACF or through a mutation, then the GraphQL query for the profile links throws an error.
typical input:
{
"input": {
"github": "https://github.com/saleebm",
"twitter": "minasa1eeb",
"linkedIn": "https://www.linkedin.com/in/link-up-with-mina-saleeb/",
"id": "dXNlcjoy",
"clientMutationId": "test1"
}
}
# the mutation
mutation UPDATE_USER_SOCIAL_LINKS($input: UpdateUserInput!) {
updateUser(input: $input) {
clientMutationId
user {
personalLinks {
github
}
seo {
social {
twitter
linkedIn
}
}
}
}
}
# the result for users
query RESULT {
users {
edges {
node {
personalLinks {
github
}
seo {
social {
twitter
linkedIn
}
}
}
}
}
}
Input for the mutation
{
"input": {
"clientMutationId": "test1324",
"id": "cG9zdDoyNjYz",
"title": "example1",
"example": "test"
}
}
The mutation to update the ACF field
# update post
mutation UPDATE_POST($input: UpdatePostInput!) {
updatePost(input: $input) {
clientMutationId
post {
id
title
example {
example
fieldGroupName
}
}
}
}