PHPackages                             mathsgod/gql-query-builder-php - 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/gql-query-builder-php

ActiveLibrary[API Development](/categories/api)

mathsgod/gql-query-builder-php
==============================

A PHP library to build GraphQL queries

1.0.0(3y ago)05.1k↓87.5%1MITPHPPHP ^8.0

Since Apr 3Pushed 3y ago1 watchersCompare

[ Source](https://github.com/mathsgod/gql-query-builder-php)[ Packagist](https://packagist.org/packages/mathsgod/gql-query-builder-php)[ Docs](https://github.com/mathsgod/gql-query-builder-php)[ RSS](/packages/mathsgod-gql-query-builder-php/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

GraphQL Query Builder for PHP
=============================

[](#graphql-query-builder-for-php)

[![PHP Composer](https://github.com/mathsgod/gql-query-builder-php/actions/workflows/php.yml/badge.svg)](https://github.com/mathsgod/gql-query-builder-php/actions/workflows/php.yml)

A simple helper function to build GraphQL queries, mutations and subscriptions use PHP.

This project is based on [GraphQL Query Builder](https://www.npmjs.com/package/gql-query-builder) for Node.js.

Install
-------

[](#install)

`composer require mathsgod/gql-query-builder-php`

Usage
-----

[](#usage)

```
use function GQLQueryBuilder\query;
use function GQLQueryBuilder\mutation;
use function GQLQueryBuilder\subscription;

$query=query($options);
$mutation=mutation($options);
$subscription=subscription($options);
```

Options
-------

[](#options)

`options is ["operation","field","variables"] or an array of options`

### Examples

[](#examples)

1. [Query](#query)
2. [Query (with variables)](#query-with-variables)
3. [Query (with nested fields selection)](#query-with-nested-fields-selection)
4. [Query (with custom argument name)](#query-with-custom-argument-name)
5. [Query (with required variables)](#query-with-required-variables)
6. [Query (with empty fields)](#query-with-empty-fields)
7. [Mutation](#mutation)
8. [Mutation (with required variables)](#mutation-with-required-variables)
9. [Subscription](#subscription)

#### Query:

[](#query)

```
$query=query([
    "operation"=>"thoughts",
    "fields"=>['id', 'name', 'thought']
]);

print_r($query);

// output:
/*
Array
(
    [query] => query { thoughts { id name thought } }
    [variables] => Array
        (
        )
)
*/
```

#### Query (with variables):

[](#query-with-variables)

```
$query=query([
    "operation"=>"thoughts",
    "fields"=>['id', 'name', 'thought'],
    "variables"=>[
        "id"=>1,
    ]
]);

print_r($query);

// output:
/*
Array
(
    [query] => query ($id: Int) { thoughts(id: $id) { id name thought } }
    [variables] => Array
        (
            [id] => 1
        )
)
*/
```

#### Query (with nested fields selection):

[](#query-with-nested-fields-selection)

```
$query = query([
    "operation" => "orders",
    "fields" => [
        "id",
        "amount",
        "user" => [
            "id",
            "name",
            "email",
            "address" => [
                "city",
                "country",
            ]
        ]
    ]
]);

// output:
/*
Array
(
    [query] => query { orders { id, amount, user { id, name, email, address { city, country } } } }
    [variables] => Array
        (
        )

)
*/
```

#### Query (with custom argument name):

[](#query-with-custom-argument-name)

```
$query = query([
    "operation" => "someoperation",
    "fields" => [
        [
            "operation" => "nestedoperation",
            "fields" => ['field1'],
            "variables" => [
                "id2" => [
                    "name" => "id",
                    "type" => "ID",
                    "value" => 123
                ]
            ],
        ]
    ],
    "variables" => [
        "id" => [
            "name" => "id",
            "type" => "ID",
            "value" => 456
        ]
    ]
]);

/*
Array
(
    [query] => query($id: ID, $id2: ID) { someoperation(id: $id) { nestedoperation (id: $id2)  { field1 }  } }
    [variables] => Array
        (
            [id] => 456
            [id2] => 123
        )

)
*/
```

#### Query (with required variables)

[](#query-with-required-variables)

```
$query=query([
    "operation"=>"userLogin",
    "variables"=>[
        "email"=>[
            "value"=>"jon.doe@example.com",
            "required"=>true
        ],
        "password"=>[
            "value"=>"123456",
            "required"=>true
        ]
    ],
    "fields"=>["userId","token"]
]);

/*

Array
(
    [query] => query($email: String!, $password: String!) { userLogin(email: $email, password: $password) { userId, token } }
    [variables] => Array
        (
            [email] => jon.doe@example.com
            [password] => 123456
        )

)

*/
```

#### Query (with empty fields):

[](#query-with-empty-fields)

```
$query = query([
    [
        "operation" => "getFilteredUsersCount",
    ],
    [
        "operation" => "getAllUsersCount",
        "fields" => []
    ],
    [
        "operation" => "getFilteredUsers",
        "fields" => [
            "count" => []
        ]
    ]
]);

print_r($query);

/*
Array
(
    [query] => query { getFilteredUsersCount getAllUsersCount getFilteredUsers { count } }
    [variables] => Array
        (
        )

)
*/
```

#### Mutation:

[](#mutation)

```
$mutation=mutation([
    "operation"=>"createThought",
    "variables"=>[
        "name"=>"John Doe",
        "thought"=>"Hello World"
    ],
    "fields"=>["id","name","thought"]
]);

print_r($mutation);

/*

Array
(
    [query] => mutation($name: String, $thought: String) { createThought(name: $name, thought: $thought) { id name thought } }
    [variables] => Array
        (
            [name] => John Doe
            [thought] => Hello World
        )

)

*/
```

#### Mutation (with required variables):

[](#mutation-with-required-variables)

```
$query = mutation([
    "operation" => "userSignup",
    "variables" => [
        "name" => [
            "value" => "Jon Doe",
        ],
        "email" => [
            "value" => "jon.doe@example.com", "required" => true
        ],
        "password" => [
            "value" => "123456", "required" => true
        ],
    ],
    "fields" => ["userId"]
]);

print_r($query);

/*

Array
(
    [query] => mutation($name: String, $email: String!, $password: String!) { userSignup(name: $name, email: $email, password: $password) { userId } }
    [variables] => Array
        (
            [name] => Jon Doe
            [email] => jon.doe@example.com
            [password] => 123456
        )
)

*/
```

#### Subscription:

[](#subscription)

```
$query = subscription([
    "operation" => "thoughtCreate",
    "variables" => [
        "name" => "Tyrion Lannister",
        "thought" => "I drink and I know things."
    ],
    "fields" => ["id"]
]);

print_r($query);

/*

Array
(
    [query] => subscription($name: String, $thought: String) { thoughtCreate(name: $name, thought: $thought) { id } }
    [variables] => Array
        (
            [name] => Tyrion Lannister
            [thought] => I drink and I know things.
        )
)

*/
```

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity50

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

Unknown

Total

1

Last Release

1186d 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 (20 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mathsgod-gql-query-builder-php/health.svg)

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

###  Alternatives

[exsyst/swagger

A php library to manipulate Swagger specifications

35916.4M7](/packages/exsyst-swagger)[hubspot/api-client

Hubspot API client

24016.2M20](/packages/hubspot-api-client)[pocketmine/bedrock-protocol

An implementation of the Minecraft: Bedrock Edition protocol in PHP

172445.0k13](/packages/pocketmine-bedrock-protocol)[botman/driver-telegram

Telegram driver for BotMan

93459.5k6](/packages/botman-driver-telegram)

PHPackages © 2026

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