- Overview
- Features
- Installation
- Usage
- FAQ and Troubleshooting
- Contributing
- Acknowledgments
- License
- Author
GraphQL Rate Limit Directive is a middleware for Apollo Server that enables rate limiting on GraphQL queries, providing control over request frequencies to enhance security and performance.
- Flexible Rate Limiting: Set limits on specific GraphQL fields based on unique user data.
- Customizable Key Generation: Determine uniqueness/isolation of operations using a custom key generator.
- Easy Integration: Seamlessly integrates with Apollo Server and GraphQL schemas.
- CSRF Prevention: Enhance security with Cross-Site Request Forgery prevention.
To install GraphQL Rate Limit Directive, use the following npm command:
npm install graphql-rate-limit-directive
- Import the required modules and GraphQL Rate Limit Directive.
- Configure the rate limiting directives using
rateLimitDirective
. - Define your GraphQL schema, applying rate limiting to specific fields.
- Start your Apollo Server instance with the configured schema.
// Import necessary modules and packages
const { rateLimitDirective } = require("graphql-rate-limit-directive");
const { ApolloServer } = require("apollo-server");
// ... (Other imports and configurations)
// Extract rateLimitDirectiveTypeDefs and rateLimitDirectiveTransformer from the rateLimitDirective module
const { rateLimitDirectiveTypeDefs, rateLimitDirectiveTransformer } =
rateLimitDirective({
keyGenerator,
});
// Create an executable schema with rate limiting directives
let schema = makeExecutableSchema({
typeDefs: [
rateLimitDirectiveTypeDefs,
// Your GraphQL schema definitions with rate limiting directives
],
resolvers,
});
// Apply rate limiting directives to the schema
schema = rateLimitDirectiveTransformer(schema);
// ... (Other configurations)
// Start Apollo Server on the specified port.
startApolloServer(port);
For more detailed information, refer to the GraphQL Rate Limit Directive Documentation.
Certainly! Here's an example of how you might structure a FAQ or Troubleshooting section for your GraphQL Rate Limit project:
A: This error occurs when the PORT
environment variable is not set in your .env
file. Ensure that you have a .env
file in the project root with the PORT
variable defined.
A: You can customize the rate limit by applying the @rateLimit
directive to specific fields in your GraphQL schema. Check "Step 3: Attach directive to field or object" in the Documentation for an example of how to apply rate limiting to specific fields.
A: Please make sure that you have correctly applied the rate limiting directives to your schema. Ensure that the rateLimitDirectiveTypeDefs
and rateLimitDirectiveTransformer
are properly integrated. Refer to the Usage section for a step-by-step guide.
A: Yes, you can selectively disable rate limiting for specific queries by not applying the @rateLimit
directive to those fields in your schema.
Contributions to this example are welcome! Feel free to submit issues or pull requests.
- Apollo Server
- GraphQL Tools
- Rate Limiter Flexible
- GraphQL Rate Limit Directive - Used for rate limiting in this project, and this implementation is based on the context demo by ravangen.
This project is licensed under the MIT License - see the LICENSE file for details.
Karl Horning: