Automation
AutomationQuery Resolution Action

Query Resolution Action

When the GraphQL server resolves a query, it triggers the following action hooks with the GraphQL response:

  1. gatographql__executed_query:{$operationName} (only if the GraphQL operation to execute was provided)
  2. gatographql__executed_query

The action hooks that are triggered are:

// Triggered only if the GraphQL operation to execute was provided
do_action(
  "gatographql__executed_query:{$operationName}",
  $response,
  $isInternalExecution,
  $query,
  $variables,
);
 
// Triggered always
do_action(
  'gatographql__executed_query',
  $response,
  $isInternalExecution,
  $operationName,
  $query,
  $variables,
);

The parameters passed are:

  • $response: An object of class PoP\Root\HttpFoundation\Response, containing the GraphQL response (including content and headers)
  • $isInternalExecution: true if the query was executed via the Internal GraphQL Server (eg: via class GatoGraphQL\InternalGraphQLServer\GraphQLServer), or false otherwise (eg: via the single endpoint)
  • $operationName: Executed GraphQL operation (for the second action hook only; on the first one, it is implicit on the hook name)
  • $query: Executed GraphQL query
  • $variables: Provided GraphQL variables

Examples

Thanks to the Internal GraphQL Server, we can react to the resolution of a GraphQL query (whether executed against the internal GraphQL Server, single endpoint, custom endpoint or persisted query), and execute another GraphQL query against the internal GraphQL Server.

An example workflow is:

  • Hook into the execution of a GraphQL query, for instance by its operation name (such as CreatePost)
  • Send a notification to the admin, by executing mutation _sendEmail via GatoGraphQL\InternalGraphQLServer\GraphQLServer::executeQuery

This PHP code is chaining 2 GraphQL query executions:

GraphQLServer::executeQuery(
  <<<GRAPHQL
    mutation CreatePost(
      \$postTitle: String!,
      \$postContent: String!
    ) {
      createPost(input: {
        title: \$postTitle
        contentAs: { html: \$postContent }
      }) {
        status
        errors {
          __typename
          ...on ErrorPayload {
            message
          }
        }
        postID
      }
    }
  GRAPHQL,
  [
    'postTitle' => 'New post',
    'postContent' => 'Some content',
  ],
  'CreatePost'
);
 
add_action(
  "gatographql__executed_query:CreatePost",
  function (Response $response) {
    /** @var string */
    $responseContent = $response->getContent();
    /** @var array<string,mixed> */
    $responseJSON = json_decode($responseContent, true);
    $postID = $responseJSON['data']['createPost']['postID'] ?? null;
    if ($postID === null) {
      // Do nothing
      return;
    }
 
    $post = get_post($postID);
 
    // Execute the chained query!
    GraphQLServer::executeQuery(
      <<<GRAPHQL
        mutation SendEmail(
          \$emailSubject: String!
          \$emailMessage: String!
        ) {
          _sendEmail(
            input: {
              to: "admin@site.com"
              subject: \$emailSubject
              messageAs: {
                html: \$emailMessage
              }
            }
          ) {
            status
          }
        }
      GRAPHQL,
      [
        'emailSubject' => sprintf(__("New post: %s"), $post->post_title),
        'emailMessage' => $post->post_content,
      ]
    );
  }
);