Internal GraphQL Server
Führe GraphQL-queries direkt in deiner Anwendung aus, mit PHP-Code.

Diese Erweiterung installiert einen internen GraphQL-Server, der innerhalb deiner Anwendung per PHP-Code aufgerufen werden kann.
Der interne GraphQL-Server wird über die Klasse GatoGraphQL\InternalGraphQLServer\GraphQLServer zugegriffen, durch diese drei Methoden:
executeQuery: Führt eine GraphQL-query ausexecuteQueryInFile: Führt eine GraphQL-query aus, die in einer (.gql)-Datei enthalten istexecutePersistedQuery: Führt eine persisted GraphQL-query aus (mit der ID als Integer oder dem Slug als String) (die Erweiterung Persisted Queries ist erforderlich)
Dies sind die Methodensignaturen:
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 {
// ...
}
}Um eine GraphQL-query auszuführen und den Antwortinhalt zu erhalten:
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"] ?? [];