mathsgod/array_to_gql
Composer 安装命令:
composer require mathsgod/array_to_gql
包简介
A PHP library for converting PHP arrays to GraphQL query syntax with support for parameters, aliases, and nested structures
关键字:
README 文档
README
A PHP library for converting PHP arrays to GraphQL query syntax.
Features
- 🔧 Basic Queries: Support for simple field selection
- 📊 Nested Queries: Support for multi-level nested array structures
- 🎯 Parameterized Queries: Use
__argsto add query parameters - 🏷️ Alias Queries: Use
__aliasForto create GraphQL aliases - 🎨 Object Parameters: Support for complex nested object parameters
- ✅ Boolean Fields:
truevalues display as field selection,falsevalues are ignored - 🎯 Value Processing: All non-
falsevalues (strings, numbers,true) only display key names, not values - 🔒 Character Escaping: Automatic handling of special character escaping
Installation
composer require mathsgod/array_to_gql
Usage
Basic Usage
require 'function.php'; // Simple query - all non-false values only show key names $result = array_to_gql([ 'user' => [ 'id' => 1, // Number value → only show key name 'name' => 'John', // String value → only show key name 'email' => true, // true value → show key name 'phone' => false // false value → ignored ] ]); // Output: user { id name email }
Value Processing Rules
// Value processing rules example $result = array_to_gql([ 'users' => [ 'name' => [ 'first' => 'John', // String → only show first 'last' => 'Doe' // String → only show last ], 'age' => 25, // Number → only show age 'active' => true, // true → show active 'deleted' => false, // false → completely ignored 'status' => 'online' // String → only show status ] ]); // Output: users { name { first last } age active status }
Parameterized Queries
// Simple parameters $result = array_to_gql([ 'users' => [ '__args' => [ 'limit' => 10 ], 'id' => true, 'name' => true ] ]); // Output: users(limit: "10") { id name } // Object parameters $result = array_to_gql([ 'users' => [ '__args' => [ 'search' => [ 'first_name' => 'John', 'last_name' => 'Doe' ] ], 'id' => true, 'name' => true ] ]); // Output: users(search: {first_name: "John", last_name: "Doe"}) { id name }
Alias Queries
$result = array_to_gql([ 'allUsers' => [ '__aliasFor' => 'users', '__args' => [ 'status' => 'active' ], 'id' => true, 'name' => true ] ]); // Output: allUsers: users(status: "active") { id name }
Complex Nested Queries
$result = array_to_gql([ 'posts' => [ '__args' => [ 'limit' => 10 ], 'id' => true, 'title' => true, 'author' => [ 'name' => true, 'profile' => [ 'bio' => true, 'avatar' => true ] ] ] ]); // Output: posts(limit: "10") { id title author { name profile { bio avatar } } }
Running Tests
# Run all tests vendor/bin/phpunit # Run specific test file vendor/bin/phpunit tests/ArrayToGqlTest.php # Run tests with detailed output vendor/bin/phpunit --testdox
Test Coverage
Current tests cover:
- ✅ Basic field selection
- ✅ Nested array structures
- ✅ Boolean value fields
- ✅ Simple and complex parameters
- ✅ Object parameters (multi-level nesting)
- ✅ Alias functionality
- ✅ Alias with parameter combinations
- ✅ Special character escaping
- ✅ Empty array handling
- ✅ Mixed value types
API Reference
array_to_gql($array): string
Converts a PHP array to a GraphQL query string.
Special Keys
__args: Define query parameters__aliasFor: Define field alias
Parameters
$array(array): The PHP array to convert
Return Value
string: GraphQL query string
License
MIT License
mathsgod/array_to_gql 适用场景与选型建议
mathsgod/array_to_gql 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 63 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「array」 「query」 「converter」 「builder」 「aliases」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mathsgod/array_to_gql 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mathsgod/array_to_gql 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mathsgod/array_to_gql 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
Query filtering in your frontend
Anax Database Active Record module for model classes.
Trait providing methods to set class properties with an array.
Traits to build collections of specific objects
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
统计信息
- 总下载量: 63
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-25