axtiva/flexible-graphql-php

Schema SDL 首选库,用于将 GraphQL SDL 生成 PHP 代码到 TypeRegistry,使用 webonyx/graphql-php

v2.2.0 2024-07-18 13:50 UTC

README

Schema 首选库,用于将 GraphQL SDL 生成 PHP 代码到 TypeRegistry,支持与 webonyx/graphql-php 简单集成到任何控制器。

TL;DR

轻松将 GraphQL 集成到任何项目,您需要的只是控制器。快速开始示例

特性

设置

Composer 安装

composer require axtiva/flexible-graphql-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