자동화쿼리 해결 액션
쿼리 해결 액션
GraphQL 서버가 쿼리를 해결하면, GraphQL 응답과 함께 다음 액션 훅이 트리거됩니다.
gatographql__executed_query:{$operationName}(실행할 GraphQL 작업이 지정된 경우에만)gatographql__executed_query
트리거되는 액션 훅은 다음과 같습니다.
// 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 응답(콘텐츠와 헤더 포함)을 담고 있습니다$isInternalExecution: 쿼리가 Internal GraphQL Server를 통해 실행된 경우(예:GatoGraphQL\InternalGraphQLServer\GraphQLServer클래스를 통해)에는true, 그렇지 않은 경우(예: 단일 엔드포인트를 통해)에는false$operationName: 실행된 GraphQL 작업(두 번째 액션 훅에서만 사용되며, 첫 번째에서는 훅 이름에 암묵적으로 포함됩니다)$query: 실행된 GraphQL 쿼리$variables: 제공된 GraphQL 변수
예시
Internal GraphQL Server 덕분에, GraphQL 쿼리의 해결(Internal GraphQL Server, 단일 엔드포인트, 커스텀 엔드포인트, 또는 persisted query에 대해 실행된 것)에 반응하여, Internal GraphQL Server에 대해 다른 GraphQL 쿼리를 실행할 수 있습니다.
워크플로우 예시는 다음과 같습니다.
- GraphQL 쿼리의 실행에 훅을 연결합니다. 예를 들어 작업 이름(
CreatePost등)을 기준으로 GatoGraphQL\InternalGraphQLServer\GraphQLServer::executeQuery를 통해 뮤테이션_sendEmail을 실행하여 관리자에게 알림을 전송합니다
이 PHP 코드는 2개의 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,
]
);
}
);