Skip to content

Commit 17c8007

Browse files
committed
fix(handler): Don't validate if onSubscribe returns execution arguments
1 parent 0dcaf89 commit 17c8007

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

docs/interfaces/HandlerOptions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ If you return `ExecutionArgs` from the callback, it will be used instead of
140140
trying to build one internally. In this case, you are responsible for providing
141141
a ready set of arguments which will be directly plugged in the operation execution.
142142

143+
You *must* validate the `ExecutionArgs` yourself if returning them.
144+
143145
If you return an array of `GraphQLError` from the callback, they will be reported
144146
to the client while complying with the spec.
145147

src/__tests__/server.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,7 @@ describe('Request', () => {
108108
describe('GET', () => {
109109
it('must not allow executing mutations', async () => {
110110
const url = new URL(serverUrl);
111-
url.searchParams.set(
112-
'query',
113-
'mutation { f10d019f15804f92a7c7470205c866da }', // making sure the field doesnt exist
114-
);
111+
url.searchParams.set('query', 'mutation { __typename }');
115112

116113
const res = await fetch(url.toString(), {
117114
headers: {

src/handler.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export interface HandlerOptions<RawRequest = unknown> {
7777
/**
7878
* A custom GraphQL validate function allowing you to apply your
7979
* own validation rules.
80+
*
81+
* Will not be used when implementing a custom `onSubscribe`.
8082
*/
8183
validate?: typeof graphqlValidate;
8284
/**
@@ -104,6 +106,8 @@ export interface HandlerOptions<RawRequest = unknown> {
104106
* trying to build one internally. In this case, you are responsible for providing
105107
* a ready set of arguments which will be directly plugged in the operation execution.
106108
*
109+
* You *must* validate the `ExecutionArgs` yourself if returning them.
110+
*
107111
* If you return an array of `GraphQLError` from the callback, they will be reported
108112
* to the client while complying with the spec.
109113
*
@@ -485,6 +489,30 @@ export function createHandler<RawRequest = unknown>(
485489
schema,
486490
};
487491
}
492+
493+
const validationErrs = validate(args.schema, args.document);
494+
if (validationErrs.length) {
495+
return [
496+
JSON.stringify({ errors: validationErrs }),
497+
{
498+
...(acceptedMediaType === 'application/json'
499+
? {
500+
status: 200,
501+
statusText: 'OK',
502+
}
503+
: {
504+
status: 400,
505+
statusText: 'Bad Request',
506+
}),
507+
headers: {
508+
'content-type':
509+
acceptedMediaType === 'application/json'
510+
? 'application/json; charset=utf-8'
511+
: 'application/graphql+json; charset=utf-8',
512+
},
513+
},
514+
];
515+
}
488516
}
489517

490518
let operation: OperationTypeNode;
@@ -542,30 +570,6 @@ export function createHandler<RawRequest = unknown>(
542570
args.contextValue = maybeResOrContext;
543571
}
544572

545-
const validationErrs = validate(args.schema, args.document);
546-
if (validationErrs.length) {
547-
return [
548-
JSON.stringify({ errors: validationErrs }),
549-
{
550-
...(acceptedMediaType === 'application/json'
551-
? {
552-
status: 200,
553-
statusText: 'OK',
554-
}
555-
: {
556-
status: 400,
557-
statusText: 'Bad Request',
558-
}),
559-
headers: {
560-
'content-type':
561-
acceptedMediaType === 'application/json'
562-
? 'application/json; charset=utf-8'
563-
: 'application/graphql+json; charset=utf-8',
564-
},
565-
},
566-
];
567-
}
568-
569573
let result = await execute(args);
570574
const maybeResOrResult = await onOperation?.(req, args, result);
571575
if (isResponse(maybeResOrResult)) return maybeResOrResult;

0 commit comments

Comments
 (0)