Skip to content

Commit

Permalink
Add support for all graphql-rate-limit options (ardatan#2276)
Browse files Browse the repository at this point in the history
* 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 <v.cocaud@gmail.com>

* Spread in options since options are now at the root level.

* Add changeset

* Update readme to reflect recent change

---------

Co-authored-by: Valentin Cocaud <v.cocaud@gmail.com>
  • Loading branch information
deggertsen and EmrysMyrddin authored Aug 20, 2024
1 parent e77fc83 commit ba368ba
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/red-cameras-help.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions packages/plugins/rate-limiter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 18 additions & 3 deletions packages/plugins/rate-limiter/src/index.ts
Original file line number Diff line number Diff line change
@@ -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<ContextType = unknown> = (context: ContextType) => string;
Expand All @@ -24,14 +36,17 @@ export type RateLimiterPluginOptions = {
context: unknown;
info: GraphQLResolveInfo;
}) => void;
};
} & Omit<GraphQLRateLimitConfig, 'identifyContext'>;

interface RateLimiterContext {
rateLimiterFn: ReturnType<typeof getGraphQLRateLimiter>;
}

export const useRateLimiter = (options: RateLimiterPluginOptions): Plugin<RateLimiterContext> => {
const rateLimiterFn = getGraphQLRateLimiter({ identifyContext: options.identifyFn });
const rateLimiterFn = getGraphQLRateLimiter({
...options, // Pass through all available options
identifyContext: options.identifyFn,
});

return {
onPluginInit({ addPlugin }) {
Expand Down

0 comments on commit ba368ba

Please sign in to comment.