Skip to content

Repository files navigation

HTTP Schema

unit tests codecov

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

About

Building GraphQL schema over HTTP (aka remote schema)

Resources

Code of conduct

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages