api-skeletons/doctrine-orm-graphql

Doctrine ORM 的 GraphQL 类型驱动器

12.1.0 2024-08-16 23:52 UTC

README

Doctrine ORM 的 GraphQL 类型驱动器

Build Status Code Coverage Scrutinizer Code Quality PHP Version Total Downloads License

这个库为使用 webonyx/graphql-php 库的 Doctrine ORM 提供了一个 GraphQL 驱动器。它 不会 试图重新定义该优秀库的操作方式。相反,它创建在库提供的框架中使用的类型。

安装

通过 composer

composer require api-skeletons/doctrine-orm-graphql

文档

完整文档可在 https://doctrine-orm-graphql.apiskeletons.devdocs 目录中找到。

版本

更多详细信息请参阅 文档

示例

LDOG Stack:Laravel、Doctrine ORM 和 GraphQL 使用此库:https://ldog.apiskeletons.dev

有关工作实现,请参阅 https://graphql.lcdb.org 和相应的应用程序 https://github.com/lcdborg/graphql.lcdb.org

功能

快速入门

向您的 Doctrine 实体添加属性或查看 globalEnable,以在您的模式中的所有实体上启用 GraphQL 而无需属性配置。

use ApiSkeletons\Doctrine\ORM\GraphQL\Attribute as GraphQL;

#[GraphQL\Entity]
class Artist
{
    #[GraphQL\Field]
    public $id;

    #[GraphQL\Field]
    public $name;

    #[GraphQL\Association]
    public $performances;
}

#[GraphQL\Entity]
class Performance
{
    #[GraphQL\Field]
    public $id;

    #[GraphQL\Field]
    public $venue;

    /**
     * Not all fields need attributes.
     * Only add attribues to fields you want available in GraphQL
     */
    public $city;
}

创建驱动器和 GraphQL 模式

use ApiSkeletons\Doctrine\ORM\GraphQL\Driver;
use Doctrine\ORM\EntityManager;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;

$driver = new Driver($entityManager);

$schema = new Schema([
    'query' => new ObjectType([
        'name' => 'query',
        'fields' => [
            'artists' => $driver->completeConnection(Artist::class),
        ],
    ]),
    'mutation' => new ObjectType([
        'name' => 'mutation',
        'fields' => [
            'artistUpdateName' => [
                'type' => $driver->type(Artist::class),
                'args' => [
                    'id' => Type::nonNull(Type::id()),
                    'input' => Type::nonNull($driver->input(Artist::class, ['name'])),
                ],
                'resolve' => function ($root, $args) use ($driver): Artist {
                    $artist = $driver->get(EntityManager::class)
                        ->getRepository(Artist::class)
                        ->find($args['id']);

                    $artist->setName($args['input']['name']);
                    $driver->get(EntityManager::class)->flush();

                    return $artist;
                },
            ],
        ],
    ]),
]);

运行 GraphQL 查询

use GraphQL\GraphQL;

$query = '{
  artists {
    edges {
      node {
        id
        name
        performances {
          edges {
            node {
              venue
            }
          }
        }
      }
    }
  }
}';

$result = GraphQL::executeQuery(
    schema: $schema,
    source: $query,
    variableValues: null,
    operationName: null
);

$output = $result->toArray();

运行 GraphQL 变更

use GraphQL\GraphQL;

$query = '
  mutation ArtistUpdateName($id: Int!, $name: String!) {
    artistUpdateName(id: $id, input: { name: $name }) {
      id
      name
    }
  }
';

$result = GraphQL::executeQuery(
    schema: $schema,
    source: $query,
    variableValues: [
        'id' => 1,
        'name' => 'newName',
    ],
    operationName: 'ArtistUpdateName'
);

$output = $result->toArray();

过滤器

对于每个启用的字段和关联,都提供了查询的过滤器。

示例

{
  artists (
    filter: {
      name: {
        contains: "Dead"
      }
    }
  ) {
    edges {
      node {
        id
        name
        performances (
          filter: {
            venue: {
              eq: "The Fillmore"
            }
          }
        ) {
          edges {
            node {
              venue
            }
          }
        }
      }
    }
  }
}

每个字段都有自己的过滤器集。根据字段类型,以下一些或全部过滤器可用

  • eq - 等于。
  • neq - 不等于。
  • lt - 小于。
  • lte - 小于或等于。
  • gt - 大于。
  • gte - 大于或等于。
  • isnull - 是空。如果值为 true,则该字段必须为 null。如果值为 false,则该字段不能为 null。
  • between - 介于。等同于在同一字段上使用 gte & lte。以 low, high 的形式给出值。
  • in - 存在于数组中。
  • notin - 不存在于数组中。
  • startwith - 在值右侧使用通配符的 like 查询。
  • endswith - 在值左侧使用通配符的 like 查询。
  • contains - like 查询。

您可以从任何实体、关联或全局中 排除任何过滤器

历史

本项目的起源可以追溯到2018年5月,与https://github.com/API-Skeletons/zf-doctrine-graphql有关;该代码是为Zend Framework 2编写的。后来,它迁移到了框架无关的https://packagist.org.cn/packages/api-skeletons/doctrine-graphql,但该存储库的名称不正确,因为它没有仅指定ORM。因此,创建了此存储库,而其他存储库则被废弃。

许可证

请参阅LICENSE