Internal GraphQL Server
Voer GraphQL queries rechtstreeks uit binnen je applicatie, met PHP-code.

Deze extensie installeert een interne GraphQL-server, die binnen je applicatie kan worden aangeroepen via PHP-code.
De interne GraphQL-server is toegankelijk via de klasse GatoGraphQL\InternalGraphQLServer\GraphQLServer, via deze drie methoden:
executeQuery: Voer een GraphQL query uitexecuteQueryInFile: Voer een GraphQL query uit die in een (.gql)-bestand staatexecutePersistedQuery: Voer een persisted GraphQL query uit (door het ID als integer of de slug als string op te geven) (de extensie Persisted Queries is vereist)
Dit zijn de methode-handtekeningen:
namespace GatoGraphQL\InternalGraphQLServer;
use PoP\Root\HttpFoundation\Response;
class GraphQLServer {
/**
* Execute a GraphQL query
*/
public static function executeQuery(
string $query,
array $variables = [],
?string $operationName = null,
int|string|null $schemaConfigurationIDOrSlug = null,
): Response {
// ...
}
/**
* Execute a GraphQL query contained in a (`.gql`) file
*/
public static function executeQueryInFile(
string $file,
array $variables = [],
?string $operationName = null,
int|string|null $schemaConfigurationIDOrSlug = null,
): Response {
// ...
}
/**
* Execute a persisted GraphQL query (providing its object
* of type WP_Post, ID as an int, or slug as a string)
*/
public static function executePersistedQuery(
WP_Post|string|int $persistedQuery,
array $variables = [],
?string $operationName = null
): Response {
// ...
}
}Om een GraphQL query uit te voeren en de inhoud van het antwoord op te halen:
use GatoGraphQL\InternalGraphQLServer\GraphQLServer;
// Provide the GraphQL query
$query = "{ ... }";
// Execute the query against the internal server
$response = GraphQLServer::executeQuery($query);
// Get the content and decode it
$responseContent = json_decode($response->getContent(), true);
// Access the data and errors from the response
$responseData = $responseContent["data"] ?? [];
$responseErrors = $responseContent["errors"] ?? [];