定制 maiscraft/graphql-hyperf 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

maiscraft/graphql-hyperf

Composer 安装命令:

composer require maiscraft/graphql-hyperf

包简介

Hyperf adapter for Maiscraft GraphQL - Auto-wiring, PSR container bridge, HTTP controller, and configuration provider

README 文档

README

maiscraft/graphql 的 Hyperf 框架适配包,提供自动装配、PSR 容器桥接、HTTP 控制器和配置发布。

功能

  • ConfigProvider:自动注册 GraphQLEngine 及所有依赖到 Hyperf DI 容器
  • HyperfContainer:PSR-11 容器桥接,将 Hyperf DI 适配为 Maiscraft\GraphQL\Contract\ContainerInterface
  • GraphQLController:处理 POST /graphqlGET /graphqlGET /playground 请求
  • 适配器:Cache、Logger、EventDispatcher、Validator、RateLimiter 全部桥接到 Hyperf 对应组件
  • 配置发布php bin/hyperf.php vendor:publish maiscraft/graphql-hyperf

安装

composer require maiscraft/graphql
composer require maiscraft/graphql-hyperf

配置

安装后发布配置文件:

php bin/hyperf.php vendor:publish maiscraft/graphql-hyperf

生成 config/autoload/graphql.php

return [
    'debug' => env('GRAPHQL_DEBUG', true),

    // 显式指定 Resolver 类
    'sources' => [
        // App\GraphQL\ArticleResolver::class,
    ],

    // 目录路径扫描(从文件内容解析 namespace 获取类名)
    'scan_paths' => [
        BASE_PATH . '/app',
    ],

    'security' => [
        'max_query_depth' => 15,
        'max_query_complexity' => 1000,
    ],

    'rate_limit' => [
        'max_attempts' => 100,
        'decay_seconds' => 60,
    ],
];

使用

定义 Entity、Enum、DTO、Resolver 后(参见 maiscraft/graphql),Hyperf 自动发现并构建 Schema:

namespace App\GraphQL;

use App\Domain\Entity\Article;
use Maiscraft\GraphQL\Annotation\Query;
use Maiscraft\GraphQL\Annotation\Arg;
use Maiscraft\GraphQL\Contract\ResolverInterface;

class ArticleResolver implements ResolverInterface
{
    public function __construct(
        private ArticleRepositoryInterface $repository
    ) {}

    #[Query(description: 'Get an article')]
    public function article(#[Arg(type: 'ID!')] string $id): ?Article
    {
        return $this->repository->findById((int) $id);
    }
}

查询:

curl -X POST http://localhost:9501/graphql \
  -H "Content-Type: application/json" \
  -d '{"query": "{ article(id: \"1\") { id title } }"}'

API 端点

方法 路径 说明
POST /graphql 执行 GraphQL 查询
GET /graphql Introspection 查询
GET /playground GraphQL Playground 调试界面

项目结构

src/
├── ConfigProvider.php        # Hyperf 服务注册
├── Contract/                 # Hyperf 组件适配器
│   ├── HyperfCache.php
│   ├── HyperfContainer.php
│   ├── HyperfEventDispatcher.php
│   ├── HyperfLogger.php
│   ├── HyperfRateLimiter.php
│   └── HyperfValidator.php
├── Controller/
│   └── GraphQLController.php # HTTP 控制器
publish/
└── graphql.php               # 配置模板

依赖

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-21