https://github.com/panvid/php-graphql-request-builder
Creates a GraphQL request payload with simple objects
https://github.com/panvid/php-graphql-request-builder
Last synced: 27 days ago
JSON representation
Creates a GraphQL request payload with simple objects
- Host: GitHub
- URL: https://github.com/panvid/php-graphql-request-builder
- Owner: panvid
- License: mit
- Created: 2018-08-14T17:50:25.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-05-13T20:46:33.000Z (almost 5 years ago)
- Last Synced: 2026-03-12T04:13:57.633Z (about 2 months ago)
- Language: PHP
- Size: 34.2 KB
- Stars: 4
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-graphql - graphql-request-builder - Builds request payload in GraphQL structure. (Implementations / PHP)
- fucking-awesome-graphql - graphql-request-builder - Builds request payload in GraphQL structure. (Implementations / PHP)
README
# GraphQL Request Builder
[](//packagist.org/packages/dpauli/graphql-request-builder)
[](//packagist.org/packages/dpauli/graphql-request-builder)
[](//packagist.org/packages/dpauli/graphql-request-builder)
[](//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?
*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
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
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:
```graphql
{
forum {
posts {
authors,
title
}
}
}
```
To specify your requested data for crawling only the **last 5** *posts* you can modify your request like this:
```graphql
{
forum {
posts(last: 5) {
authors,
title
}
}
}
```
*GraphQL* requested data can complex as you want:
```graphql
{
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?
This library helps building this **payload structure** without any `string` concatenation or other strange ideas.
### Example
To create following request payload
```graphql
{
field {
search(criteria: {start: "2019-08-23"}) {
errors {
code
type
description
}
id
}
}
}
```
you need to execute following PHP code
```php
addArgument(new Argument('criteria', new Argument('start', '2019-08-23')))
->addSubTypes([
(new Type('errors'))->addSubTypes(['code', 'type', 'description']),
'id'
]
);
echo (string) (new RootType('field'))->addSubType($searchType);
```
## Build complex types
Its also possible to build complex types. This code examples show you how to do this.
### Arguments with array of arguments
Sometimes you want to have arrays with complex `Argument` types, like the following example.
```graphql
{
persons: [
{age: 30},
{age: 20},
{age: 12}
]
}
```
For this concept you can use the class `ArrayArgument` which give the possibility to add `Argument`s to an array.
```php