Internal GraphQL Server
PHP 코드를 사용하여 애플리케이션 내부에서 직접 GraphQL 쿼리를 실행합니다.

이 확장 기능은 내부 GraphQL 서버를 설치합니다. 이 서버는 PHP 코드를 사용하여 애플리케이션 내부에서 호출할 수 있습니다.
내부 GraphQL 서버는 클래스 GatoGraphQL\InternalGraphQLServer\GraphQLServer를 통해 다음 세 가지 메서드로 접근합니다:
executeQuery: GraphQL 쿼리를 실행합니다executeQueryInFile: (.gql) 파일에 포함된 GraphQL 쿼리를 실행합니다executePersistedQuery: 저장된 GraphQL 쿼리를 실행합니다(ID를 정수로, 또는 슬러그를 문자열로 지정)(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 쿼리를 실행하고 응답 내용을 가져오려면:
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"] ?? [];