phps-cans/harmony-graphql-tool

此包包含工具,可以轻松地将graphql集成到您的应用程序中

v0.1.0 2017-09-13 13:34 UTC

This package is not auto-updated.

Last update: 2024-09-15 04:31:49 UTC


README

此包旨在在使用 webonyx的Graphql实现 时简化使用 PSR 11容器。为了简化使用,您必须使用 ServiceProvider

它提供

  • 类型注册表。这是用于注册类型(而不是查询)。它做什么?它会询问容器是否已注册请求的对象类型,如果没有,则使用请求的类型名称作为类名(如果存在,则在类型请求的末尾删除“Type”)。如果类型具有使用其名称的类,它会检查该类是否实现了 GraphqlTypeInterface,如果是,则使用此类型。
  • GraphqlQueryInterface:所有查询对象都应实现此接口才能注册到模式中
  • GraphqlTypeInterface:所有类型对象都应实现此接口才能被类型注册表发现
  • DefaultServiceProvider:一个服务提供商,可以自动创建graphql服务器/模式

如何使用

使用ServiceProvider

您必须在 PsCs\Harmony\Graphql\Tool\GraphqlQueryInterface 下注册您的查询对象实例的标识符名称列表(实现 \PsCs\Harmony\Graphql\Tool\GraphqlQueryInterface)。例如

  • 首先定义您的TypeClass
namespace Foo;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;

class MyType implements \PsCs\Harmony\Graphql\Tool\GraphqlTypeInterface {
    public $id = "foo";
    public function getId() {
        return $this->id;
    }
    static public function resolveField(MyType $value, $args, $context, ResolveInfo $info)  {
        switch ($info->fieldName) {
            case 'id':
              return $bill->getId();
            default:
              return null;
        }
    }
    static public function getType($typeRegistry): ObjectType {
        return new ObjectType([
            "name" => "MyType",
            "fields" => [
                "id" => Type::nonNull(Type::id())
            ],
            "resolveField" => [self::class, "resolveField"]
        ]);
    }
  }
  • 其次,创建您的查询对象
namespace Foo;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\FieldDefinition;

class MyGraphqlQueryInterfaceImplementation extends MydataFinder implements \PsCs\Harmony\Graphql\Tool\GraphqlQueryInterface {

    public function getQueryField($typeRegistry): FieldDefinition {
        $that = $this;
        return FieldDefinition::create([
            "name" => "billPerYear",
            "type" => $typeRegistry->get((MyType::class)."Type"),
            'args'    => [
                'id' => Type::nonNull(Type::id())
            ],
            "resolve" => function($rootValue, $args) use ($that) {
                return $that->findOneById($args["id"]);
            }
        ]);
    }
  }

或者,您也可以创建一个返回多个查询的对象

namespace Foo;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\FieldDefinition;

class MyGraphqlQueriesInterfaceImplementation extends MydataFinder implements \PsCs\Harmony\Graphql\Tool\GraphqlQueriesInterface {

    public function getQueryField($typeRegistry): array {
        $that = $this;
        return [FieldDefinition::create([
            "name" => "billPerYear",
            "type" => $typeRegistry->get((MyType::class)."Type"),
            'args'    => [
                'id' => Type::nonNull(Type::id())
            ],
            "resolve" => function($rootValue, $args) use ($that) {
                return $that->findOneById($args["id"]);
            }
        ])];
    }
  }
  • 然后创建您的serviceProvider
namespace Foo\ServiceProvider;

use Interop\Container\ServiceProvider;
use GraphQL\Type\Schema;

use Foo\MyGraphqlQueryInterfaceImplementation;
use Interop\Container\ContainerInterface as Container;

class Graphql implements ServiceProvider {
 public function getServices()
    {
        return [
            (MyGraphqlQueryInterfaceImplementation::class)."Type" => [self::class, 'getMyGraphqlQueryInterfaceImplementation'],
            (\PsCs\Harmony\Graphql\Tool\GraphqlQueryInterface::class) => [self::class, 'getGraphqlQueryQueue'],
        ]; // By convention
    }
    public static function getMyGraphqlQueryInterfaceImplementation(Container $container) {
        return new MyGraphqlQueryInterfaceImplementation();
    }

    public static function getGraphqlQueryQueue(Container $container) {
        return [
            (MyGraphqlQueryInterfaceImplementation::class)."Type"
        ];
    }
}

然后注册所需的服务提供商

    $container = require_once("container.php");
    $container->register(new \PsCs\Harmony\Graphql\Tool\ServiceProvider\DefaultServiceProvider());
    $container->register(new \Foo\ServiceProvider\Graphql);
    $standardServer = $container->get(\GraphQL\Server\StandardServer::class);
    // ....

不使用服务提供商

此包试图通过服务提供商简化库的使用。如果您不使用服务提供商,此包无法自动注册实例。您仍然可以使用 Registry

    $container = require_once("container.php");
    $registry = new \PsCs\Harmony\Graphql\Tool\Registry\Registry($container);
    $registry->get(MyTypeFoo::class);
    //...

待办事项

实现使用doctrine注解的类型发现