PHP code example of suribit / graphql-bundle

1. Go to this page and download the library: Download suribit/graphql-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

suribit / graphql-bundle example snippets



// app/AppKernel.php

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

        // ...
    }

    // ...
}



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'
            ]
        ];
    }
}




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
        ];
    }
}



// ...

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;
    }
}