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