t3n / graphql-apollofederation

此包最新版本(0.2.0)的许可证信息不可用。

t3n.GraphQL 的 Apollo Federation 规范实现

安装次数: 4,136

依赖项: 0

建议者: 0

安全性: 0

星标: 3

关注者: 8

分支: 0

开放问题: 0

类型:neos-package

0.2.0 2019-12-23 06:53 UTC

This package is auto-updated.

Last update: 2024-09-23 17:11:28 UTC


README

用于在您的 GraphQL 模式中实现 Apollo Federation 规范的 t3n.GraphQL 的边车包。

通过 composer 安装它

composer require "t3n/graphql-apollofederation"

注意:此包仍在开发中,其实现可能发生变化

关于 Apollo Federation

Apollo Federation 是一种将多个 GraphQL 服务组合成一个单一图架构。此包提供所有功能,以在您的端点实现规范,以便您可以使用 Apollo 的工具使用您的服务。

调整

为了实现规范,您需要对现有模式进行一些小的调整。您可以在此处查看示例实现

添加 Federation Spec 模式

  1. 将 GraphQL 规范添加到您的模式中
t3n:
  GraphQL:
    endpoints:
      'your-endpoint':
        schemas:
          federationSpec: # make sure to start this key with "federation"
            typeDefs: 'resource://t3n.GraphQL.ApolloFederation/Private/GraphQL/federation-schema.graphql'
              resolvers:
                _Entity: 't3n\GraphQL\ApolloFederation\Resolver\EntityResolver'
                _Service: 't3n\GraphQL\ApolloFederation\Resolver\ServiceResolver'
  1. ServiceQueryTrait 添加到您的 Query-Resolver 中
<?php

declare(strict_types=1);

namespace Some\Vendor\Namespace\Resolver;

use t3n\GraphQL\ApolloFederation\Resolver\ServiceQueryTrait;
use t3n\GraphQL\ResolverInterface;

class QueryResolver implements ResolverInterface
{
    use ServiceQueryTrait;

    // [...]
}
  1. 向您的模式中添加一个新的实体联合

您需要使每个实体成为 _Entity 联合的一部分。为此,向您的端点添加一个新的模式文件,该文件定义了联合

# Resources/Private/GraphQL/federation.graphql
union _Entity = User | Product | _allOfYourEntities_
t3n:
  GraphQL:
    endpoints:
      'your-endpoint':
        schemas:
          federationEntity: # make sure to start this key with "federation"
            typeDefs: 'resource://Your.Package/Private/GraphQL/federation.graphql'
  1. EntitiesQueryTrait 添加到您的查询解析器中

如果没有实体,此步骤是可选的。

联邦规范需要一个名为 _entities 的新查询。将特性添加到所有的 QueryResolver

<?php

declare(strict_types=1);

namespace Some\Vendor\Namespace\Resolver;

use t3n\GraphQL\ApolloFederation\Resolver\EntitiesQueryTrait;
use t3n\GraphQL\ResolverInterface;

class QueryResolver implements ResolverInterface
{
    use EntitiesQueryTrait;

    // [...]
}

还将 _entities 添加到您的查询类型中

type Query @extends {
  _entities(representations: [_Any!]!): [_Entity]!
}
  1. 实现 EntityResolverInterface

一旦调用 _entities 查询,Apollo Gateway 服务器将发送查询,如:

query($representations: [_Any!]!) {
  _entities(representations: $representations) {
    ... on User {
      name
    }
  }
}

根据表示输入,将实例化具体的解析器。因此,所有解析实体的解析器必须实现 EntityResolverInterface

    # Return the typename of your Entity
    public function __resolveType(): string;

    /**
     * This method actually has to resolve your object/array that represents your entity
     *
     * @param array $variables those variables are passed down via the representation array
     */
    public function __resolveEntity(array $variables);
  1. 如有需要,实现 GraphQLTypeAwareInterface

您实际解析并返回的、属于 _Entity 联合类型的数据必须知道其类型名称。如果是对象,您必须实现 GraphQLTypeAwareInterface。如果您的数据是数组,则必须在第一级上有一个键 __typename