Skip to content

Commit 3ff6bdb

Browse files
authored
Merge pull request #34 from moufmouf/httpcodedecider
Migrating HTTP code decider to GraphQLite core library
2 parents db5b5d2 + 3de2d6b commit 3ff6bdb

File tree

1 file changed

+4
-38
lines changed

1 file changed

+4
-38
lines changed

Controller/GraphqliteController.php

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace TheCodingMachine\Graphqlite\Bundle\Controller;
55

66

7+
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
78
use function array_map;
89
use GraphQL\Error\ClientAware;
910
use GraphQL\Error\Debug;
@@ -100,15 +101,16 @@ private function handlePsr7Request(ServerRequestInterface $request, Request $sym
100101
$standardService = new StandardServer($serverConfig);
101102
$result = $standardService->executePsrRequest($request);
102103

104+
$httpCodeDecider = new HttpCodeDecider();
103105
if ($result instanceof ExecutionResult) {
104-
return new JsonResponse($result->toArray($this->debug), $this->decideHttpStatusCode($result));
106+
return new JsonResponse($result->toArray($this->debug), $httpCodeDecider->decideHttpStatusCode($result));
105107
}
106108
if (is_array($result)) {
107109
$finalResult = array_map(function (ExecutionResult $executionResult) {
108110
return $executionResult->toArray($this->debug);
109111
}, $result);
110112
// Let's return the highest result.
111-
$statuses = array_map([$this, 'decideHttpStatusCode'], $result);
113+
$statuses = array_map([$httpCodeDecider, 'decideHttpStatusCode'], $result);
112114
$status = max($statuses);
113115
return new JsonResponse($finalResult, $status);
114116
}
@@ -117,40 +119,4 @@ private function handlePsr7Request(ServerRequestInterface $request, Request $sym
117119
}
118120
throw new RuntimeException('Unexpected response from StandardServer::executePsrRequest'); // @codeCoverageIgnore
119121
}
120-
121-
/**
122-
* Decides the HTTP status code based on the answer.
123-
*
124-
* @see https://github.com/APIs-guru/graphql-over-http#status-codes
125-
*/
126-
private function decideHttpStatusCode(ExecutionResult $result): int
127-
{
128-
// 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.
129-
if ($result->data !== null || empty($result->errors)) {
130-
return 200;
131-
}
132-
133-
$status = 0;
134-
// There might be many errors. Let's return the highest code we encounter.
135-
foreach ($result->errors as $error) {
136-
$wrappedException = $error->getPrevious();
137-
if ($wrappedException !== null) {
138-
$code = $wrappedException->getCode();
139-
if (!isset(Response::$statusTexts[$code])) {
140-
// The exception code is not a valid HTTP code. Let's ignore it
141-
continue;
142-
}
143-
} else {
144-
$code = 400;
145-
}
146-
$status = max($status, $code);
147-
}
148-
149-
// If exceptions have been thrown and they have not a "HTTP like code", let's throw a 500.
150-
if ($status < 200) {
151-
$status = 500;
152-
}
153-
154-
return $status;
155-
}
156122
}

0 commit comments

Comments
 (0)