suribit/graphql-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

suribit/graphql-bundle

Composer 安装命令:

composer require suribit/graphql-bundle

包简介

Symfony2 GraphQl Bundle used graphql-php

关键字:

README 文档

README

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

统计信息

  • 总下载量: 79
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 35
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 35
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-02-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固