dev-master
9999999-devRelay support for digiaonline/graphql
MIT
The Requires
- php >=7.1
The Development Requires
Wallogit.com
2017 © Pedro Peláez
Relay support for digiaonline/graphql
Relay support for our GraphQL implementation., (*2)
Run the following command to install the package through Composer:, (*3)
composer require digiaonline/graphql-relay:dev-master
Executing this script:, (*4)
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:, (*5)
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:, (*6)
```graphql schema "A connection to a list of items." interface Connection { "A list of edges." edges: [Edge] "Information to aid in pagination." pageInfo: PageInfo! }, (*7)
"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 }, (*8)
"An object with an ID." interface Node { "ID of the object." id: ID! }, (*9)
"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 }, (*10)
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 }, (*11)
"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 }, (*12)
type ShipConnection implements Connection { edges: [ShipEdge] pageInfo: PageInfo! }, (*13)
type ShipEdge implements Edge { cursor: String! node: Ship }, (*14)
type Query { rebels: Faction empire: Faction node(id: ID!): Node }, (*15)
schema { query: Query }, (*16)
### Node root field For implementing the [Node root field](https://facebook.github.io/relay/graphql/objectidentification.htm#sec-Node-root-field) a convenience class is provided: #### Convert Type and ID to Global ID ```php $nodeId = Node::toGlobalId('Ship', '1');
returns a global ID which can be passed to the node root:, (*17)
U2hpcDox
$node = Node::fromGlobalId('U2hpcDox');
returns an object which can be queried:, (*18)
$node->getType(); // Ship $node->getId(); // 1
For an example of how to implement the node root resolver please check the StarWarsConnectionTest.php, (*19)
Please read our guidelines., (*20)
See LICENSE., (*21)
Relay support for digiaonline/graphql
MIT