PHPackages                             dpauli/graphql-request-builder - 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. dpauli/graphql-request-builder

ActiveLibrary[API Development](/categories/api)

dpauli/graphql-request-builder
==============================

Request Builder for GraphQL payload

1.8.0(5y ago)4105.8k↑446.8%[1 issues](https://github.com/dpauli/php-graphql-request-builder/issues)MITPHPPHP &gt;=7.4.0

Since Aug 14Pushed 5y agoCompare

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

READMEChangelog (10)Dependencies (7)Versions (16)Used By (0)

GraphQL Request Builder
=======================

[](#graphql-request-builder)

[![Latest Stable Version](https://camo.githubusercontent.com/0e79e7e44928bc9a63b6c880f03f4f2229c5278c45ea997d496816b7b7bfa3b9/68747470733a2f2f706f7365722e707567782e6f72672f647061756c692f6772617068716c2d726571756573742d6275696c6465722f76)](//packagist.org/packages/dpauli/graphql-request-builder)[![Total Downloads](https://camo.githubusercontent.com/f3f144033005c18caa0926d56364bb08cfb9bb9412371cd3450c66c88a452d7d/68747470733a2f2f706f7365722e707567782e6f72672f647061756c692f6772617068716c2d726571756573742d6275696c6465722f646f776e6c6f616473)](//packagist.org/packages/dpauli/graphql-request-builder)[![License](https://camo.githubusercontent.com/ab717d04574acaaa4369692d47f0d9245f9546cc88ee51b9430d3c3e99370c0f/68747470733a2f2f706f7365722e707567782e6f72672f647061756c692f6772617068716c2d726571756573742d6275696c6465722f6c6963656e7365)](//packagist.org/packages/dpauli/graphql-request-builder)[![UnitTests](https://github.com/dpauli/php-graphql-request-builder/actions/workflows/phpunit.yml/badge.svg)](//github.com/dpauli/php-graphql-request-builder/actions/workflows/phpunit.yml)

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

What is GraphQL?
----------------

[](#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](https://www.howtographql.com/).

### Naming conventions

[](#naming-conventions)

The schema of *GraphQL* is defined by two easy attributes:

- *Type*s
- *Argument*s

*Type* define a structured requested data object. *Argument*s can define these types.

#### Example

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

[](#why-this-library)

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

### Example

[](#example-1)

To create following request payload

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

you need to execute following PHP code

```
