Portara directive is a rate limiter / throttler for GraphQL
Portara is an open source rate limiter designed for easy use with Apollo Server, including other Apollo implementations (including Apollo-Server-Express
, Apollo-Server-Hapi
, Apollo-Server-Koa
, and Apollo-Server-Lambda
). By using GraphQL Directives, developers have the ability to easily implement multiple rate limiters with as little as four lines of code.
Node.js version 8.0.0+
Redis version 2.6.12+
With npm:
npm install --save portara
Note: Redis is a requirement for this package. You can visit Redis' Getting Started Page for information on getting started with Redis. If you are using multiple servers (or the serverless framework), we recommend using Redis Cloud.
import Portara from 'portara';
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req, res }) = ({ req, res }),
schemaDirectives: { portara: portara("TOKEN GOES BETWEEN THESE QUOTES") },
});
The token is optional. You can get a token from Portara.io with a quick sign up throught Github Oauth. The token grants access to modify your rate limiter / throttler without redeploying your app. If you do not plan on using this feature, leave the parameter empty.
const typeDefs = gql`
directive @portara(limit: Int!, per: ID!, throttle: ID!) on FIELD_DEFINITION | OBJECT
type Query { etc...
4. You can type out exactly how you want the rate limiter to work in plain English. Please note that the usage of any strings must be in DOUBLE QUOTES, and values default to seconds. Below are a few examples:
On Object Type
This implementation applies the Portara rate limiter on the entire Query Object (which includes the "hello" and "goodbye" field definitions).
The rate limiter limits 10 requests to the endpoint (per IP address) per every 5 seconds.
type Query @portara(limit: 10, per: "5 seconds", throttle: "0") {
hello: String!
goodbye: String!
}
Throttling
On Field Type
This implementation applies the Portara rate limiter on just the Field Defintion (just the "hello").
The rate limiter limits 300 requests to the endpoint (per IP address) per every 12 minutes.
type Query {
hello: String! @portara(limit: 300, per: "12 minutes", throttle: 0)
goodbye: String!
}
On Both
type Query @portara(limit: 10, per: "5 seconds", throttle: 0) {
hello: String! @portara(limit: 15, per: "5 seconds", throttle: 0)
goodbye: String!
}
Other Time Measurements
type Query @portara(limit: 12, per: "5 h", throttle: 0) {
hello: String! @portara(limit: 20, per: "94 ms", throttle: 0)
goodbye: String! @portara(limit: 90, per: "2 minutes", throttle: 0)
thankyou: String!
}
@Portara
Steve Frend: Steve's Github and Steve's LinkedIn
Todd Alexander: Todd's Github and Todd's LinkedIn
Cary L Chan: Cary's Github and Cary's LinkedIn
Alexander Infante: Alex's Github and Alex's LinkedIn