-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathmutation_example.php
47 lines (37 loc) · 1.16 KB
/
mutation_example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use GraphQL\Client;
use GraphQL\Exception\QueryError;
use GraphQL\Mutation;
use GraphQL\RawObject;
// Create Client object to contact the GraphQL endpoint
$client = new Client(
'api_url',
[] // Replace with array of extra headers to be sent with request for auth or other purposes
);
// Create the GraphQL mutation
$gql = (new Mutation('createCompany'))
->setArguments(['companyObject' => new RawObject('{name: "Trial Company", employees: 200}')])
->setSelectionSet(
[
'_id',
'name',
'serialNumber',
]
);
// Run query to get results
try {
$results = $client->runQuery($gql);
}
catch (QueryError $exception) {
// Catch query error and desplay error details
print_r($exception->getErrorDetails());
exit;
}
// Display original response from endpoint
var_dump($results->getResponseObject());
// Display part of the returned results of the object
var_dump($results->getData()->pokemon);
// Reformat the results to an array and get the results of part of the array
$results->reformatResults(true);
print_r($results->getData()['pokemon']);