2017 © Pedro Peláez
 

library graphql-relay

Relay support for digiaonline/graphql

image

digiaonline/graphql-relay

Relay support for digiaonline/graphql

  • Tuesday, July 10, 2018
  • by crisu83
  • Repository
  • 6 Watchers
  • 4 Stars
  • 166 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 61 % Grown

The README.md

GraphQL Relay

Build Status Coverage Status License, (*1)

Relay support for our GraphQL implementation., (*2)

Requirements

  • PHP >= 7.1

Usage

Installation

Run the following command to install the package through Composer:, (*3)

composer require digiaonline/graphql-relay:dev-master

Example

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

Convert Global ID back to type and ID

$node = Node::fromGlobalId('U2hpcDox');

returns an object which can be queried:, (*18)

$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, (*19)

Contributing

Please read our guidelines., (*20)

License

See LICENSE., (*21)

The Versions

10/07 2018

dev-master

9999999-dev

Relay support for digiaonline/graphql

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires