digiaonline / graphql-relay
Relay support for digiaonline/graphql
Installs: 7 144
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 7
Forks: 0
Open Issues: 0
Requires
- php: >=7.1
Requires (Dev)
- digiaonline/graphql: dev-master
- phpstan/phpstan: ^0.10
- phpunit/phpunit: ^7.1
This package is auto-updated.
Last update: 2023-03-29 00:41:46 UTC
README
Relay support for our GraphQL implementation.
Requirements
- PHP >= 7.1
Usage
Installation
Run the following command to install the package through Composer:
composer require digiaonline/graphql-relay:dev-master
Example
Executing this script:
use function Digia\GraphQL\buildSchema; use function Digia\GraphQL\graphql; $source = file_get_contents(__DIR__ . '/star-wars.graphqls'); $schema = buildSchema($source, [ 'Query' => [ 'rebels' => function () { return rebels(); }, 'empire' => function () { return empire(); } ], 'Faction' => [ 'ships' => function ($faction, $args) { $data = getShips($faction); $arguments = ConnectionArguments::fromArray($args); return ArrayConnectionBuilder::fromArray($data, $arguments); } ] ]); $result = graphql($schema, ' query RebelsShipsQuery { rebels { name, ships(first: 1) { edges { node { name } } } } }'); print_r($result);
Produces the following output:
Array ( [rebels] => Array ( [name] => Alliance to Restore the Republic [ships] => Array ( [edges] => Array ( [0] => Array ( [node] => Array ( [name] => X-Wing ) ) ) ) ) )
The schema definition used looks like this:
"A connection to a list of items." interface Connection { "A list of edges." edges: [Edge] "Information to aid in pagination." pageInfo: PageInfo! } "An edge in a connection." interface Edge { "A cursor for use in pagination." cursor: String! "The item at the end of the edge." node: Node } "An object with an ID." interface Node { "ID of the object." id: ID! } "Information about pagination in a connection." type PageInfo { "When paginating forwards, are there more items?" hasPreviousPage: Boolean! "When paginating backwards, are there more items?" hasNextPage: Boolean! "When paginating backwards, the cursor to continue." endCursor: String "When paginating forwards, the cursor to continue." startCursor: String } type Faction implements Node { "The ID of an object." id: ID! "The name of the faction." name: String "The ships used by the faction." ships(after: String, before: String, first: Int, last: Int): ShipConnection } "A ship in the Star Wars saga" type Ship implements Node { "The ID of an object." id: ID! "The name of the ship." name: String } type ShipConnection implements Connection { edges: [ShipEdge] pageInfo: PageInfo! } type ShipEdge implements Edge { cursor: String! node: Ship } type Query { rebels: Faction empire: Faction node(id: ID!): Node } schema { query: Query }
Node root field
For implementing the Node root field a convenience class is provided:
Convert Type and ID to Global ID
$nodeId = Node::toGlobalId('Ship', '1');
returns a global ID which can be passed to the node root:
U2hpcDox
Convert Global ID back to type and ID
$node = Node::fromGlobalId('U2hpcDox');
returns an object which can be queried:
$node->getType(); // Ship $node->getId(); // 1
Node root resolver
For an example of how to implement the node root resolver please check the StarWarsConnectionTest.php
Contributing
Please read our guidelines.
License
See LICENSE.