player259/graphql-bundle

Lightweight Symfony GraphQL bundle over webonyx/graphql-php with DI and autowiring

Installs: 12

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 0

Open Issues: 0

Type:symfony-bundle

1.0.1 2021-08-02 10:11 UTC

This package is auto-updated.

Last update: 2021-09-02 10:23:30 UTC


README

About

The Player259GraphQLBundle integrates webonyx/graphql-php library into symfony applications.

CI Status

Usage documentation.

Features

Installation

The Player259GraphQLBundle requires PHP 7.1+ and Symfony 4.4+.

You can install the bundle using Symfony Flex:

$ composer require player259/graphql-bundle

If you're not using Flex, then add the bundle to your config/bundles.php:

// config/bundles.php
return [
    // ...
    Player259\GraphQLBundle\Player259GraphQLBundle::class => ['all' => true],
];

Import routing file:

# in app/config/routes.yaml
player259_graphql:
    resource: '@Player259GraphQLBundle/Resources/config/routing.xml'
    prefix: /

Or assign endpoint to specific url:

# in app/config/routes.yaml
player259_graphql_index:
    path: /graphql
    controller: Player259\GraphQLBundle\Controller\GraphQLController

By default bundle registers /graphql endpoint.

Configuration

Default configuration in config/packages/player259_graphql.yaml.

# in app/config/packages/player259_graphql.yml
player259_graphql:
    debug: '%kernel.debug%'
    logger: '?logger'

With debug option set to true response errors will contain debugMessage and trace.

logger parameter is a service name to log exceptions.
If it's prefixed with ? it will not throw exception if no such service exists.

Example

To create your first GraphQL API (with default Symfony 5 installation and no configuration):

  1. Create class which extends webonyx ObjectType with name Query
  2. Add at least one field and resolver
  3. Make a request to /graphql url
<?php

namespace App\GraphQL;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use Symfony\Component\Security\Core\Security;

class QueryType extends ObjectType
{
    public function __construct()
    {
        $config = [
            'name' => 'Query',
            'fields' => [
                'username' => [
                    'type' => Type::string(),
                    'description' => 'Current User username',
                ],
            ],
        ];

        parent::__construct($config);
    }

    public function resolveUsername(Security $security): ?string
    {
        return $security->getUser() ? $security->getUser()->getUsername() : null;
    }
}

Documentation

Usage examples can be found in documentation.

Not yet implemented

Pass execution rules, disabling introspection, query depth and complexity.

Dispatching events to override server parameters such as promiseAdapter, error formatters and handlers.

Allow to merge non-root types to get more flexibility.

Maybe custom type config property resolveMethod to call specific method or another service.

Another option is annotations, something like @GraphQL\Resolve("App\GraphQL\QueryType", "users") so it could be attached to any service with public method. There will be no autowiring but it can be useful in some cases.

License

Released under the MIT License, see LICENSE.