There are 3 primary ways to use this package to generate your GraphQL queries:
- Query Class: Simple class that maps to GraphQL queries. It's designed to manipulate queries with ease and speed.
- QueryBuilder Class: Builder class that can be used to generate Query objects dynamically. It's design to be used in cases where a query is being build in a dynamic fashion.
- PHP GraphQL-OQM: An extension to this package. It Eliminates the need to write any GraphQL queries or refer to the API documentation or syntax. It generates query objects from the API schema, declaration exposed through GraphQL's introspection, which can then be simply interacted with.
Run the following command to install the package using composer:
A Client object can easily be instantiated by providing the GraphQL endpoint URL.
The Client constructor also receives an optional "authorizationHeaders" array, which can be used to add authorization headers to all requests being sent to the GraphQL server.
Always remember to replace
API_SECRET with the correct value that you copied from apito console.
Go to this page if you do not know where to find your api secrets for your project
The Client constructor also receives an optional "httpOptions" array, which overrides the "authorizationHeaders" and can be used to add custom Guzzle HTTP Client request options.
It is possible to use your own preconfigured HTTP client that implements the PSR-18 interface.
Although not the primary goal of this package, but it supports running raw string queries, just like any other client using the runRawQuery method in the Client class. Here's an example on how to use it:
Running query with the GraphQL client and getting the results in object structure:
Or getting results in array structure:
Running queries containing variables requires passing an associative array which
maps variable names (keys) to variable values (values) to the
Here's an example:
Mutations follow the same rules of queries in GraphQL, they select fields on returned objects, receive arguments, and can have sub-fields.
Here's a sample example on how to construct and run mutations:
Mutations can be run by the client the same way queries are run.
Mutations can utilize the variables in the same way Queries can. Here's an example on how to use the variables to pass input objects to the GraphQL server dynamically:
These are the resulting mutation and the variables passed with it:
For a detailed guide, be sure to check out the official github page here