GraphQL Bundle for Symfony 2.

Related tags

API GraphQLBundle
Overview

Symfony 2 GraphQl Bundle

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

Installation

1- Require the package via Composer in your composer.json.

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

2- Run Composer to install or update the new requirement.

$ composer install

or

$ composer update

3- Add the service provider to your app/AppKernel.php file

<?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

<?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

<?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

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

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

<?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

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

TODO:

  1. Add the complete documentation
  2. Add validation
You might also like...
Symfony bundle for EventSauce (WIP)

Symfony EventSauce (WIP) This bundle provides the basic and extended container configuration of symfony for the EventSauce library. Before using it, I

Symfony bundle integrating server-sent native notifications
Symfony bundle integrating server-sent native notifications

Symfony UX Notify Symfony UX Notify is a Symfony bundle integrating server-sent native notifications in Symfony applications using Mercure. It is part

Bundle to integrate Tactician with Symfony projects

TacticianBundle Symfony2 Bundle for the Tactician library https://github.com/thephpleague/tactician/ Installation Step 1: Download the Bundle Open a c

A Symfony bundle that provides #StandWithUkraine banner and has some built-in features to block access to your resource for Russian-speaking users.
A Symfony bundle that provides #StandWithUkraine banner and has some built-in features to block access to your resource for Russian-speaking users.

StandWithUkraineBundle На русском? Смотри README.ru.md This bundle provides a built-in StandWithUkraine banner for your Symfony application and has so

OpenAPI(v3) Validators for Symfony http-foundation, using `league/openapi-psr7-validator` and `symfony/psr-http-message-bridge`.

openapi-http-foundation-validator OpenAPI(v3) Validators for Symfony http-foundation, using league/openapi-psr7-validator and symfony/psr-http-message

Fork of Symfony Rate Limiter Component for Symfony 4

Rate Limiter Component Fork (Compatible with Symfony =4.4) The Rate Limiter component provides a Token Bucket implementation to rate limit input and

Enter-to-the-Matrix-with-Symfony-Console - Reproduction of the
Enter-to-the-Matrix-with-Symfony-Console - Reproduction of the "Matrix characterfall" effect with the Symfony Console component.

Enter to the Matrix (with Symfony Console) Reproduction of the "Matrix characterfall" effect with the Symfony Console component. Run Clone the project

Create REST and GraphQL APIs, scaffold Jamstack webapps, stream changes in real-time.
Create REST and GraphQL APIs, scaffold Jamstack webapps, stream changes in real-time.

API Platform is a next-generation web framework designed to easily create API-first projects without compromising extensibility and flexibility: Desig

Laravel wrapper for Facebook's GraphQL

Laravel GraphQL Use Facebook's GraphQL with Laravel 6.0+. It is based on the PHP port of GraphQL reference implementation. You can find more informati

Owner
Sergey Varibrus
Sergey Varibrus
GraphQL Bundle for Symfony 2.

Symfony 2 GraphQl Bundle Use Facebook GraphQL with Symfony 2. This library port laravel-graphql. It is based on the PHP implementation here. Installat

Sergey Varibrus 35 Nov 17, 2022
Monorepo of the PoP project, including: a server-side component model in PHP, a GraphQL server, a GraphQL API plugin for WordPress, and a website builder

PoP PoP is a monorepo containing several projects. The GraphQL API for WordPress plugin GraphQL API for WordPress is a forward-looking and powerful Gr

Leonardo Losoviz 265 Jan 7, 2023
Syntax to query GraphQL through URL params, which grants a GraphQL API the capability to be cached on the server.

Field Query Syntax to query GraphQL through URL params, which grants a GraphQL API the capability to be cached on the server. Install Via Composer com

PoP 4 Jan 7, 2022
Monorepo of the PoP project, including: a server-side component model in PHP, a GraphQL server, a GraphQL API plugin for WordPress, and a website builder

PoP PoP is a monorepo containing several projects. The GraphQL API for WordPress plugin GraphQL API for WordPress is a forward-looking and powerful Gr

Leonardo Losoviz 265 Jan 7, 2023
Airbrake.io & Errbit integration for Symfony 3/4/5. This bundle plugs the Airbrake API client into Symfony project

AmiAirbrakeBundle Airbrake.io & Errbit integration for Symfony 3/4/5. This bundle plugs the Airbrake API client into Symfony project. Prerequisites Th

Anton Minin 8 May 6, 2022
DataTables bundle for Symfony

Symfony DataTables Bundle This bundle provides convenient integration of the popular DataTables jQuery library for realtime Ajax tables in your Symfon

Omines Internetbureau 199 Jan 3, 2023
An Unleash bundle for Symfony applications to provide an easy way to use feature flags

Unleash Bundle An Unleash bundle for Symfony applications. This provide an easy way to implement feature flags using Gitlab Feature Flags Feature. Ins

Stogon 7 Oct 20, 2022
Symfony Health Check Bundle Monitoring Project Status

Symfony Health Check Bundle Version Build Status Code Coverage master develop Installation Step 1: Download the Bundle Open a command console, enter y

MacPaw Inc. 27 Jul 7, 2022
A bundle providing routes and glue code between Symfony and a WOPI connector.

WOPI Bundle A Symfony bundle to facilitate the implementation of the WOPI endpoints and protocol. Description The Web Application Open Platform Interf

Champs-Libres 5 Aug 20, 2022
Mediator - CQRS Symfony bundle. Auto Command/Query routing to corresponding handlers.

Installation $ composer require whsv26/mediator Bundle configuration // config/packages/mediator.php return static function (MediatorConfig $config)

Alexander Sv. 6 Aug 31, 2022