Automation
AutomationAksi Resolusi Query

Aksi Resolusi Query

Ketika server GraphQL menyelesaikan sebuah query, ia memicu action hooks berikut dengan respons GraphQL:

  1. gatographql__executed_query:{$operationName} (hanya jika operasi GraphQL yang akan dieksekusi telah disediakan)
  2. gatographql__executed_query

Action hooks yang dipicu adalah:

// 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,
);

Parameter yang diteruskan adalah:

  • $response: Objek dari kelas PoP\Root\HttpFoundation\Response, berisi respons GraphQL (termasuk konten dan header)
  • $isInternalExecution: true jika query dieksekusi melalui Internal GraphQL Server (mis: melalui kelas GatoGraphQL\InternalGraphQLServer\GraphQLServer), atau false sebaliknya (mis: melalui single endpoint)
  • $operationName: Operasi GraphQL yang dieksekusi (hanya untuk action hook kedua; pada yang pertama, sudah tersirat dalam nama hook)
  • $query: Query GraphQL yang dieksekusi
  • $variables: Variabel GraphQL yang disediakan

Contoh

Berkat Internal GraphQL Server, kita dapat bereaksi terhadap resolusi sebuah query GraphQL (baik yang dieksekusi terhadap Internal GraphQL Server, single endpoint, custom endpoint, maupun persisted query), dan mengeksekusi query GraphQL lain terhadap Internal GraphQL Server.

Contoh alur kerjanya adalah:

  • Pasangkan hook ke eksekusi sebuah query GraphQL, misalnya berdasarkan nama operasinya (seperti CreatePost)
  • Kirim notifikasi ke admin, dengan mengeksekusi mutasi _sendEmail melalui GatoGraphQL\InternalGraphQLServer\GraphQLServer::executeQuery

Kode PHP ini merangkaikan 2 eksekusi query GraphQL:

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,
      ]
    );
  }
);