-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathHttpCodeDecider.php
More file actions
55 lines (47 loc) · 2 KB
/
HttpCodeDecider.php
File metadata and controls
55 lines (47 loc) · 2 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
<?php
declare(strict_types=1);
namespace TheCodingMachine\GraphQLite\Http;
use GraphQL\Error\ClientAware;
use GraphQL\Executor\ExecutionResult;
use function max;
class HttpCodeDecider implements HttpCodeDeciderInterface
{
/**
* Decides the HTTP status code based on the answer.
*
* @see https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#status-codes
*/
public function decideHttpStatusCode(ExecutionResult $result): int
{
// If the data entry in the response has any value other than null (when the operation has successfully executed without error) then the response should use the 200 (OK) status code.
if ($result->data !== null && empty($result->errors)) {
return 200;
}
$status = 0;
// There might be many errors. Let's return the highest code we encounter.
foreach ($result->errors as $error) {
$wrappedException = $error->getPrevious();
if ($wrappedException !== null) {
$code = $wrappedException->getCode();
if ($code < 400 || $code >= 600) {
if (! ($wrappedException instanceof ClientAware) || $wrappedException->isClientSafe() !== true) {
// The exception code is not a valid HTTP code. Let's ignore it
continue;
}
// A "client aware" exception is almost certainly targeting the client (there is
// no need to pass a server exception error message to the client).
// So a ClientAware exception is almost certainly a HTTP 400 code
$code = 400;
}
} else {
$code = 400;
}
$status = (int) max($status, $code);
}
// If exceptions have been thrown, and they have not an "HTTP like code", let's throw a 500.
if ($status < 200) {
$status = 500;
}
return $status;
}
}