-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
ExecutionResult.php
185 lines (165 loc) · 5.09 KB
/
ExecutionResult.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php declare(strict_types=1);
namespace GraphQL\Executor;
use GraphQL\Error\DebugFlag;
use GraphQL\Error\Error;
use GraphQL\Error\FormattedError;
/**
* Returned after [query execution](executing-queries.md).
* Represents both - result of successful execution and of a failed one
* (with errors collected in `errors` prop).
*
* Could be converted to [spec-compliant](https://facebook.github.io/graphql/#sec-Response-Format)
* serializable array using `toArray()`.
*
* @phpstan-type SerializableError array{
* message: string,
* locations?: array<int, array{line: int, column: int}>,
* path?: array<int, int|string>,
* extensions?: array<string, mixed>
* }
* @phpstan-type SerializableErrors list<SerializableError>
* @phpstan-type SerializableResult array{
* data?: array<string, mixed>,
* errors?: SerializableErrors,
* extensions?: array<string, mixed>
* }
* @phpstan-type ErrorFormatter callable(\Throwable): SerializableError
* @phpstan-type ErrorsHandler callable(list<Error> $errors, ErrorFormatter $formatter): SerializableErrors
*
* @see \GraphQL\Tests\Executor\ExecutionResultTest
*/
class ExecutionResult implements \JsonSerializable
{
/**
* Data collected from resolvers during query execution.
*
* @api
*
* @var array<string, mixed>|null
*/
public ?array $data = null;
/**
* Errors registered during query execution.
*
* If an error was caused by exception thrown in resolver, $error->getPrevious() would
* contain original exception.
*
* @api
*
* @var list<Error>
*/
public array $errors = [];
/**
* User-defined serializable array of extensions included in serialized result.
*
* @api
*
* @var array<string, mixed>|null
*/
public ?array $extensions = null;
/**
* @var callable|null
*
* @phpstan-var ErrorFormatter|null
*/
private $errorFormatter;
/**
* @var callable|null
*
* @phpstan-var ErrorsHandler|null
*/
private $errorsHandler;
/**
* @param array<string, mixed>|null $data
* @param list<Error> $errors
* @param array<string, mixed> $extensions
*/
public function __construct(?array $data = null, array $errors = [], array $extensions = [])
{
$this->data = $data;
$this->errors = $errors;
$this->extensions = $extensions;
}
/**
* Define custom error formatting (must conform to http://facebook.github.io/graphql/#sec-Errors).
*
* Expected signature is: function (GraphQL\Error\Error $error): array
*
* Default formatter is "GraphQL\Error\FormattedError::createFromException"
*
* Expected returned value must be an array:
* array(
* 'message' => 'errorMessage',
* // ... other keys
* );
*
* @phpstan-param ErrorFormatter|null $errorFormatter
*
* @api
*/
public function setErrorFormatter(?callable $errorFormatter): self
{
$this->errorFormatter = $errorFormatter;
return $this;
}
/**
* Define custom logic for error handling (filtering, logging, etc).
*
* Expected handler signature is:
* fn (array $errors, callable $formatter): array
*
* Default handler is:
* fn (array $errors, callable $formatter): array => array_map($formatter, $errors)
*
* @phpstan-param ErrorsHandler|null $errorsHandler
*
* @api
*/
public function setErrorsHandler(?callable $errorsHandler): self
{
$this->errorsHandler = $errorsHandler;
return $this;
}
/** @phpstan-return SerializableResult */
#[\ReturnTypeWillChange]
public function jsonSerialize(): array
{
return $this->toArray();
}
/**
* Converts GraphQL query result to spec-compliant serializable array using provided
* errors handler and formatter.
*
* If debug argument is passed, output of error formatter is enriched which debugging information
* ("debugMessage", "trace" keys depending on flags).
*
* $debug argument must sum of flags from @see \GraphQL\Error\DebugFlag
*
* @phpstan-return SerializableResult
*
* @api
*/
public function toArray(int $debug = DebugFlag::NONE): array
{
$result = [];
if ($this->errors !== []) {
$errorsHandler = $this->errorsHandler
?? static fn (array $errors, callable $formatter): array => \array_map($formatter, $errors);
$handledErrors = $errorsHandler(
$this->errors,
FormattedError::prepareFormatter($this->errorFormatter, $debug)
);
// While we know that there were errors initially, they might have been discarded
if ($handledErrors !== []) {
$result['errors'] = $handledErrors;
}
}
if ($this->data !== null) {
$result['data'] = $this->data;
}
if ($this->extensions !== null && $this->extensions !== []) {
$result['extensions'] = $this->extensions;
}
return $result;
}
}