Internal GraphQL Server
Thực thi các GraphQL queries trực tiếp trong ứng dụng của bạn bằng mã PHP.

Tiện ích mở rộng này cài đặt một máy chủ GraphQL nội bộ, có thể được gọi trong ứng dụng của bạn bằng mã PHP.
Máy chủ GraphQL nội bộ được truy cập thông qua lớp GatoGraphQL\InternalGraphQLServer\GraphQLServer, với ba phương thức sau:
executeQuery: Thực thi một GraphQL queryexecuteQueryInFile: Thực thi một GraphQL query chứa trong tệp (.gql)executePersistedQuery: Thực thi một persisted GraphQL query (cung cấp ID dưới dạng số nguyên, hoặc slug dưới dạng chuỗi) (yêu cầu tiện ích mở rộng Persisted Queries)
Dưới đây là chữ ký của các phương thức:
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 {
// ...
}
}Để thực thi một GraphQL query và lấy nội dung phản hồi:
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"] ?? [];