Help to build and execute GraphQL schema over HTTP (aka remote schema).
Install this package via Composer
composer require x-graphql/http-schema
This library require PSR-18 Client or Httplug Async Client for sending async requests. Run the command bellow if you don't have yet.
composer require php-http/guzzle7-adapter
This library offers to you 2 strategy to build schema:
- Build from schema definition language (SDL), this strategy use for limiting fields user can access.
- Build from introspection query, with this strategy it will make a http request for introspecting schema, user can access all the fields.
use GraphQL\GraphQL;
use XGraphQL\HttpSchema\HttpDelegator;
use XGraphQL\HttpSchema\HttpSchemaFactory;
$delegator = new HttpDelegator('https://countries.trevorblades.com/');
$schema = HttpSchemaFactory::createFromSDL(
$delegator,
<<<'SDL'
type Query {
countries: [Country!]!
}
type Country {
name: String!
}
SDL
);
$result = GraphQL::executeQuery($schema, 'query { countries { name } }');
var_dump($result->toArray());
use GraphQL\GraphQL;
use XGraphQL\HttpSchema\HttpDelegator;
use XGraphQL\HttpSchema\HttpSchemaFactory;
$delegator = new HttpDelegator('https://countries.trevorblades.com/');
$schema = HttpSchemaFactory::createFromIntrospectionQuery($delegator);
$result = GraphQL::executeQuery($schema, 'query { countries { name } }');
var_dump($result->toArray());
For optimizing time to build schema from SDL or introspection query, you can put PSR-16 instance to factory methods for caching schema after built:
use XGraphQL\HttpSchema\HttpDelegator;
use XGraphQL\HttpSchema\HttpSchemaFactory;
/// $psr16Cache = ....
$delegator = new HttpDelegator('https://countries.trevorblades.com/');
$schemaFromSDL = HttpSchemaFactory::createFromSDL($delegator, /// $sdl, $psr16Cache);
$schemaFromIntrospection = HttpSchemaFactory::createFromIntrospectionQuery($delegator, /// $psr16Cache);
/// ........
Created by Minh Vuong