1- import Redis , { RedisOptions } from 'ioredis' ;
21import { parse , validate } from 'graphql' ;
2+ import { RedisOptions } from 'ioredis' ;
33import { GraphQLSchema } from 'graphql/type/schema' ;
44import { Request , Response , NextFunction , RequestHandler } from 'express' ;
55
@@ -8,6 +8,7 @@ import setupRateLimiter from './rateLimiterSetup';
88import getQueryTypeComplexity from '../analysis/typeComplexityAnalysis' ;
99import { RateLimiterOptions , RateLimiterSelection } from '../@types/rateLimit' ;
1010import { TypeWeightConfig } from '../@types/buildTypeWeights' ;
11+ import { connect } from '../utils/redis' ;
1112
1213// FIXME: Will the developer be responsible for first parsing the schema from a file?
1314// Can consider accepting a string representing a the filepath to a schema
@@ -40,7 +41,9 @@ export function expressRateLimiter(
4041 // TODO: Throw ValidationError if schema is invalid
4142 const typeWeightObject = buildTypeWeightsFromSchema ( schema , typeWeightConfig ) ;
4243 // TODO: Throw error if connection is unsuccessful
43- const redisClient = new Redis ( redisClientOptions ) ; // Default port is 6379 automatically
44+ // Default connection timeout is 10000 ms of inactivity
45+ // FIXME: Do we need to re-establish connection?
46+ const redisClient = connect ( redisClientOptions ) ; // Default port is 6379 automatically
4447 const rateLimiter = setupRateLimiter ( rateLimiterAlgo , rateLimiterOptions , redisClient ) ;
4548
4649 // return the rate limiting middleware
@@ -66,7 +69,7 @@ export function expressRateLimiter(
6669 * req.ip and req.ips will worx in express but not with other frameworks
6770 */
6871 // check for a proxied ip address before using the ip address on request
69- const ip : string = req . ips [ 0 ] || req . ip ;
72+ const ip : string = req . ips ? req . ips [ 0 ] : req . ip ;
7073
7174 // FIXME: this will only work with type complexity
7275 const queryAST = parse ( query ) ;
@@ -80,8 +83,8 @@ export function expressRateLimiter(
8083
8184 const queryComplexity = getQueryTypeComplexity ( queryAST , variables , typeWeightObject ) ;
8285 try {
83- // process the request and conditinoally respond to client with status code 429 o
84- // r pass the request onto the next middleware function
86+ // process the request and conditinoally respond to client with status code 429 or
87+ // pass the request onto the next middleware function
8588 const rateLimiterResponse = await rateLimiter . processRequest (
8689 ip ,
8790 requestTimestamp ,
0 commit comments