axtiva/ flexible-graphql-php
Schema SDL 首选库,用于将 GraphQL SDL 生成 PHP 代码到 TypeRegistry,使用 webonyx/graphql-php
v2.2.0
2024-07-18 13:50 UTC
Requires
- php: ^7.4 | ^8.0
- nikic/php-parser: ^4.12 | ^5
- webonyx/graphql-php: ^15.2
Requires (Dev)
- phpunit/phpunit: ^9.5
- psr/container: ^1 | ^2
This package is auto-updated.
Last update: 2024-09-18 14:25:44 UTC
README
Schema 首选库,用于将 GraphQL SDL 生成 PHP 代码到 TypeRegistry,支持与 webonyx/graphql-php 简单集成到任何控制器。
TL;DR
轻松将 GraphQL 集成到任何项目,您需要的只是控制器。快速开始示例
特性
- Schema/SDL 首选代码生成(查看 示例目录)
- 支持 webonyx/graphql-php 的所有功能
- 可执行指令
- Apollo Federation/Federation2 支持
- 流行框架集成
- Symfony axtiva/flexible-graphql-bundle
设置
Composer 安装
composer require axtiva/flexible-graphql-php
示例
- 通过 Graphql Schema 中的类型生成模型 example/generate_type_models.php
- 通过 Graphql Schema 生成自定义标量解析器 example/generate_scalar_resolver.php
- 通过 Graphql Schema 生成指令解析器 example/generate_directive_resolver.php
- 通过 Graphql Schema 生成类型字段解析器 example/generate_field_resolver.php
- 创建懒加载的 TypeRegistry example/generate_type_registry.php
- 使用懒加载的 TypeRegistry 设置 graphql 请求处理器 example/start_graphql_server.php
演示
启动开发服务器以测试 http Graphql 请求
php -S localhost:8080 example/start_graphql_server.php
更改 schema 并运行示例命令
从 schema 和解析器映射更新 TypeRegistry 和模型类
php example/generate_code.php
如果您需要创建字段解析器,请从模型 CodedCurrencyType 中移除 AutoGenerationInterface
示例
- final class CodedCurrencyType implements AutoGenerationInterface, NodeInterface, CurrencyInterface + final class CodedCurrencyType implements NodeInterface, CurrencyInterface { public string $id; - public int $code; }
并运行 php example/generate_code.php
,之后您将在 example/Resolver/CodedCurrency/CodeResolver.php 中找到它。这是您的字段解析器,在 example/start_graphql_server.php 中像 PsrContainerExample 一样定义它
$container = new PsrContainerExample([ \Axtiva\FlexibleGraphql\Example\GraphQL\Resolver\Query\AccountResolver::class => new \Axtiva\FlexibleGraphql\Example\GraphQL\Resolver\Query\AccountResolver, + \Axtiva\FlexibleGraphql\Example\GraphQL\Resolver\CodedCurrency\CodeResolver::class => + new \Axtiva\FlexibleGraphql\Example\GraphQL\Resolver\CodedCurrency\CodeResolver, ]);
运行演示应用程序 php -S localhost:8080 example/start_graphql_server.php
并在查询中尝试请求 CodedCurrency.code 字段
测试
运行测试
php vendor/bin/phpunit