flownative/graphql

Last activity 15 days ago
  • 1
  • 14
composer require flownative/graphql

Flownative GraphQL

v0.4.0

Flownative GraphQL

Version
v0.4.0
Type
neos-package
Release Date
Feb 17, 2021 8:03:19 a.m.
Source
git
Distribution
zip
Requires
Requires (dev)
None
Suggest
None
Provides
None
Conflicts
None
Replaces
None
MIT 57ee3f32b2123f782c61521f0e1625a619573df7

MIT license Packagist CI

GraphQL Library for Neos Flow

This Flow package provides a minimal wrapper around the webonyx/graphql-php package. Rather than providing a big range of features or automagic code analysis, this library only solves the bare necessary tasks and gets not into the way between your implementation and the original GraphQL library.

Feature Overview

tbd.

Example

Here's a minimal Hello-World-example:

Classes/GraphQL/QueryResolver.php:

<?php
declare(strict_types=1);
namespace Flownative\Example\GraphQL;

final class QueryResolver
{
    /**
     * @param $_
     * @param array $arguments
     * @return array
     */
    public function ping($_, array $arguments): array
    {
        return [
            'pong' => time()
        ];
    }
}

Resources/Private/GraphQL/schema.graphql:

type Query {
    ping: String
}

Classes/GraphQL/Endpoint.php:

<?php
declare(strict_types=1);
namespace Flownative\Example\GraphQL;

use Flownative\GraphQL\EndpointInterface;
use GraphQL\Executor\ExecutionResult;
use GraphQL\GraphQL;
use GraphQL\Type\Schema;

final class Endpoint implements EndpointInterface
{
    /**
     * @var QueryResolver
     */
    protected $queryResolver;

    /**
     * @param QueryResolver $queryResolver
     */
    public function __construct(QueryResolver $queryResolver)
    {
        $this->queryResolver = $queryResolver;
    }

    /**
     * @return string
     */
    public static function getPath(): string
    {
        return '/api/graphql';
    }

    /**
     * @return string
     */
    public function getSchemaUri(): string
    {
        return 'resource://Flownative.Example/Private/GraphQL/schema.graphql';
    }

    /**
     * @param Schema $schema
     * @param array $input
     * @return ExecutionResult
     */
    public function executeQuery(Schema $schema, array $input): ExecutionResult
    {
        return GraphQL::executeQuery(
            $schema,
            $input['query'],
            $this->getRootValue(),
            null,
            $input['variables'] ?? null,
        );
    }

    /**
     * @return mixed
     */
    private function getRootValue(): array
    {
        $queryResolver = $this->queryResolver;
        return [
            'ping' => static function ($rootValue, array $args, $context) use ($queryResolver) {
                return $queryResolver->ping($rootValue, $args);
            }
        ];
    }

    /**
     * @return callable|null
     */
    public function getTypeConfigDecorator(): ?callable
    {
        return null;
    }
}

Credits and Support

This library was developed by Robert Lemke / Flownative. Feel free to suggest new features, report bugs or provide bug fixes in our Github project.

The content of the readme is provided by Github
The same vendor provides 39 package(s).