Download the PHP package softonic/graphql-client without Composer
On this page you can find all versions of the php package softonic/graphql-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package graphql-client
PHP GraphQL Client
PHP Client for GraphQL
Main features
- Client with Oauth2 Support
- Easy query/mutation execution
- Simple array results for mutation and queries
- Powerful object results for mutation and queries
- Filter results
- Manipulate results precisely and bulk
- Transform query results in mutations
Installation
Via composer:
Documentation
Instantiate a client
You can instantiate a simple client or with Oauth2 support.
Simple Client
OAuth2 provider
This package allows you to use thephpleague/oauth2-client adapters for authentication, so the endpoint depends on the adapter that you are using. The adapter could be custom or provided by the library as official or third party.
This is an example using a custom provider.
Using the GraphQL Client
You can use the client to execute queries and mutations and get the results.
In the previous examples, the client is used to execute queries and mutations. The response object is used to get the results in array format.
This can be convenient for simple use cases, but it is not recommended for complex results or when you need to use that output to generate mutations. For this reason, the client provides another output called data objects. Those objects allow you to get the results in a more convenient format, allowing you to generate mutations, apply filters, etc.
How to use a data object and transform it to a mutation query
The query result can be obtained as an object which will provide facilities to convert it to a mutation and modify the data easily. At the end, the mutation object will be able to be used as the variables of the mutation query in the GraphQL client.
First we execute a "read" query and obtain the result as an object compound of Items and Collections.
We can also filter the results in order to work with fewer data later. The filter method returns a new object with the filtered results, so you need to reassign the object to the original one, if you want to modify it.
Then we can generate the mutation variables object from the previous query results. This is build using a mutation config. The config for each type has the following parameters:
- linksTo: the location in the query result object where the data can be obtained for that type. If not present, it means it's a level that has no data from the source.
- type: mutation object type (Item or Collection).
- children: if the mutation has a key which value is another mutation type.
Now we can modify the mutation data using the following methods:
- add(): Adds an Item to a Collection.
- set(): Updates some values of an Item. It also works on Collections, updating all its Items.
- filter(): Filters the Items of a Collection.
- count(): Counts the Items of a Collection.
- isEmpty(): Check if a Collection is empty.
- has(): Checks whether an Item has an argument or not. Works on Collections too. Dot notation is also allowed.
- hasItem(): Checks whether a Collection has an Item with the provided data or not.
- remove(): Removes an Item from a Collection.
- __unset(): Removes a property from an Item or from all the Items of a Collection.
Finally, the modified mutation data can be passed to the GraphQL client to execute the mutation. When the query is executed, the mutation variables are encoded using json_encode(). This modifies the mutation data just returning the items changed and its parents.
So the final variables sent to the query would be:
NOTE 2: The example has been done for a root Item "book", but it also works for a Collection as root object.
Testing
softonic/graphql-client
has a PHPUnit test suite, and a coding style compliance test suite using PHP CS Fixer.
To run the tests, run the following command from the project folder.
To open a terminal in the dev environment:
License
The Apache 2.0 license. Please see LICENSE for more information.
All versions of graphql-client with dependencies
guzzlehttp/guzzle Version ^6.3 || ^7.0
softonic/guzzle-oauth2-middleware Version ^2.1
ext-json Version *
symfony/console Version ^6.0 || ^7.0