From ba368bac9539177689eb9e561f388cff7f0cf048 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 20 Aug 2024 04:39:10 -0400 Subject: [PATCH] Add support for all graphql-rate-limit options (#2276) * Add store support on rate limiter * Allow passing all rate limit config options to graphql-rate-limit * Add to the notes in the readme to explain that other props are available * Export graphql-rate-limit types * Add comment to explain that we are passing through all available options * Update packages/plugins/rate-limiter/src/index.ts Join the two types Co-authored-by: Valentin Cocaud * Spread in options since options are now at the root level. * Add changeset * Update readme to reflect recent change --------- Co-authored-by: Valentin Cocaud --- .changeset/red-cameras-help.md | 6 ++++++ packages/plugins/rate-limiter/README.md | 3 +++ packages/plugins/rate-limiter/src/index.ts | 21 ++++++++++++++++++--- 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 .changeset/red-cameras-help.md diff --git a/.changeset/red-cameras-help.md b/.changeset/red-cameras-help.md new file mode 100644 index 0000000000000..dbaa620372dde --- /dev/null +++ b/.changeset/red-cameras-help.md @@ -0,0 +1,6 @@ +--- +'@envelop/rate-limiter': patch +--- + +useRateLimiter will now accept all options available to graphql-rate-limit getGraphQLRateLimiter +function so that they are usable. diff --git a/packages/plugins/rate-limiter/README.md b/packages/plugins/rate-limiter/README.md index 2be945a611802..73a5450be6f51 100644 --- a/packages/plugins/rate-limiter/README.md +++ b/packages/plugins/rate-limiter/README.md @@ -65,4 +65,7 @@ type Query { ## Notes +All options available to the graphql-rate-limit getGraphQLRateLimiter function may also be passed +into useRateLimiter. + You can find more details here: https://github.com/teamplanes/graphql-rate-limit#readme diff --git a/packages/plugins/rate-limiter/src/index.ts b/packages/plugins/rate-limiter/src/index.ts index 426d4186f90be..586c1dd4c57bb 100644 --- a/packages/plugins/rate-limiter/src/index.ts +++ b/packages/plugins/rate-limiter/src/index.ts @@ -1,11 +1,23 @@ import { GraphQLResolveInfo, IntValueNode, StringValueNode } from 'graphql'; -import { getGraphQLRateLimiter } from 'graphql-rate-limit'; +import { getGraphQLRateLimiter, GraphQLRateLimitConfig } from 'graphql-rate-limit'; import { Plugin } from '@envelop/core'; import { useOnResolve } from '@envelop/on-resolve'; import { getDirective } from './utils.js'; export * from './utils.js'; +export { + FormatErrorInput, + GraphQLRateLimitConfig, + GraphQLRateLimitDirectiveArgs, + Identity, + InMemoryStore, + Options, + RateLimitError, + RedisStore, + Store, +} from 'graphql-rate-limit'; + export class UnauthenticatedError extends Error {} export type IdentifyFn = (context: ContextType) => string; @@ -24,14 +36,17 @@ export type RateLimiterPluginOptions = { context: unknown; info: GraphQLResolveInfo; }) => void; -}; +} & Omit; interface RateLimiterContext { rateLimiterFn: ReturnType; } export const useRateLimiter = (options: RateLimiterPluginOptions): Plugin => { - const rateLimiterFn = getGraphQLRateLimiter({ identifyContext: options.identifyFn }); + const rateLimiterFn = getGraphQLRateLimiter({ + ...options, // Pass through all available options + identifyContext: options.identifyFn, + }); return { onPluginInit({ addPlugin }) {