PHP code example of ivome / graphql-relay-php

1. Go to this page and download the library: Download ivome/graphql-relay-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ivome / graphql-relay-php example snippets


$shipConnection = Relay::connectionDefinitions([
    'nodeType' => $shipType
]);

// this could also be written as
//
// $shipEdge = Relay::edgeType([
//     'nodeType' => $shipType
// ]);
// $shipConnection = Relay::connectionType([
//     'nodeType' => $shipType,
//     'edgeType' => $shipEdge
// ]);

$factionType = new ObjectType([
    'name' => 'Faction',
    'description' => 'A faction in the Star Wars saga',
    'fields' => function() use ($shipConnection) {
        return [
            'id' => Relay::globalIdField(),
            'name' => [
                'type' => Type::string(),
                'description' => 'The name of the faction.'
            ],
            'ships' => [
                'type' => $shipConnection['connectionType'],
                'description' => 'The ships used by the faction.',
                'args' => Relay::connectionArgs(),
                'resolve' => function($faction, $args) {
                    // Map IDs from faction back to ships
                    $data = array_map(function($id) {
                        return StarWarsData::getShip($id);
                    }, $faction['ships']);
                    return Relay::connectionFromArray($data, $args);
                }
            ]
        ];
    },
    'interfaces' => [$nodeDefinition['nodeInterface']]
]);

$nodeDefinition = Relay::nodeDefinitions(
    // The ID fetcher definition
    function ($globalId) {
        $idComponents = Relay::fromGlobalId($globalId);
        if ($idComponents['type'] === 'Faction'){
            return StarWarsData::getFaction($idComponents['id']);
        } else if ($idComponents['type'] === 'Ship'){
            return StarWarsData::getShip($idComponents['id']);
        } else {
            return null;
        }
    },
    // Type resolver
    function ($object) {
        return isset($object['ships']) ? self::getFactionType() : self::getShipType();
    }
);

$factionType = new ObjectType([
    'name' => 'Faction',
    'description' => 'A faction in the Star Wars saga',
    'fields' => function() use ($shipConnection) {
        return [
            'id' => Relay::globalIdField(),
            'name' => [
                'type' => Type::string(),
                'description' => 'The name of the faction.'
            ],
            'ships' => [
                'type' => $shipConnection['connectionType'],
                'description' => 'The ships used by the faction.',
                'args' => Relay::connectionArgs(),
                'resolve' => function($faction, $args) {
                    // Map IDs from faction back to ships
                    $data = array_map(function($id) {
                        return StarWarsData::getShip($id);
                    }, $faction['ships']);
                    return Relay::connectionFromArray($data, $args);
                }
            ]
        ];
    },
    'interfaces' => [$nodeDefinition['nodeInterface']]
]);

$queryType = new ObjectType([
    'name' => 'Query',
    'fields' => function () use ($nodeDefinition) {
        return [
            'node' => $nodeDefinition['nodeField']
        ];
    },
]);

$shipMutation = Relay::mutationWithClientMutationId([
    'name' => 'IntroduceShip',
    'inputFields' => [
        'shipName' => [
            'type' => Type::nonNull(Type::string())
        ],
        'factionId' => [
            'type' => Type::nonNull(Type::id())
        ]
    ],
    'outputFields' => [
        'ship' => [
            'type' => $shipType,
            'resolve' => function ($payload) {
                return StarWarsData::getShip($payload['shipId']);
            }
        ],
        'faction' => [
            'type' => $factionType,
            'resolve' => function ($payload) {
                return StarWarsData::getFaction($payload['factionId']);
            }
        ]
    ],
    'mutateAndGetPayload' => function ($input) {
        $newShip = StarWarsData::createShip($input['shipName'], $input['factionId']);
        return [
            'shipId' => $newShip['id'],
            'factionId' => $input['factionId']
        ];
    }
]);

$mutationType = new ObjectType([
    'name' => 'Mutation',
    'fields' => function () use ($shipMutation) {
        return [
            'introduceShip' => $shipMutation
        ];
    }
]);