ระบบอัตโนมัติ
ระบบอัตโนมัติQuery Resolution Action

Query Resolution Action

เมื่อ GraphQL server ประมวลผล query เสร็จสิ้น จะทริกเกอร์ action hooks ต่อไปนี้พร้อมกับ GraphQL response:

  1. gatographql__executed_query:{$operationName} (เฉพาะเมื่อมีการระบุ GraphQL operation ที่ต้องการประมวลผล)
  2. gatographql__executed_query

Action hooks ที่ถูกทริกเกอร์มีดังนี้:

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

พารามิเตอร์ที่ส่งผ่านมามีดังนี้:

  • $response: อ็อบเจ็กต์ของคลาส PoP\Root\HttpFoundation\Response ที่บรรจุ GraphQL response (รวมถึงเนื้อหาและ headers)
  • $isInternalExecution: true หาก query ถูกประมวลผลผ่าน Internal GraphQL Server (เช่น ผ่านคลาส GatoGraphQL\InternalGraphQLServer\GraphQLServer) หรือ false ในกรณีอื่น (เช่น ผ่าน single endpoint)
  • $operationName: GraphQL operation ที่ถูกประมวลผล (สำหรับ action hook ที่สองเท่านั้น; ใน hook แรกจะระบุโดยปริยายในชื่อ hook)
  • $query: GraphQL query ที่ถูกประมวลผล
  • $variables: GraphQL variables ที่ระบุ

ตัวอย่าง

ด้วย Internal GraphQL Server เราสามารถตอบสนองต่อการประมวลผล GraphQL query (ไม่ว่าจะประมวลผลผ่าน Internal GraphQL Server, single endpoint, custom endpoint หรือ persisted query) และประมวลผล GraphQL query อีกชุดผ่าน Internal GraphQL Server ได้

ตัวอย่าง workflow มีดังนี้:

  • เชื่อมต่อกับการประมวลผล GraphQL query เช่น โดยใช้ชื่อ operation (เช่น CreatePost)
  • ส่งการแจ้งเตือนไปยัง admin โดยประมวลผล mutation _sendEmail ผ่าน GatoGraphQL\InternalGraphQLServer\GraphQLServer::executeQuery

โค้ด PHP นี้เชื่อมต่อการประมวลผล GraphQL queries จำนวน 2 ชุดต่อเนื่องกัน:

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