💂♀️ GraphQL Rate Limit 💂♂️
A GraphQL directive to add basic but granular rate limiting to your Queries or Mutations.
Features
- 💂♀️ Add rate limits to queries or mutations
- 🔑 Add filters to rate limits based on the query or mutation args
- ❌ Custom error messaging
- ⏰ Configure using a simple
max
per window
arguments - 💼 Custom stores, use Redis, Postgres, Mongo... it defaults to in-memory
- 💪 Written in TypeScript
Install
yarn add graphql-rate-limit
Example
directive @rateLimit(
max: Int,
window: Int,
message: String,
identityArgs: [String],
) on FIELD_DEFINITION
type Query {
getItems: [Item] @rateLimit(window: 1000, max: 5)
getItem(id: ID!): Item @rateLimit(identityArgs: ["id"])
}
type Mutation {
createItem(title: String!): Item @rateLimit(message: "You are doing that too often.")
}
Usage
Step 1.
Create a configured GraphQLRateLimit class.
const { createRateLimitDirective } = require('graphql-rate-limit');
import { createRateLimitDirective } from 'graphql-rate-limit';
const GraphQLRateLimit = createRateLimitDirective({
identifyContext: (ctx) => ctx.user.id,
store: new MyCustomStore(),
});
Step 2.
Add GraphQLRateLimit to your GraphQL server configuration. Example using Apollo Server:
const server = new ApolloServer({
typeDefs,
resolvers,
schemaDirectives: {
rateLimit: GraphQLRateLimit
}
});
Step 3.
Use in your GraphQL Schema.
directive @rateLimit(
max: Int,
window: Int,
message: String,
identityArgs: [String],
) on FIELD_DEFINITION
type Query {
getThings: [Thing] @rateLimit(max: 10, window: 60000)
}
type Query {
login(email: String!, password: String!): String @rateLimit(max: 10, window: 60000, identityArgs: ["email"])
}