api-skeletons / doctrine-orm-graphql
Doctrine ORM 的 GraphQL 类型驱动器
Requires
- php: ^8.1
- doctrine/doctrine-laminas-hydrator: ^3.2
- doctrine/orm: ^2.18 || ^3.0
- league/event: ^3.0
- psr/container: ^1.1 || ^2.0
- webonyx/graphql-php: ^v15.0
Requires (Dev)
- doctrine/coding-standard: ^11.0 || ^12.0
- doctrine/dbal: ^3.1
- php-parallel-lint/php-parallel-lint: ^1.3.2
- phpunit/phpunit: ^9.6
- symfony/cache: ^5.3||^6.2
- vimeo/psalm: ^5.4
Suggests
- ramsey/uuid-doctrine: Support for an UUID Doctrine type
- 12.2.x-dev
- 12.1.x-dev
- 12.1.0
- 12.0.x-dev
- 12.0.1
- 12.0.0
- 11.0.x-dev
- 11.0.2
- 11.0.1
- 11.0.0
- 10.2.x-dev
- 10.2.3
- 10.2.2
- 10.2.1
- 10.2.0
- 10.1.x-dev
- 10.1.2
- 10.1.1
- 10.1.0
- 10.0.x-dev
- 10.0.5
- 10.0.4
- 10.0.3
- 10.0.2
- 10.0.1
- 10.0.0
- 9.1.x-dev
- 9.1.2
- 9.1.1
- 9.1.0
- 9.0.3
- 9.0.2
- 9.0.1
- 9.0.0
- 8.3.3
- 8.3.2
- 8.3.1
- 8.3.0
- 8.2.0
- 8.1.5
- 8.1.4
This package is auto-updated.
Last update: 2024-09-18 00:06:26 UTC
README
Doctrine ORM 的 GraphQL 类型驱动器
这个库为使用 webonyx/graphql-php 库的 Doctrine ORM 提供了一个 GraphQL 驱动器。它 不会 试图重新定义该优秀库的操作方式。相反,它创建在库提供的框架中使用的类型。
安装
通过 composer
composer require api-skeletons/doctrine-orm-graphql
文档
完整文档可在 https://doctrine-orm-graphql.apiskeletons.dev 或 docs 目录中找到。
版本
- 12.x - 支持 league/event 版本 3.0 并符合 PSR-14 标准
- 11.x - 支持 league/event 版本 2.2
更多详细信息请参阅 文档。
示例
LDOG Stack:Laravel、Doctrine ORM 和 GraphQL 使用此库:https://ldog.apiskeletons.dev
有关工作实现,请参阅 https://graphql.lcdb.org 和相应的应用程序 https://github.com/lcdborg/graphql.lcdb.org。
功能
- 支持所有 Doctrine 类型 并允许自定义类型
- 使用 GraphQL Complete Connection Model 进行分页
- 子集合过滤
- 事件 用于修改查询、实体类型等
- 支持多个配置组
快速入门
向您的 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。