|
9 | 9 | ExecutionResult,
|
10 | 10 | GraphQLSchema,
|
11 | 11 | validate as graphqlValidate,
|
| 12 | + ValidationRule, |
| 13 | + specifiedRules, |
12 | 14 | execute as graphqlExecute,
|
13 | 15 | parse as graphqlParse,
|
14 | 16 | DocumentNode,
|
@@ -203,6 +205,25 @@ export interface HandlerOptions<
|
203 | 205 | * Will not be used when implementing a custom `onSubscribe`.
|
204 | 206 | */
|
205 | 207 | validate?: typeof graphqlValidate;
|
| 208 | + /** |
| 209 | + * The validation rules for running GraphQL validate. |
| 210 | + * |
| 211 | + * When providing an array, the rules will be APPENDED to the default |
| 212 | + * `specifiedRules` array provided by the graphql-js module. |
| 213 | + * |
| 214 | + * Alternatively, providing a function instead will OVERWRITE the defaults |
| 215 | + * and use exclusively the rules returned by the function. The third (last) |
| 216 | + * argument of the function are the default `specifiedRules` array provided |
| 217 | + * by the graphql-js module, you're free to prepend/append the defaults to |
| 218 | + * your rule set, or omit them altogether. |
| 219 | + */ |
| 220 | + validationRules?: |
| 221 | + | readonly ValidationRule[] |
| 222 | + | (( |
| 223 | + req: Request<RequestRaw, RequestContext>, |
| 224 | + args: OperationArgs<Context>, |
| 225 | + specifiedRules: readonly ValidationRule[], |
| 226 | + ) => Promise<readonly ValidationRule[]> | readonly ValidationRule[]); |
206 | 227 | /**
|
207 | 228 | * Is the `execute` function from GraphQL which is
|
208 | 229 | * used to execute the query and mutation operations.
|
@@ -373,6 +394,7 @@ export function createHandler<
|
373 | 394 | schema,
|
374 | 395 | context,
|
375 | 396 | validate = graphqlValidate,
|
| 397 | + validationRules = [], |
376 | 398 | execute = graphqlExecute,
|
377 | 399 | parse = graphqlParse,
|
378 | 400 | getOperationAST = graphqlGetOperationAST,
|
@@ -545,7 +567,12 @@ export function createHandler<
|
545 | 567 | };
|
546 | 568 | }
|
547 | 569 |
|
548 |
| - const validationErrs = validate(args.schema, args.document); |
| 570 | + const validationErrs = validate(args.schema, args.document, [ |
| 571 | + ...specifiedRules, |
| 572 | + ...(typeof validationRules === 'function' |
| 573 | + ? await validationRules(req, args, specifiedRules) |
| 574 | + : validationRules), |
| 575 | + ]); |
549 | 576 | if (validationErrs.length) {
|
550 | 577 | return makeResponse(validationErrs, acceptedMediaType);
|
551 | 578 | }
|
|
0 commit comments