x-graphql / http-schema
Building GraphQL schema over HTTP (aka remote schema)
Installs: 11 977
Dependents: 2
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- php: >=8.2
- php-http/discovery: ^1.19
- php-http/httplug: ^2.4
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0 || ^2.0
- webonyx/graphql-php: ^15.9
- x-graphql/delegate-execution: ^0.9
- x-graphql/schema-cache: ^0.1.0
Requires (Dev)
- guzzlehttp/promises: ^2.0
- nyholm/psr7: ^1.8
- phpunit/phpunit: ^11.0
- symfony/cache: ^6.4 || ^7.0
- symfony/http-client: ^6.4 || ^7.0
- symplify/easy-coding-standard: ^12.1
README
Help to build and execute GraphQL schema over HTTP (aka remote schema).
Getting Started
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
Usages
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.
From SDL
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());
From introspection query
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());
Caching schema
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); /// ........
Credits
Created by Minh Vuong