AutomazioneAzione di Risoluzione della Query
Azione di Risoluzione della Query
Quando il server GraphQL risolve una query, attiva i seguenti action hook con la risposta GraphQL:
gatographql__executed_query:{$operationName}(solo se è stata fornita l'operazione GraphQL da eseguire)gatographql__executed_query
Gli action hook che vengono attivati sono:
// 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,
);I parametri passati sono:
$response: Un oggetto della classePoP\Root\HttpFoundation\Response, contenente la risposta GraphQL (incluso il contenuto e gli header)$isInternalExecution:truese la query è stata eseguita tramite l'Internal GraphQL Server (ad esempio: tramite la classeGatoGraphQL\InternalGraphQLServer\GraphQLServer), oppurefalsein caso contrario (ad esempio: tramite l'endpoint singolo)$operationName: Operazione GraphQL eseguita (solo per il secondo action hook; nel primo è implicita nel nome dell'hook)$query: Query GraphQL eseguita$variables: Variabili GraphQL fornite
Esempi
Grazie all'Internal GraphQL Server, possiamo reagire alla risoluzione di una query GraphQL (sia che venga eseguita contro l'Internal GraphQL Server, l'endpoint singolo, un endpoint personalizzato o una persisted query), ed eseguire un'altra query GraphQL contro l'Internal GraphQL Server.
Un esempio di flusso di lavoro è:
- Agganciarsi all'esecuzione di una query GraphQL, ad esempio tramite il suo nome di operazione (come
CreatePost) - Inviare una notifica all'amministratore, eseguendo la mutation
_sendEmailtramiteGatoGraphQL\InternalGraphQLServer\GraphQLServer::executeQuery
Questo codice PHP concatena 2 esecuzioni di 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,
]
);
}
);