-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommon.php
More file actions
154 lines (140 loc) · 3.88 KB
/
Copy pathcommon.php
File metadata and controls
154 lines (140 loc) · 3.88 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
use GraphQL\Executor\Promise\PromiseAdapter;
use GraphQL\GraphQL;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
function renderMessage($query, $data, $calls, $callsIds)
{
echo "Schema:\n";
echo <<<EOF
type Character : Character {
id: String!
name: String
friends: [Character]
}
type Query {
character(id: String!): Character
}\n\n
EOF;
echo "Query:\n $query\n\n";
echo "Response:\n".json_encode($data, JSON_PRETTY_PRINT)."\n";
echo "Resolver calls: ".var_export($calls, true)."\n";
echo "Resolver calls ids:\n".json_encode($callsIds, JSON_PRETTY_PRINT)."\n";
echo "\n\n";
}
function createSchema(callable $friendsResolver, callable $characterResolver)
{
/**
* This implements the following type system shorthand:
* type Character : Character {
* id: String!
* name: String
* friends: [Character]
* }
*/
$characterType = new ObjectType([
'name' => 'Character',
'fields' => function () use (&$characterType, $friendsResolver) {
return [
'id' => [
'type' => new NonNull(Type::string()),
'description' => 'The id of the character.',
],
'name' => [
'type' => Type::string(),
'description' => 'The name of the character.',
],
'friends' => array(
'type' => Type::listOf($characterType),
'description' => 'The friends of the character, or an empty list if they have none.',
'resolve' => $friendsResolver,
),
];
},
]);
/**
* This implements the following type system shorthand:
* type Query {
* character(id: String!): Character
* }
*
*/
$queryType = new ObjectType([
'name' => 'Query',
'fields' => [
'character' => [
'type' => $characterType,
'args' => [
'id' => [
'name' => 'id',
'description' => 'id of the character',
'type' => Type::nonNull(Type::string())
]
],
'resolve' => $characterResolver,
],
]
]);
return new Schema(['query' => $queryType]);
}
function executeQueries(Schema $schema, &$calls, &$callsIds, PromiseAdapter $promiseAdapter = null, callable $beforeExecute = null, callable $afterExecute = null)
{
foreach (getQueries() as $query) {
$calls = 0;
$callsIds = [];
if (null !== $beforeExecute) { $beforeExecute(); }
if (null === $promiseAdapter) {
$result = GraphQL::executeQuery($schema, $query);
} else {
$promise = GraphQL::promiseToExecute($promiseAdapter, $schema, $query);
if ($promiseAdapter instanceof \GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter) {
$result = $promiseAdapter->wait($promise)->toArray();
} else {
$result = $promise->then(static function ($r) {
return $r->toArray();
});
}
}
if (null !== $afterExecute) { $result = $afterExecute($result); }
renderMessage($query, $result, $calls, $callsIds);
}
}
function getQueries()
{
$queries = [];
$queries[] = <<<QUERY
{
character1: character(id: "1000") {
id
name
friends {
id
name
}
}
character2: character(id: "1002") {
id
name
friends {
id
name
}
}
}
QUERY;
$queries[] = <<<QUERY
{
character1: character(id: "1000") {
id
name
}
character2: character(id: "1002") {
id
name
}
}
QUERY;
return $queries;
}