Internal GraphQL Server
รันคำสั่ง GraphQL queries โดยตรงภายในแอปพลิเคชันของคุณ โดยใช้โค้ด PHP

ส่วนขยายนี้ติดตั้ง Internal GraphQL Server ที่สามารถเรียกใช้งานได้ภายในแอปพลิเคชันของคุณ โดยใช้โค้ด PHP
เข้าถึง Internal GraphQL Server ผ่านคลาส GatoGraphQL\InternalGraphQLServer\GraphQLServer ด้วยเมธอดสามรูปแบบดังนี้:
executeQuery: รัน GraphQL queryexecuteQueryInFile: รัน GraphQL query ที่บรรจุอยู่ในไฟล์ (.gql)executePersistedQuery: รัน GraphQL query ที่บันทึกไว้ (ระบุ ID เป็น int หรือ slug เป็น string) (ต้องใช้ส่วนขยาย Persisted Queries)
ลายเซ็นของเมธอดมีดังนี้:
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 {
// ...
}
}เพื่อรัน GraphQL query และรับเนื้อหาของ response:
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"] ?? [];