Skip to content

Commit 16c9600

Browse files
committed
fix: Request parameters query field can only be a string
Closes enisdenjo#65
1 parent fed0f01 commit 16c9600

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

src/common.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
*/
66

7-
import type { DocumentNode, GraphQLError } from 'graphql';
7+
import type { GraphQLError } from 'graphql';
88
import { isObject } from './utils';
99

1010
/**
@@ -36,7 +36,7 @@ export const TOKEN_QUERY_KEY = 'token' as const;
3636
*/
3737
export interface RequestParams {
3838
operationName?: string;
39-
query: DocumentNode | string;
39+
query: string;
4040
variables?: Record<string, unknown>;
4141
extensions?: Record<string, unknown>;
4242
}

src/handler.ts

+23-24
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
validate as graphqlValidate,
1414
execute as graphqlExecute,
1515
subscribe as graphqlSubscribe,
16+
DocumentNode,
1617
} from 'graphql';
1718
import { isObject } from './utils';
1819
import {
@@ -605,31 +606,29 @@ export function createHandler<
605606
if (!schema) throw new Error('The GraphQL schema is not provided');
606607

607608
const { operationName, variables } = params;
608-
let { query } = params;
609+
let query: DocumentNode;
609610

610-
if (typeof query === 'string') {
611-
try {
612-
query = parse(query);
613-
} catch (err) {
614-
return [
615-
JSON.stringify({
616-
errors: [
617-
err instanceof Error
618-
? {
619-
message: err.message,
620-
// TODO: stack might leak sensitive information
621-
// stack: err.stack,
622-
}
623-
: err,
624-
],
625-
}),
626-
{
627-
status: 400,
628-
statusText: 'Bad Request',
629-
headers: { 'content-type': 'application/json; charset=utf-8' },
630-
},
631-
];
632-
}
611+
try {
612+
query = parse(params.query);
613+
} catch (err) {
614+
return [
615+
JSON.stringify({
616+
errors: [
617+
err instanceof Error
618+
? {
619+
message: err.message,
620+
// TODO: stack might leak sensitive information
621+
// stack: err.stack,
622+
}
623+
: err,
624+
],
625+
}),
626+
{
627+
status: 400,
628+
statusText: 'Bad Request',
629+
headers: { 'content-type': 'application/json; charset=utf-8' },
630+
},
631+
];
633632
}
634633

635634
const argsWithoutSchema = {

0 commit comments

Comments
 (0)