GraphQL Client
A simple package to consume GraphQL APIs., (*1)
Installation
composer require euautomation/graphql-client
, (*2)
Usage
Create an instance of EUAutomation\GraphQL\Client
:, (*3)
new Client($graphQLURL);
or, (*4)
$client = new Client();
$client->setUrl($graphQLURL);
Response class
Pass in your query, optional variables and headers (eg bearer token), $variables
and $headers
are optional, (*5)
$response = $client->response($query, $variables, $headers);
, (*6)
all()
Use $response->all();
to get all of the data returned in the response, (*7)
errors()
Use $response->errors();
to get all the errors returned in the response, (*8)
hasErrors()
Use $response->hasErrors();
to check if the response contains any errors, (*9)
Specific data from the response class
For example purposes, let's assume you want to get a list of all categories and execute this query., (*10)
{
allCategories(first:10) {
category {
id,
name,
slug,
description
}
}
}
Now in order to fetch some meaningful data from the Response class you can do the following:, (*11)
$categories = $response->allCategories->category;
foreach($categories as $category) {
// Do something with the data?
$category->id;
$category->name;
$category->slug;
$category->description;
}
You can also set, unset or isset data on the Response class., (*12)
Other responses
Raw guzzle response
Pass in your query, optional variables and headers (eg bearer token), $variables
and $headers
are optional, (*13)
$client->raw($query, $variables, $headers);
, (*14)
Json
Pass in your query, optional variables and headers (eg bearer token), $variables
and $headers
are optional, (*15)
$client->json($query, $variables, $headers);
, (*16)