Otomasyon
OtomasyonQuery Çözümleme Eylemi

Query Çözümleme Eylemi

GraphQL sunucusu bir query çözümlediğinde, GraphQL yanıtıyla birlikte aşağıdaki action hook'ları tetikler:

  1. gatographql__executed_query:{$operationName} (yalnızca çalıştırılacak GraphQL işlemi sağlandıysa)
  2. gatographql__executed_query

Tetiklenen action hook'lar şunlardır:

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

Aktarılan parametreler şunlardır:

  • $response: GraphQL yanıtını (içerik ve başlıklar dahil) barındıran PoP\Root\HttpFoundation\Response sınıfından bir nesne
  • $isInternalExecution: Query, Internal GraphQL Server aracılığıyla çalıştırıldıysa (örn: GatoGraphQL\InternalGraphQLServer\GraphQLServer sınıfı aracılığıyla) true, aksi takdirde (örn: tek endpoint aracılığıyla) false
  • $operationName: Çalıştırılan GraphQL işlemi (yalnızca ikinci action hook için; birincisinde hook adından zaten anlaşılmaktadır)
  • $query: Çalıştırılan GraphQL query
  • $variables: Sağlanan GraphQL değişkenleri

Örnekler

Internal GraphQL Server sayesinde, bir GraphQL query çözümlemesine (Internal GraphQL Server, tek endpoint, özel endpoint veya persisted query aracılığıyla çalıştırılmış olsun) tepki verebilir ve Internal GraphQL Server'a karşı başka bir GraphQL query çalıştırabiliriz.

Örnek bir iş akışı şöyledir:

  • Bir GraphQL query çalıştırmasını, örneğin işlem adına göre (örn: CreatePost) yakala
  • GatoGraphQL\InternalGraphQLServer\GraphQLServer::executeQuery aracılığıyla _sendEmail mutation'ını çalıştırarak yöneticiye bir bildirim gönder

Bu PHP kodu, 2 GraphQL query çalıştırmasını zincirlemektedir:

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