PHPackages                             mathsgod/array\_to\_gql - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. mathsgod/array\_to\_gql

ActiveLibrary[API Development](/categories/api)

mathsgod/array\_to\_gql
=======================

A PHP library for converting PHP arrays to GraphQL query syntax with support for parameters, aliases, and nested structures

1.0.5(11mo ago)0581MITPHPCI passing

Since Jul 25Pushed 3w agoCompare

[ Source](https://github.com/mathsgod/array_to_gql)[ Packagist](https://packagist.org/packages/mathsgod/array_to_gql)[ RSS](/packages/mathsgod-array-to-gql/feed)WikiDiscussions main Synced today

READMEChangelog (5)Dependencies (1)Versions (7)Used By (1)

Array to GraphQL
================

[](#array-to-graphql)

[![Tests](https://github.com/mathsgod/array_to_gql/actions/workflows/tests.yml/badge.svg)](https://github.com/mathsgod/array_to_gql/actions/workflows/tests.yml)[![PHP Version](https://camo.githubusercontent.com/ff8db6404facd56c83d963c171142861f6fa4bfcda6350ca5953ec3ddbe34536/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545372e30253230253743253743253230253545382e302d3838393242462e737667)](https://github.com/mathsgod/array_to_gql)[![License](https://camo.githubusercontent.com/8bb50fd2278f18fc326bf71f6e88ca8f884f72f179d3e555e20ed30157190d0d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e2e737667)](LICENSE)

A PHP library for converting PHP arrays to GraphQL query syntax.

Features
--------

[](#features)

- 🔧 **Basic Queries**: Support for simple field selection
- 📊 **Nested Queries**: Support for multi-level nested array structures
- 🎯 **Parameterized Queries**: Use `__args` to add query parameters
- 🏷️ **Alias Queries**: Use `__aliasFor` to create GraphQL aliases
- 🎨 **Object Parameters**: Support for complex nested object parameters
- ✅ **Boolean Fields**: `true` values display as field selection, `false` values are ignored
- 🎯 **Value Processing**: All non-`false` values (strings, numbers, `true`) only display key names, not values
- 🔒 **Character Escaping**: Automatic handling of special character escaping

Installation
------------

[](#installation)

```
composer require mathsgod/array_to_gql
```

Usage
-----

[](#usage)

### Basic 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)

```
// 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

[](#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

[](#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

[](#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
-------------

[](#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
-------------

[](#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
-------------

[](#api-reference)

### `array_to_gql($array): string`

[](#array_to_gqlarray-string)

Converts a PHP array to a GraphQL query string.

#### Special Keys

[](#special-keys)

- `__args`: Define query parameters
- `__aliasFor`: Define field alias

#### Parameters

[](#parameters)

- `$array` (array): The PHP array to convert

#### Return Value

[](#return-value)

- `string`: GraphQL query string

License
-------

[](#license)

MIT License

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance76

Regular maintenance activity

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

6

Last Release

343d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/18732337?v=4)[Raymond](/maintainers/mathsgod)[@mathsgod](https://github.com/mathsgod)

---

Top Contributors

[![mathsgod](https://avatars.githubusercontent.com/u/18732337?v=4)](https://github.com/mathsgod "mathsgod (11 commits)")

---

Tags

phparraygraphqlconverterquerybuilderparametersquery builderaliasesgqlgraphql-query

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mathsgod-array-to-gql/health.svg)

```
[![Health](https://phpackages.com/badges/mathsgod-array-to-gql/health.svg)](https://phpackages.com/packages/mathsgod-array-to-gql)
```

###  Alternatives

[gmostafa/php-graphql-client

GraphQL client and query builder.

3218.2M29](/packages/gmostafa-php-graphql-client)[gmostafa/php-graphql-oqm

GraphQL Object-to-Query Mapper (QOM) which generates objects from API schema

44937.4k2](/packages/gmostafa-php-graphql-oqm)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
