There are 3 primary ways to use this package to generate your GraphQL queries:
Run the following command to install the package using composer:
$ composer require gmostafa/php-graphql-client
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.
Tip:
Always remember to replace
API_SECRET
in the Bearer Token andproject-id
in the URL with the correct values from the Apito console.
If you are unsure where to find your API secrets and endpoints for your project, visit this page.
Example:
$client = new Client(
'https://api.apito.io/secured/graphql',
['Authorization' => 'Basic API_SECTET']
);
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.
Example:
$client = new Client(
'https://api.apito.io/secured/graphql',
[],
[
'connect_timeout' => 5,
'timeout' => 5,
'headers' => [
'Authorization' => 'Basic API_SECTET'
'User-Agent' => 'testing/1.0',
],
'proxy' => [
'http' => 'tcp://localhost:8125', // Use this proxy with "http"
'https' => 'tcp://localhost:9124', // Use this proxy with "https",
'no' => ['.mit.edu', 'foo.com'] // Don't use a proxy with these
],
'cert' => ['/path/server.pem', 'password']
...
]
);
It is possible to use your own preconfigured HTTP client that implements the PSR-18 interface.
Example:
$client = new Client(
'https://api.apito.io/secured/graphql',
[],
[],
$myHttpClient
);
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:
$gql = <<<QUERY
query {
pokemon(name: "Pikachu") {
id
number
name
attacks {
special {
name
type
damage
}
}
}
}
QUERY;
$results = $client->runRawQuery($gql);
Running query with the GraphQL client and getting the results in object structure:
$results = $client->runQuery($gql);
$results->getData()->companies[0]->branches;
Or getting results in array structure:
$results = $client->runQuery($gql, true);
$results->getData()['companies'][1]['branches']['address'];
Running queries containing variables requires passing an associative array which
maps variable names (keys) to variable values (values) to the runQuery
method.
Here's an example:
$gql = (new Query('companies'))
->setVariables(
[
new Variable('name', 'String', true),
new Variable('limit', 'Int', false, 5)
]
)
->setArguments(['name' => '$name', 'first' => '$limit'])
->setSelectionSet(
[
'name',
'serialNumber'
]
);
$variablesArray = ['name' => 'Tech Co.', 'first' => 5];
$results = $client->runQuery($gql, true, $variablesArray);
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:
$mutation = (new Mutation('createCompany'))
->setArguments(['companyObject' => new RawObject('{name: "Trial Company", employees: 200}')])
->setSelectionSet(
[
'_id',
'name',
'serialNumber',
]
);
$results = $client->runQuery($mutation);
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:
$mutation = (new Mutation('createCompany'))
->setVariables([new Variable('company', 'CompanyInputObject', true)])
->setArguments(['companyObject' => '$company']);
$variables = ['company' => ['name' => 'Tech Company', 'type' => 'Testing', 'size' => 'Medium']];
$client->runQuery(
$mutation, true, $variables
);
These are the resulting mutation and the variables passed with it:
mutation($company: CompanyInputObject!) {
createCompany(companyObject: $company)
}
{"company":{"name":"Tech Company","type":"Testing","size":"Medium"}}
For a detailed guide, be sure to check out the official github page here