suribit/graphql-bundle

Symfony2 GraphQl Bundle 使用 graphql-php

v0.0.1 2016-02-02 16:01 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:25:29 UTC


README

使用 Facebook GraphQL 与 Symfony 2。此库移植自 laravel-graphql。它基于此处的 PHP 实现 here

安装

1- 在您的 composer.json 中通过 Composer 需求此包。

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

2- 运行 Composer 以安装或更新新的需求。

$ composer install

或者

$ composer update

3- 将服务提供者添加到您的 app/AppKernel.php 文件中

<?php
// app/AppKernel.php

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

        // ...
    }

    // ...
}

4- 创建类型 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- 创建查询 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- 为 GraphQL 模式创建配置 src/path your bundle/Resources/config/graphql.yml

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

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

  mutation: []

7- 编辑文件 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- 创建一个控制器,作为处理请求的起点

<?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- 现在可以发送数据请求

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

待办事项

  1. 添加完整的文档
  2. 添加验证