Skip to content

Commit 2063e3f

Browse files
committed
middleware is now parseing and validating the query before processing complexity.
1 parent 0e9447f commit 2063e3f

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/middleware/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Redis, { RedisOptions } from 'ioredis';
2+
import { parse, validate } from 'graphql';
23
import { GraphQLSchema } from 'graphql/type/schema';
34
import { Request, Response, NextFunction, RequestHandler } from 'express';
45

@@ -62,8 +63,13 @@ export function expressRateLimiter(
6263
const ip: string = req.ips[0] || req.ip;
6364

6465
// FIXME: this will only work with type complexity
65-
// TODO: add query varibales parameter
66-
const queryComplexity = getQueryTypeComplexity(query, typeWeightObject);
66+
const queryAST = parse(query);
67+
// validate the query against the schema. The GraphQL validation function returns an array of errors.
68+
const validationErrors = validate(schema, queryAST);
69+
// check if the length of the returned GraphQL Errors array is greater than zero. If it is, there were errors. Call next so that the GraphQL server can handle those.
70+
if (validationErrors.length > 0) return next();
71+
72+
const queryComplexity = getQueryTypeComplexity(queryAST, variables, typeWeightObject);
6773

6874
try {
6975
// process the request and conditinoally respond to client with status code 429 o

0 commit comments

Comments
 (0)