
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
graphql-rate-limit
Advanced tools
A GraphQL directive to add basic but granular rate limiting to your Queries or Mutations.
max
per window
argumentsyarn add graphql-rate-limit
directive @rateLimit(
max: Int,
window: String,
message: String,
identityArgs: [String],
) on FIELD_DEFINITION
type Query {
# Rate limit to 5 per second
getItems: [Item] @rateLimit(window: "1s", max: 5)
# Rate limit access per item ID
getItem(id: ID!): Item @rateLimit(identityArgs: ["id"])
}
type Mutation {
# Rate limit with a custom error message
createItem(title: String!): Item @rateLimit(message: "You are doing that too often.")
}
Create a configured GraphQLRateLimit class.
const { createRateLimitDirective } = require('graphql-rate-limit');
// OR
import { createRateLimitDirective } from 'graphql-rate-limit';
const GraphQLRateLimit = createRateLimitDirective({
/**
* `identifyContext` is required and used to identify the user/client. The most likely cases
* are either using the context's request.ip, or the user ID on the context.
* A function that accepts the context and returns a string that is used to identify the user.
*/
identifyContext: (ctx) => ctx.user.id, // Or could be something like: return ctx.req.ip;
/**
* `store` is optional as it defaults to an InMemoryStore. See the implementation of InMemoryStore if
* you'd like to implement your own with your own database.
*/
store: new MyCustomStore(),
/**
* Generate a custom error message. Note that the `message` passed in to the directive will be used
* if its set.
*/
formatError: ({ fieldName }) => {
return `Woah there, you are doing way too much ${fieldName}`
}
});
Add GraphQLRateLimit to your GraphQL server configuration. Example using Apollo Server:
const server = new ApolloServer({
typeDefs,
resolvers,
schemaDirectives: {
rateLimit: GraphQLRateLimit
}
});
Use in your GraphQL Schema.
# This must be added to the top of your schema.
directive @rateLimit(
max: Int,
window: String,
message: String,
identityArgs: [String],
) on FIELD_DEFINITION
type Query {
# Limit queries to getThings to 10 per minute.
getThings: [Thing] @rateLimit(max: 10, window: "6s")
}
type Query {
# Limit attempts to login with a particular email to 10 per 2 hours.
login(email: String!, password: String!): String @rateLimit(max: 10, window: "2h", identityArgs: ["email"])
}
Note: We use Zeit's ms
to parse the window
arg, docs here.
It is recommended to use a persistent store rather than the default InMemoryStore. GraphQLRateLimit supports Redis. You need to install Redis in your project first.
import * as graphqlRateLimit from 'graphql-rate-limit';
const GraphQLRateLimit = graphqlRateLimit.createRateLimitDirective({
identifyContext: ctx => ctx.user.id,
/**
* Import the class from graphql-rate-limit and pass in an instance of redis client to the constructor
*/
store: new graphqlRateLimit.RedisStore(redis.createClient())
});
FAQs
Add Rate Limiting To Your GraphQL Resolvers πββ
The npm package graphql-rate-limit receives a total of 27,773 weekly downloads. As such, graphql-rate-limit popularity was classified as popular.
We found that graphql-rate-limit demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.Β It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezerβs API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.