Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

digiaonline/graphql-relay-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL Relay

Build Status Coverage Status License

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.

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages