PHPackages                             cfpinto/graphql - 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. cfpinto/graphql

ActiveLibrary[API Development](/categories/api)

cfpinto/graphql
===============

A GraphQL query builder class

v2.0.1(1y ago)19227.7k↑206.2%10[6 issues](https://github.com/cfpinto/graphql/issues)4MITPHPPHP 8.\*CI failing

Since Oct 2Pushed 1y ago2 watchersCompare

[ Source](https://github.com/cfpinto/graphql)[ Packagist](https://packagist.org/packages/cfpinto/graphql)[ Docs](https://github.com/cfpinto/graphql)[ RSS](/packages/cfpinto-graphql/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (22)Used By (4)

graphql
=======

[](#graphql)

A simple yet powerful GraphQL query builder [![Codacy Badge](https://camo.githubusercontent.com/43a93e20dc4fdee5db18be2dceead24d14bf05ffbc2492e48418bd2f41c438f5/68747470733a2f2f6170702e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f6663653230653363336131393461373761636135383565363866643438666331)](https://www.codacy.com/gh/cfpinto/graphql/dashboard?utm_source=github.com&utm_medium=referral&utm_content=cfpinto/graphql&utm_campaign=Badge_Grade)[![Codacy Badge](https://camo.githubusercontent.com/c6fc26cffd1a22595de28b8625ba36842b88fbe5c3f64f382c233a041af2c66c/68747470733a2f2f6170702e636f646163792e636f6d2f70726f6a6563742f62616467652f436f7665726167652f6663653230653363336131393461373761636135383565363866643438666331)](https://www.codacy.com/gh/cfpinto/graphql/dashboard?utm_source=github.com&utm_medium=referral&utm_content=cfpinto/graphql&utm_campaign=Badge_Coverage)

Change log
----------

[](#change-log)

A lot of rewriting was done on this version particularly at code organization and entity management. Whit this level of rewriting keeping backwards compatibility is tricky but did my best to do so.

There are a few deprecation notices that will be dropped next version:

- Root level classes will be dropped in favour of context namespace (GraphQL\\Mutation =&gt; GraphQL\\Actions\\Mutation)
- Parsers and Entities will be decoupled, you will be forced to inject Parsers in the Entities constructor
- The `on()` method is used as syntax sugar, might be deprecated in favour of passing InlineFragment instances in the use method

How it works
------------

[](#how-it-works)

Writing GraphQL queries can be a painful process. With GraphpQL query builder write PHP and get GraphQL

### Examples

[](#examples)

When finding a Hero to be his sidekick, one finds himself overwhelmed with the number of Heroes out there. There are all sort of Heroes so lets list them all

#### Fields

[](#fields)

```
$hero = new \GraphQL\Actions\Query('hero');
echo $hero->use('name')
    ->friends
        ->use('name')
    ->root()
        ->query();
```

will generate

```
{
    hero {
        name
        friends {
            name
        }
    }
}

```

#### Arguments

[](#arguments)

A Hero will have many friends which can make it hard to walk through, it would be great limit the Hero's friends to 2

```
$hero = new \GraphQL\Actions\Query('hero');
echo $hero->use('name')
    ->friends(['first'=>2])
        ->use('name')
    ->root()
        ->query();
```

will generate

```
{
    hero {
        name
        friends(first: 2) {
            name
        }
    }
}

```

#### Going back the tree

[](#going-back-the-tree)

Sometimes you might need to know more about this Hero, like when you want to know a hero friends and costumes. We then need to go back in the Hero tree. For that we'll use `$node->prev()`

```
$hero = new \GraphQL\Actions\Query('hero');
echo $hero->use('name')
    ->friends(['first'=>2])
        ->use('name')
    ->prev()
    ->costumes
        ->color
    ->root()
        ->query();
```

will generate

```
{
    hero {
        name
        friends(first: 2) {
            name
        }
        costumes {
            color
        }
    }
}

```

#### Inline Fragments

[](#inline-fragments)

Sometimes you don't quite know the type of hero you looking for. Maybe you looking a flying Hero, maybe a Strong Hero

```
$hero = new \GraphQL\Actions\Query('hero');
echo $hero->use('name')
    ->on('FlyingHero')
        ->use('hasCape')
    ->prev()
    ->on('StrongHero')
        ->use('strengthLevel')
    ->prev()
    ->friends(['first'=>2])
        ->use('name')
    ->prev()
    ->costumes
        ->color
    ->root()
        ->query();
```

Will generate

```
{
    hero {
        name
        ... on FlyingHero {
            hasCape
        }
        ... on StrongHero {
            strengthLevel
        }
        friends(first: 2) {
            name
        }
        costumes {
            color
        }
    }
}

```

#### Aliases

[](#aliases)

For the element of surprise, you might need to name some of the hero's properties differently; You might want to call friends as partners\_in\_good or name as call\_me\_this

```
$hero = new \GraphQL\Actions\Query('hero');
echo $hero->use('name')
    ->alias('call_me_this', 'name')
    ->friends(['first'=>2])
        ->alias('partners_in_good')
        ->use('name')
    ->prev()
    ->costumes
        ->color
    ->root()
        ->query();
```

will generate

```
{
    hero {
        call_me_this: name
        partners_in_good: friends(first: 2) {
            name
        }
        costumes {
            color
        }
    }
}

```

#### Fragments

[](#fragments)

Sorry have no super hero narrative from here :D . sticking to good old technical explanation

To use fragments declare the fragment as you would a graph and then use it within a `->use()` call as you would with a regular property

```
$fragment = new GraphQL\Entities\Fragment('properties', 'Hero');
$fragment->use('id', 'age');
$hero = new \GraphQL\Actions\Query('hero');
echo $hero->use('name', $fragment)->query();
echo $fragment->query();
```

will generate

```
{
    hero {
        name
        ...properties
    }
}

fragment properties on Hero {
    id
    age
}

```

#### Variables

[](#variables)

The use of variables feels less necessary because we're using PHP to build the query. Still...

```
$variable = new GraphQL\Entities\Variable('name', 'String');
$hero = new \GraphQL\Actions\Query('hero', ['name' => $variable]);
echo $hero->use('name')->query();
```

will generate

```
query getGraph($name: String){
    hero(name: $name) {
        name
    }
}

```

#### Meta fields

[](#meta-fields)

you can also use meta fields the same way you would request a property

```
$variable = new GraphQL\Entities\Variable('name', 'String');
$hero = new \GraphQL\Actions\Query('hero', ['name' => $variable]);
echo $hero->use('name', '__typename')->query();
```

will generate

```
query getGraph($name: String){
    hero(name: $name) {
        name
        __typename
    }
}

```

Which can also be aliased

```
$variable = new GraphQL\Entities\Variable('name', 'String');
$hero = new \GraphQL\Actions\Query('hero', ['name' => $variable]);
echo $hero->use('name', '__typename')
    ->alias('type', '__typename')
    ->query();
```

will generate

```
query getGraph($name: String){
    hero(name: $name) {
        name
        type: __typename
    }
}

```

#### Mutations

[](#mutations)

After you chose your Hero and he takes you as his sidekick he will let you do some help him with some of his daily routine. He might even let you choose his costume color. How cool is that?

```
$mutation = new GraphQL\Actions\Mutation('changeHeroCostumeColor', ['id' => 'theHeroId', 'color'=>'red']);
$mutation
    ->hero
        ->use('name')
        ->costumes
            ->use('color')
    ->root()
        ->query();
```

Will generate

```
mutation changeHeroCostumeColor(id: 'theHeroId', color: 'red') {
    hero {
        name
        costumes {
            color
        }
    }
}

```

With variables

```
$mutation = new GraphQL\Actions\Mutation('changeHeroCostumeColor', ['id' => new GraphQL\Entities\Variable('uuid', 'String', ''), new GraphQL\Entities\Variable('color', 'String', '')]);
$mutation
    ->hero
        ->use('name')
        ->costumes
            ->use('color')
    ->root()
        ->query();
```

Will generate

```
mutation ChangeHeroCostumeColorMutation($uuid: String, $color: String) {
    changeHeroCostumeColorAction(id: $uuid, color: $color) {
        hero {
            name
            costumes {
                color
            }
        }
    }
}

```

Build Status
------------

[](#build-status)

[![Codacy Badge](https://camo.githubusercontent.com/43a93e20dc4fdee5db18be2dceead24d14bf05ffbc2492e48418bd2f41c438f5/68747470733a2f2f6170702e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f6663653230653363336131393461373761636135383565363866643438666331)](https://app.codacy.com/gh/cfpinto/graphql/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)[![Codacy Badge](https://camo.githubusercontent.com/c6fc26cffd1a22595de28b8625ba36842b88fbe5c3f64f382c233a041af2c66c/68747470733a2f2f6170702e636f646163792e636f6d2f70726f6a6563742f62616467652f436f7665726167652f6663653230653363336131393461373761636135383565363866643438666331)](https://app.codacy.com/gh/cfpinto/graphql/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance30

Infrequent updates — may be unmaintained

Popularity46

Moderate usage in the ecosystem

Community22

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 87.3% 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 ~142 days

Recently: every ~248 days

Total

19

Last Release

584d ago

Major Versions

0.1.0 → 1.0.02021-04-11

v1.0.4 → v2.0.02023-01-20

PHP version history (5 changes)0.0.1PHP &gt;=7.0.0

0.0.9PHP &gt;=7.2.0

1.0.0PHP &gt;=7.4

v1.0.4PHP &gt;=7.4|8.\*

v2.0.0PHP 8.\*

### Community

Maintainers

![](https://www.gravatar.com/avatar/f9090f664aaf571ab6c65657f6a49b20ff6a341fb3adc672ce87b2ea4d479686?d=identicon)[cfpinto](/maintainers/cfpinto)

---

Top Contributors

[![cfpinto](https://avatars.githubusercontent.com/u/11293388?v=4)](https://github.com/cfpinto "cfpinto (55 commits)")[![claudiu-cristea](https://avatars.githubusercontent.com/u/473868?v=4)](https://github.com/claudiu-cristea "claudiu-cristea (4 commits)")[![fakeheal](https://avatars.githubusercontent.com/u/1038697?v=4)](https://github.com/fakeheal "fakeheal (3 commits)")[![MurzNN](https://avatars.githubusercontent.com/u/336662?v=4)](https://github.com/MurzNN "MurzNN (1 commits)")

---

Tags

graphqlquery builder

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/cfpinto-graphql/health.svg)

```
[![Health](https://phpackages.com/badges/cfpinto-graphql/health.svg)](https://phpackages.com/packages/cfpinto-graphql)
```

###  Alternatives

[webonyx/graphql-php

A PHP port of GraphQL reference implementation

4.7k77.3M333](/packages/webonyx-graphql-php)[nuwave/lighthouse

A framework for serving GraphQL from Laravel

3.5k10.7M93](/packages/nuwave-lighthouse)[gmostafa/php-graphql-client

GraphQL client and query builder.

3217.6M25](/packages/gmostafa-php-graphql-client)[overblog/graphql-bundle

This bundle provides tools to build a GraphQL server in your Symfony App.

8027.9M28](/packages/overblog-graphql-bundle)[gmostafa/php-graphql-oqm

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

44898.8k2](/packages/gmostafa-php-graphql-oqm)[mll-lab/graphql-php-scalars

A collection of custom scalar types for usage with https://github.com/webonyx/graphql-php

1394.2M28](/packages/mll-lab-graphql-php-scalars)

PHPackages © 2026

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