1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\DataCollector; |
6
|
|
|
|
7
|
|
|
use GraphQL\Error\SyntaxError; |
8
|
|
|
use GraphQL\Language\AST\DocumentNode; |
9
|
|
|
use GraphQL\Language\AST\OperationDefinitionNode; |
10
|
|
|
use GraphQL\Language\Parser; |
11
|
|
|
use Overblog\GraphQLBundle\Event\ExecutorResultEvent; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
15
|
|
|
|
16
|
|
|
class GraphQLCollector extends DataCollector |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* GraphQL Batchs executed. |
20
|
|
|
*/ |
21
|
|
|
protected $batches = []; |
22
|
|
|
|
23
|
|
|
public function collect(Request $request, Response $response, \Throwable $exception = null): void |
24
|
|
|
{ |
25
|
|
|
$error = false; |
26
|
|
|
$count = 0; |
27
|
|
|
foreach ($this->batches as $batch) { |
28
|
|
|
if (isset($batch['error'])) { |
29
|
|
|
$error = true; |
30
|
|
|
} |
31
|
|
|
$count += $batch['count']; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->data = [ |
35
|
|
|
'schema' => $request->attributes->get('_route_params')['schemaName'] ?? 'default', |
36
|
|
|
'batches' => $this->batches, |
37
|
|
|
'count' => $count, |
38
|
|
|
'error' => $error, |
39
|
|
|
]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Check if we have an error. |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public function getError() |
48
|
|
|
{ |
49
|
|
|
return $this->data['error'] ?? false; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Count the number of executed queries. |
54
|
|
|
* |
55
|
|
|
* @return int |
56
|
|
|
*/ |
57
|
|
|
public function getCount() |
58
|
|
|
{ |
59
|
|
|
return $this->data['count'] ?? 0; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return the targeted schema. |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function getSchema() |
68
|
|
|
{ |
69
|
|
|
return $this->data['schema'] ?? 'default'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return the list of executed batch. |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function getBatches() |
78
|
|
|
{ |
79
|
|
|
return $this->data['batches'] ?? []; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function reset(): void |
86
|
|
|
{ |
87
|
|
|
$this->data = []; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function getName() |
94
|
|
|
{ |
95
|
|
|
return 'graphql'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Hook into the GraphQL events to populate the collector. |
100
|
|
|
* |
101
|
|
|
* @param ExecutorResultEvent $event |
102
|
|
|
*/ |
103
|
|
|
public function onPostExecutor(ExecutorResultEvent $event): void |
104
|
|
|
{ |
105
|
|
|
$executorArgument = $event->getExecutorArguments(); |
106
|
|
|
$queryString = $executorArgument->getRequestString(); |
107
|
|
|
$variables = $executorArgument->getVariableValue(); |
108
|
|
|
|
109
|
|
|
$result = $event->getResult()->toArray(); |
110
|
|
|
|
111
|
|
|
$batch = [ |
112
|
|
|
'queryString' => $queryString, |
113
|
|
|
'variables' => $this->cloneVar($variables), |
114
|
|
|
'result' => $this->cloneVar($result), |
115
|
|
|
'count' => 0, |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
try { |
119
|
|
|
$parsed = Parser::parse($queryString); |
120
|
|
|
$batch['graphql'] = $this->extractGraphql($parsed); |
121
|
|
|
if (isset($batch['graphql']['fields'])) { |
122
|
|
|
$batch['count'] += \count($batch['graphql']['fields']); |
123
|
|
|
} |
124
|
|
|
$error = $result['errors'][0] ?? false; |
125
|
|
|
if ($error) { |
126
|
|
|
$batch['error'] = [ |
127
|
|
|
'message' => $error['message'], |
128
|
|
|
'location' => $error['locations'][0] ?? false, |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
} catch (SyntaxError $error) { |
132
|
|
|
$location = $error->getLocations()[0] ?? false; |
133
|
|
|
$batch['error'] = ['message' => $error->getMessage(), 'location' => $location]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->batches[] = $batch; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Extract GraphQL Information from the documentNode². |
141
|
|
|
* |
142
|
|
|
* @param DocumentNode $document |
143
|
|
|
* @param array $result |
144
|
|
|
* @param array $variables |
145
|
|
|
* |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
protected function extractGraphql(DocumentNode $document) |
149
|
|
|
{ |
150
|
|
|
$operation = null; |
151
|
|
|
$fields = []; |
152
|
|
|
|
153
|
|
|
foreach ($document->definitions as $definition) { |
154
|
|
|
if ($definition instanceof OperationDefinitionNode) { |
155
|
|
|
$operation = $definition->operation; |
156
|
|
|
foreach ($definition->selectionSet->selections as $selection) { |
157
|
|
|
$name = $selection->name->value; |
|
|
|
|
158
|
|
|
$alias = $selection->alias ? $selection->alias->value : null; |
|
|
|
|
159
|
|
|
|
160
|
|
|
$fields[] = [ |
161
|
|
|
'name' => $name, |
162
|
|
|
'alias' => $alias, |
163
|
|
|
]; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return [ |
169
|
|
|
'operation' => $operation, |
170
|
|
|
'fields' => $fields, |
171
|
|
|
]; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|