Skip to content

Commit 4af0ff0

Browse files
committed
refactor: increase accuracy for isExecutionResult and makeResponse
1 parent d1ffdf7 commit 4af0ff0

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/handler.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -534,25 +534,12 @@ export function makeResponse(
534534
| Readonly<GraphQLError>,
535535
acceptedMediaType: AcceptableMediaType,
536536
): Response {
537-
if (!('data' in resultOrErrors)) {
537+
if (isExecutionResult(resultOrErrors)) {
538538
return [
539-
JSON.stringify({
540-
errors: Array.isArray(resultOrErrors)
541-
? isObject(resultOrErrors)
542-
? resultOrErrors
543-
: new GraphQLError(String(resultOrErrors))
544-
: [resultOrErrors],
545-
}),
539+
JSON.stringify(resultOrErrors),
546540
{
547-
...(acceptedMediaType === 'application/json'
548-
? {
549-
status: 200,
550-
statusText: 'OK',
551-
}
552-
: {
553-
status: 400,
554-
statusText: 'Bad Request',
555-
}),
541+
status: 200,
542+
statusText: 'OK',
556543
headers: {
557544
'content-type':
558545
acceptedMediaType === 'application/json'
@@ -564,10 +551,23 @@ export function makeResponse(
564551
}
565552

566553
return [
567-
JSON.stringify(resultOrErrors),
554+
JSON.stringify({
555+
errors: Array.isArray(resultOrErrors)
556+
? isObject(resultOrErrors)
557+
? resultOrErrors
558+
: new GraphQLError(String(resultOrErrors))
559+
: [resultOrErrors],
560+
}),
568561
{
569-
status: 200,
570-
statusText: 'OK',
562+
...(acceptedMediaType === 'application/json'
563+
? {
564+
status: 200,
565+
statusText: 'OK',
566+
}
567+
: {
568+
status: 400,
569+
statusText: 'Bad Request',
570+
}),
571571
headers: {
572572
'content-type':
573573
acceptedMediaType === 'application/json'

src/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export function areGraphQLErrors(obj: unknown): obj is readonly GraphQLError[] {
2525
/** @private */
2626
export function isExecutionResult(val: unknown): val is ExecutionResult {
2727
return (
28-
isObject(val) && ('data' in val || 'errors' in val || 'extensions' in val)
28+
isObject(val) &&
29+
('data' in val || ('data' in val && val.data == null && 'errors' in val))
2930
);
3031
}
3132

0 commit comments

Comments
 (0)