2017 © Pedro Peláez
 

library graphql-bundle

Symfony2 GraphQl Bundle used graphql-php

image

suribit/graphql-bundle

Symfony2 GraphQl Bundle used graphql-php

  • Saturday, February 13, 2016
  • by suribit
  • Repository
  • 2 Watchers
  • 37 Stars
  • 64 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Symfony 2 GraphQl Bundle

Use Facebook GraphQL with Symfony 2. This library port laravel-graphql. It is based on the PHP implementation here., (*1)

Installation

1- Require the package via Composer in your composer.json., (*2)

{
    "require": {
        "suribit/graphql-bundle": "*"
    }
}

2- Run Composer to install or update the new requirement., (*3)

$ composer install

or, (*4)

$ composer update

3- Add the service provider to your app/AppKernel.php file, (*5)

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Suribit\GraphQLBundle\GraphQLBundle(),
        );

        // ...
    }

    // ...
}

4- Create Type src/path your bundle/Types/Country.php, (*6)

<?php

namespace Lgck\GraphQlBundle\Types;

use GraphQL\Type\Definition\Type as TypeBase;
use Suribit\GraphQLBundle\Support\Type;

class Country extends Type
{
    protected $attributes = [
        'name' => 'Country',
        'description' => 'A Country'
    ];

    public function fields()
    {
        return [
            'id' => [
                'type' => TypeBase::nonNull(TypeBase::int()),
                'description' => 'The id of the country'
            ],
            'name' => [
                'type' => TypeBase::string(),
                'description' => 'The name of country'
            ],
            'status' => [
                'type' => TypeBase::int(),
                'description' => 'The status of country'
            ]
        ];
    }
}

5- Create Query src/path your bundle/Queries/Country.php, (*7)

<?php

namespace Lgck\GraphQlBundle\Queries;

use GraphQL\Type\Definition\Type;
use Suribit\GraphQLBundle\Support\Query;

class Country extends Query
{
    protected $attributes = [
        'name' => 'Country query'
    ];

    public function type()
    {
        return $this->manager->type('country');
    }

    public function args()
    {
        return [
            'id' => ['name' => 'id', 'type' => Type::int()],
        ];
    }

    public function resolve($root, $args)
    {
        $em = $this->manager->em;   // Doctrine Entity Manager
        return [
            'id' => `,
            'name' => 'Russia',
            'status' => 1
        ];
    }
}

6- Create config for graphql schema src/path your bundle/Resources/config/graphql.yml, (*8)

types:
  country: 'Lgck\GraphQlBundle\Types\Country'

schema:
  query:
    country: 'Lgck\GraphQlBundle\Queries\Country'

  mutation: []

7- Edit the file src/path your bundle/Resources/config/services.yml, (*9)

services:
    lgck_graph_ql.mapping.driver.yaml:
        public: true
        class: Suribit\GraphQLBundle\ConfigDrivers\Yaml\YamlDriver
        arguments:
            - "%kernel.root_dir%/../src/path your bundle/Resources/config/graphql.yml"

    lgck_graph_ql.manager:
        class: Suribit\GraphQLBundle\GraphQL
        arguments:
            - @doctrine.orm.entity_manager
            - @lgck_graph_ql.mapping.driver.yaml

8- Create a controller that will be the starting point for processing the request, (*10)

<?php

// ...

class MainController extends Controller
{
    public function queryAction(Request $request)
    {
        $manager = $this->get('lgck_graph_ql.manager');
        $query = $request->request->get('query');
        try {
            $data = $manager->query($query);
        } catch (QueryException $e) {
            $response = new JsonResponse($e->getErrors(), 500);
            return $response;
        }

        $response = new JsonResponse($data);
        return $response;
    }
}    

9- Now it is possible to send a data request, (*11)

query FooBar {
  country(id: 1) {
    id, 
    name, 
    status 
  }
}

TODO: 1. Add the complete documentation 2. Add validation, (*12)

The Versions

13/02 2016

dev-master

9999999-dev https://github.com/suribit/GraphQLBundle

Symfony2 GraphQl Bundle used graphql-php

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sergei Waribrus

graphql

02/02 2016

v0.0.1

0.0.1.0 https://github.com/suribit/GraphQLBundle

Symfony2 GraphQl Bundle used graphql-php

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sergei Waribrus

graphql