-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathquery_example.php
81 lines (70 loc) · 2.36 KB
/
query_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use GraphQL\Client;
use GraphQL\Exception\QueryError;
use GraphQL\Query;
// Create Client object to contact the GraphQL endpoint
$client = new Client(
'https://graphql-pokemon.now.sh/',
[] // Replace with array of extra headers to be sent with request for auth or other purposes
);
// Create the GraphQL query
$gql = (new Query('pokemon'))
->setArguments(['name' => 'Pikachu'])
->setSelectionSet(
[
'id',
'number',
'name',
(new Query('attacks'))
->setSelectionSet(
[
(new Query('special'))
->setSelectionSet(
[
'name',
'type',
'damage',
]
)
]
),
(new Query('evolutions'))
->setSelectionSet(
[
'id',
'number',
'name',
(new Query('attacks'))
->setSelectionSet(
[
(new Query('fast'))
->setSelectionSet(
[
'name',
'type',
'damage',
]
)
]
)
]
)
]
);
// 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']);