Lesson 16: Sending a notification when there is a new post
Gato GraphQL can help us automate tasks in the application, such as sending a notification email to the admin when there is a new post.
In this tutorial lesson we will explore two ways to achieve this.
GraphQL query to send a notification email to the admin
This GraphQL query sends an email to the admin user, notifying of the creation of a new post on the site:
To send the email in plain text:
- Use input
messageAs: { text: ... }
in the_sendEmail
mutation - Remove the HTML tags from the post's content using global field
_htmlStripTags
(provided by the PHP Functions via Schema extension)
Let's see next how to trigger the execution of the GraphQL query.
Option 1: Trigger always by reacting to WordPress hooks
We hook into the WordPress core action new_to_publish
, retrieve the data from the newly-created post, and execute the GraphQL query defined above against the internal GraphQL server (provided via the Internal GraphQL Server extension):
Class GatoGraphQL\InternalGraphQLServer\GraphQLServer
is not accessible as an external API. Instead, it is to be used by the application via PHP code, for executing/automating admin tasks via GraphQL queries.
This class provides 3 static methods to execute queries:
executeQuery
: Execute a GraphQL queryexecuteQueryInFile
: Execute a GraphQL query contained in a (.gql
) fileexecutePersistedQuery
: Execute a persisted GraphQL query (providing its ID as an int, or slug as a string)
This GraphQL query will be executed whenever a new post is created or, to be more precise, whenever WordPress function wp_insert_post
is invoked (as this function triggers hook new_to_publish
):
This is also the case when executing another GraphQL query that executes the createPost
mutation (as its resolver, in PHP code, invokes function wp_insert_post
):
The GraphQL Server (which is "external", accessed as an API via HTTP) and the Internal GraphQL Server will execute their queries applying their own Schema Configuration, even when their execution is intertwined.
For instance, let' say we are executing a GraphQL query against the single endpoint, and it creates a post by executing mutation createPost
. Then the following sequence of steps takes place:
(External) GraphQL Server | Internal GraphQL Server |
---|---|
Execute GraphQL query against the single endpoint, using its own Schema Configuration | (not active) |
Create a post; this triggers new_to_publish | (not active) |
(waiting...) | React to new_to_publish hook: Spin the Internal GraphQL server, using its own Schema Configuration |
(waiting...) | Execute the query to send an email |
(waiting...) | Send email, end of that query |
(waiting...) | Shutdown server |
Continue execution of query, end of that query | (not active) |
Shutdown server | (not active) |
Option 2: Trigger by chaining GraphQL queries
The Automation extension makes the GraphQL Server trigger a hook after completing the execution of a GraphQL query. This allows us to chain GraphQL queries.
This PHP code executes the SendEmail
operation (GraphQL query defined above), after the GraphQL server has executed some other query with operation CreatePost
(GraphQL query defined above):
Chaining GraphQL queries allows us to execute a single query only, even when many resources were mutated.
For instance, this GraphQL query updates many posts:
Depending on our strategy, we can trigger the execution of one or multiple additional GraphQL queries:
Hooking into... | Triggers number of GraphQL queries... |
---|---|
post_updated (by WordPress core) | One for every updated post |
gatographql__executed_query:ReplaceDomains (by Automation extension) | One in total (it will receive the data for all updated posts) |