- Notifications
You must be signed in to change notification settings - Fork0
Creates a GraphQL request payload with simple objects
License
panvid/php-graphql-request-builder
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This library builds a request for sending to a GraphQL server.
GraphQL is a query language to easy request data from remote web servers. There are several pros for usingGraphQLinstead ofREST like
- decreasing request amount
- saving traffic in payload
- avoid backend changes on changing client requested data
For a full description seeHow to GraphQL.
The schema ofGraphQL is defined by two easy attributes:
- Types
- Arguments
Type define a structured requested data object.Arguments can define these types.
A typeforum can haveposts, which hasauthors and atitle. If you want to receive allauthor ant posttitleinformation as response your request can look like this:
{forum {posts {authors,title } }}
To specify your requested data for crawling only thelast 5posts 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") }}
This library helps building thispayload structure without anystring
concatenation or other strange ideas.
To create following request payload
{field {search(criteria: {start:"2019-08-23"}) {errors {codetypedescription }id } }}
you need to execute following PHP code
<?phpdeclare(strict_types=1);useGraphQL\RequestBuilder\Argument;useGraphQL\RequestBuilder\RootType;useGraphQL\RequestBuilder\Type;$searchType = (newType('search')) ->addArgument(newArgument('criteria',newArgument('start','2019-08-23'))) ->addSubTypes([ (newType('errors'))->addSubTypes(['code','type','description']),'id' ]);echo (string) (newRootType('field'))->addSubType($searchType);
Its also possible to build complex types. This code examples show you how to do this.
Sometimes you want to have arrays with complexArgument
types, like the following example.
{persons: [ {age: 30}, {age: 20}, {age: 12} ]}
For this concept you can use the classArrayArgument
which give the possibility to addArgument
s to an array.
<?phpdeclare(strict_types=1);useGraphQL\RequestBuilder\Argument;useGraphQL\RequestBuilder\ArrayArgument;$persons =newArrayArgument('persons', [newArgument('age',30),newArgument('age',20),newArgument('age',12)]);
The example above works if you have anarray
with only oneArgument
: everyperson only has oneArgument
, theage. If you want to have moreArgument
s you need to create anArgument
with an empty name.
{persons: [ {name:"Hans",age: 30 }, {name:"Max",age: 20 } ]}
YourPHP code should look like this:
<?phpdeclare(strict_types=1);useGraphQL\RequestBuilder\Argument;useGraphQL\RequestBuilder\ArrayArgument;$person1 =newArrayArgument('', [newArgument('name','Hans'),newArgument('age',30)]);$person2 =newArrayArgument('', [newArgument('name','Max'),newArgument('age',20)]);$persons =newArrayArgument('persons', [$person1,$person2]);
To create enum arguments it should look like this:
<?phpdeclare(strict_types=1);useGraphQL\RequestBuilder\EnumArgument;$person1 =newEnumArgument('EnumAttribute','EnumValue');
About
Creates a GraphQL request payload with simple objects
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.