Skip to content

calvera/graphql

 
 

Repository files navigation

GraphQL

This package is using webonyx/graphql-php as GraphQL driver and is meant to be used as automatic schema definition loader. For better understandation please reffer to efabrica/nette-graphql implementation of this package.

Installation

Via composer

composer require efabrica/graphql

Usage

Schema definition

use Efabrica\GraphQL\Resolvers\ResolverInterface;
use Efabrica\GraphQL\Schema\Definition\Arguments\FieldArgument;
use Efabrica\GraphQL\Schema\Definition\Fields\Field;
use Efabrica\GraphQL\Schema\Definition\Fields\InputObjectField;
use Efabrica\GraphQL\Schema\Definition\ResolveInfo;
use Efabrica\GraphQL\Schema\Definition\Schema;
use Efabrica\GraphQL\Schema\Definition\Types\InputObjectType;
use Efabrica\GraphQL\Schema\Definition\Types\ObjectType;
use Efabrica\GraphQL\Schema\Definition\Types\Scalar\IDType;
use Efabrica\GraphQL\Schema\Definition\Types\Scalar\IntType;
use Efabrica\GraphQL\Schema\Definition\Types\Scalar\StringType;

$userResolver = new class implements ResolverInterface {
    public function __invoke($parentValue, array $args, ResolveInfo $resolveInfo): array
    {
        $limit = $args['pagination']['limit'] ?? null;
        $offset = $args['pagination']['offset'] ?? 0;
        
        $users = [
            [
                'id' => 1,
                'name' => 'John Doe',
                'email' => '[email protected]',
            ],
            [
                'id' => 2,
                'name' => 'Jane Dane',
                'email' => '[email protected]',
            ],
            [
                'id' => 3,
                'name' => 'Moe Lester',
                'email' => '[email protected]',
            ],
        ];
        
        return array_slice($users, $offset, $limit);
    }
};

$userObjectType = (new ObjectType('User'))
    ->setFields([
        new Field('id', new IDType()),
        new Field('name', new StringType()),
        new Field('email', new StringType()),
    ]);

$paginationArgument = new FieldArgument(
    'pagination',
    (new InputObjectType('pagination_argument'))
        ->setFields([
            (new InputObjectField('limit', new IntType()))
                ->setNullable(),
            (new InputObjectField('offset', new IntType()))
                ->setNullable(),
        ])
);

$schema = (new Schema())
    ->setQuery(
        (new ObjectType('Query'))
            ->setFields([
                (new Field('Users', $userObjectType))
                    ->setMulti()
                    ->setArguments([
                        $paginationArgument,
                    ])
                    ->setResolver($userResolver),
            ])
    );

Initialization

use Efabrica\GraphQL\Drivers\WebonyxDriver;
use Efabrica\GraphQL\GraphQL;
use Efabrica\GraphQL\Schema\Loaders\DefinitionSchemaLoader;


$schemaLoader = new DefinitionSchemaLoader($schema);
$driver = new WebonyxDriver($schemaLoader);
$graphql = new GraphQL($driver);

Fetching results

$query = <<<GQL
    {
        Users (
            pagination: {
                limit: 2
                offset: 1
            }
        ) {
            id
            name
            email
        }
    }
    GQL;

$users = $graphql->executeQuery($query);

//array(1) {
//  'Users' =>
//  array(2) {
//    [0] =>
//    array(3) {
//      'id' =>
//      string(1) "2"
//      'name' =>
//      string(9) "Jane Dane"
//      'email' =>
//      string(13) "[email protected]"
//    }
//    [1] =>
//    array(3) {
//      'id' =>
//      string(1) "3"
//      'name' =>
//      string(10) "Moe Lester"
//      'email' =>
//      string(14) "[email protected]"
//    }
//  }
//}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%