2017 © Pedro Peláez
 

library graphql-bundle

Symfony GraphQl Bundle

image

youshido/graphql-bundle

Symfony GraphQl Bundle

  • Thursday, February 22, 2018
  • by portey
  • Repository
  • 15 Watchers
  • 274 Stars
  • 41,129 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 36 Forks
  • 18 Open issues
  • 44 Versions
  • 18 % Grown

The README.md

Symfony GraphQl Bundle

This is a bundle based on the pure PHP GraphQL Server implementation

This bundle provides you with:, (*1)

  • Full compatibility with the RFC Specification for GraphQL
  • Agile object oriented structure to architect your GraphQL Schema
  • Intuitive Type system that allows you to build your project much faster and stay consistent
  • Built-in validation for the GraphQL Schema you develop
  • Well documented classes with a lot of examples
  • Automatically created endpoint /graphql to handle requests

There are simple demo application to demonstrate how we build our API, see GraphQLDemoApp., (*2)

Table of Contents

Installation

We assume you have composer, if you're not – install it from the official website.
If you need any help installing Symfony framework – here's the link http://symfony.com/doc/current/book/installation.html., (*3)

Shortcut to install Symfony: composer create-project symfony/framework-standard-edition my_project_name, (*4)

Once you have your composer up and running – you're ready to install the GraphQL Bundle.
Go to your project folder and run:, (*5)

composer require youshido/graphql-bundle

Then enable bundle in your app/AppKernel.php, (*6)

new Youshido\GraphQLBundle\GraphQLBundle(),

Add the routing reference to the app/config/routing.yml:, (*7)

graphql:
    resource: "@GraphQLBundle/Controller/"

or, (*8)

graphql:
    resource: "@GraphQLBundle/Resources/config/route.xml"

If you don't have a web server configured you can use a bundled version, simply run php bin/console server:run., (*9)

Let's check if you've done everything right so far – try to access url localhost:8000/graphql.
You should get a JSON response with the following error:, (*10)

{"errors":[{"message":"Schema class does not exist"}]}

That's because there was no GraphQL Schema specified for the processor yet. You need to create a GraphQL Schema class and set it inside your app/config/config.yml file., (*11)

There is a way where you can use inline approach and do not create a Schema class, in order to do that you have to define your own GraphQL controller and use a ->setSchema method of the processor to set the Schema., (*12)

The fastest way to create a Schema class is to use a generator shipped with this bundle:, (*13)

php bin/console graphql:configure AppBundle

Here AppBundle is a name of the bundle where the class will be generated in.
You will be requested for a confirmation to create a class., (*14)

After you've added parameters to the config file, try to access the following link in the browser – http://localhost:8000/graphql?query={hello(name:World)}, (*15)

Alternatively, you can execute the same request using CURL client in your console
curl http://localhost:8000/graphql --data "query={ hello(name: \"World\") }", (*16)

Successful response from a test Schema will be displayed:, (*17)

{"data":{"hello":"world!"}}

That means you have GraphQL Bundle for the Symfony Framework configured and now can architect your GraphQL Schema:, (*18)

Next step would be to link assets for GraphiQL Explorer by executing:, (*19)

php bin/console assets:install --symlink

Now you can access it at http://localhost:8000/graphql/explorer, (*20)

Symfony features

Class AbstractContainerAwareField:

AbstractContainerAwareField class used for auto passing container to field, add ability to use container in resolve function:, (*21)

class RootDirField extends AbstractContainerAwareField
{

    /**
     * @inheritdoc
     */
    public function getType()
    {
        return new StringType();
    }

    /**
     * @inheritdoc
     */
    public function resolve($value, array $args, ResolveInfo $info)
    {
        return $this->container->getParameter('kernel.root_dir');
    }

    /**
     * @inheritdoc
     */
    public function getName()
    {
        return 'rootDir';
    }

Service method as callable:

Ability to pass service method as resolve callable:, (*22)

$config->addField(new Field([
    'name'    => 'cacheDir',
    'type'    => new StringType(),
    'resolve' => ['@resolve_service', 'getCacheDir']
]))

Events:

You can use the Symfony Event Dispatcher to get control over specific events which happen when resolving graphql queries., (*23)

namespace ...\...\..;

use Youshido\GraphQL\Event\ResolveEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class MyGraphQLResolveEventSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            'graphql.pre_resolve'  => 'onPreResolve',
            'graphql.post_resolve' => 'onPostResolve'
        ];
    }

    public function onPreResolve(ResolveEvent $event)
    {
        //$event->getFields / $event->getAstFields()..
    }

    public function onPostResolve(ResolveEvent $event)
    {
        //$event->getFields / $event->getAstFields()..
    }
}

Configuration

Now configure you subscriber so events will be caught. This can be done in Symfony by either XML, Yaml or PHP., (*24)

<service id="my_own_bundle.event_subscriber.my_graphql_resolve_event_subscriber" class="...\...\...\MyGraphQLResolveEventSubscriber">
    <tag name="graphql.event_subscriber" />
</service>

Security:

Bundle provides two ways to guard your application: using black/white operation list or using security voter., (*25)

Black/white list

Used to guard some root operations. To enable it you need to write following in your config.yml file:, (*26)

graphql:

  #...

  security:
    black_list: ['hello'] # or white_list: ['hello']

Using security voter:

Used to guard any field resolve and support two types of guards: root operation and any other field resolving (including internal fields, scalar type fields, root operations). To guard root operation with your specified logic you need to enable it in configuration and use SecurityManagerInterface::RESOLVE_ROOT_OPERATION_ATTRIBUTE attribute. The same things need to do to enable field guard, but in this case use SecurityManagerInterface::RESOLVE_FIELD_ATTRIBUTE attribute. Official documentation about voters., (*27)

Note: Enabling field security lead to a significant reduction in performance, (*28)

Config example:, (*29)

graphql:
    security:
        guard:
            field: true # for any field security
            operation: true # for root level security

Voter example (add in to your services.yml file with tag security.voter):, (*30)

use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Youshido\GraphQL\Execution\ResolveInfo;
use Youshido\GraphQLBundle\Security\Manager\SecurityManagerInterface;

class GraphQLVoter extends Voter
{

    /**
     * @inheritdoc
     */
    protected function supports($attribute, $subject)
    {
        return in_array($attribute, [SecurityManagerInterface::RESOLVE_FIELD_ATTRIBUTE, SecurityManagerInterface::RESOLVE_ROOT_OPERATION_ATTRIBUTE]);
    }

    /**
     * @inheritdoc
     */
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
    {
        // your own validation logic here

        if (SecurityManagerInterface::RESOLVE_FIELD_ATTRIBUTE == $attribute) {
            /** @var $subject ResolveInfo */
            if ($subject->getField()->getName() == 'hello') {
                return false;
            }

            return true;
        } elseif (SecurityManagerInterface::RESOLVE_ROOT_OPERATION_ATTRIBUTE == $attribute) {
            /** @var $subject Query */
            if ($subject->getName() == '__schema') {
                return true;
            }
        }
    }
}

GraphiQL extension:

To run graphiql extension just try to access to http://your_domain/graphql/explorer, (*31)

Documentation

All detailed documentation is available on the main GraphQL repository – http://github.com/youshido/graphql/., (*32)

The Versions

22/02 2018

dev-master

9999999-dev

Symfony GraphQl Bundle

  Sources   Download

MIT proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

20/10 2017

v1.4

1.4.0.0

Symfony GraphQl Bundle

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

17/06 2017

v1.3.4

1.3.4.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

09/03 2017

v1.3.2

1.3.2.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

09/03 2017

v1.3.3

1.3.3.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

01/03 2017

v1.3.1

1.3.1.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

01/03 2017

v1.3

1.3.0.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

28/02 2017

2.0.x-dev

2.0.9999999.9999999-dev

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

17/02 2017

v1.2.4

1.2.4.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

17/02 2017

v1.2.3

1.2.3.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

11/01 2017

v1.2.2

1.2.2.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

09/11 2016

v1.2.1

1.2.1.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

09/11 2016

v1.2

1.2.0.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

08/11 2016

dev-develop

dev-develop

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

01/11 2016

v1.1.5

1.1.5.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

10/10 2016

v1.1.4

1.1.4.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

05/10 2016

v1.1.3

1.1.3.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

30/09 2016

v1.1.2.2

1.1.2.2

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

24/09 2016

v1.1.2.1

1.1.2.1

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

24/09 2016

v1.1.2

1.1.2.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

23/09 2016

v1.1.1

1.1.1.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

16/09 2016

v1.1.0

1.1.0.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

11/09 2016

v1.0.10

1.0.10.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

06/09 2016

v1.0.9

1.0.9.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

05/09 2016

v1.0.8

1.0.8.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

03/09 2016

v1.0.7

1.0.7.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

03/09 2016

v1.0.6

1.0.6.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

03/09 2016

v1.0.5

1.0.5.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

26/08 2016

v1.0.4

1.0.4.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

25/08 2016

v1.0.3

1.0.3.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

04/07 2016

v1.0.2

1.0.2.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

29/06 2016

v1.0.1

1.0.1.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

02/06 2016

v1.0.0

1.0.0.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

10/05 2016

v0.1.3

0.1.3.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

09/05 2016

v0.1.2

0.1.2.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

09/05 2016

v0.1.1

0.1.1.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

08/05 2016

v0.1

0.1.0.0

Symfony GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Alexandr Viniychuk
by Portey Vasil

18/02 2016

v0.0.7

0.0.7.0

Symfony2 GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Portey Vasil

18/02 2016

v0.0.6

0.0.6.0

Symfony2 GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Portey Vasil

17/02 2016

v0.0.5

0.0.5.0

Symfony2 GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Portey Vasil

01/02 2016

v0.0.4

0.0.4.0

Symfony2 GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Portey Vasil

01/02 2016

v0.0.3

0.0.3.0

Symfony2 GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Portey Vasil

22/01 2016

v0.0.2

0.0.2.0

Symfony2 GraphQl Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Portey Vasil

14/01 2016

v0.0.1

0.0.1.0

Symfony2 GraphQl Bundle

  Sources   Download

proprietary

The Development Requires

by Portey Vasil