Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Creates a GraphQL request payload with simple objects

License

NotificationsYou must be signed in to change notification settings

panvid/php-graphql-request-builder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Latest Stable VersionTotal DownloadsLicenseUnitTests

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 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.

Naming conventions

The schema ofGraphQL is defined by two easy attributes:

  • Types
  • Arguments

Type define a structured requested data object.Arguments can define these types.

Example

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")  }}

Why this library?

This library helps building thispayload structure without anystring concatenation or other strange ideas.

Example

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);

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 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 addArguments 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)]);

Arrays with more than one Argument in value.

The example above works if you have anarray with only oneArgument: everyperson only has oneArgument, theage. If you want to have moreArguments you need to create anArgumentwith 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]);

Enum arguments

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

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp