dpauli/graphql-request-builder 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

dpauli/graphql-request-builder

最新稳定版本:1.8.0

Composer 安装命令:

composer require dpauli/graphql-request-builder

包简介

Request Builder for GraphQL payload

README 文档

README

Latest Stable Version Total Downloads License UnitTests

This library builds a request for sending to a GraphQL server.

What is GraphQL?

GraphQL is a query language to easy request data from remote web servers. There are several pros for using GraphQL instead of REST like

  • decreasing request amount
  • saving traffic in payload
  • avoid backend changes on changing client requested data

For a full description see How to GraphQL.

Naming conventions

The schema of GraphQL is defined by two easy attributes:

  • Types
  • Arguments

Type define a structured requested data object. Arguments can define these types.

Example

A type forum can have posts, which has authors and a title. If you want to receive all author ant post title information as response your request can look like this:

{
  forum {
    posts {
      authors,
      title
    }
  }
}

To specify your requested data for crawling only the last 5 posts you can modify your request like this:

{
  forum {
    posts(last: 5) {
      authors,
      title
    }
  }
}

GraphQL requested data can complex as you want:

{
  forum {
    posts(last: 5) {
      authors(registration: {date: "2019-08-08"}, visible: true) {
        surname,
        prename(startingWith: "a"),
        birthday
      },
      title
    },
    users(last: 10, sort: "registrationDate", order: "DESC")
  }
}

Why this library?

This library helps building this payload structure without any string concatenation or other strange ideas.

Example

To create following request payload

{
  field {
    search(criteria: {start: "2019-08-23"}) {
      errors {
        code
        type
        description
      }
      id
    }
  }
}

you need to execute following PHP code

<?php
declare(strict_types=1);

use GraphQL\RequestBuilder\Argument;
use GraphQL\RequestBuilder\RootType;
use GraphQL\RequestBuilder\Type;

$searchType = (new Type('search'))
    ->addArgument(new Argument('criteria', new Argument('start', '2019-08-23')))
    ->addSubTypes([
        (new Type('errors'))->addSubTypes(['code', 'type', 'description']),
        'id'
    ]
);

echo (string) (new RootType('field'))->addSubType($searchType);

Build complex types

Its also possible to build complex types. This code examples show you how to do this.

Arguments with array of arguments

Sometimes you want to have arrays with complex Argument types, like the following example.

{
  persons: [
    {age: 30},
    {age: 20},
    {age: 12}
  ]
}

For this concept you can use the class ArrayArgument which give the possibility to add Arguments to an array.

<?php
declare(strict_types=1);

use GraphQL\RequestBuilder\Argument;
use GraphQL\RequestBuilder\ArrayArgument;

$persons = new ArrayArgument(
    'persons',
    [new Argument('age', 30), new Argument('age', 20), new Argument('age', 12)]
);

Arrays with more than one Argument in value.

The example above works if you have an array with only one Argument: every person only has one Argument, the age. If you want to have more Arguments you need to create an Argumentwith an empty name.

{
  persons: [
    {
      name: "Hans",
      age: 30
    },
    {
      name: "Max",
      age: 20
    }
  ]
}

Your PHP code should look like this:

<?php
declare(strict_types=1);

use GraphQL\RequestBuilder\Argument;
use GraphQL\RequestBuilder\ArrayArgument;

$person1 = new ArrayArgument('', [new Argument('name', 'Hans'), new Argument('age', 30)]);
$person2 = new ArrayArgument('', [new Argument('name', 'Max'), new Argument('age', 20)]);

$persons = new ArrayArgument('persons', [$person1, $person2]);

Enum arguments

To create enum arguments it should look like this:

<?php
declare(strict_types=1);

use GraphQL\RequestBuilder\EnumArgument;

$person1 = new EnumArgument('EnumAttribute', 'EnumValue');

dpauli/graphql-request-builder 适用场景与选型建议

dpauli/graphql-request-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 74.63k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 dpauli/graphql-request-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dpauli/graphql-request-builder 我们能提供哪些服务?
定制开发 / 二次开发

基于 dpauli/graphql-request-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04