How to use graphql-rate-limit - 6 common examples

To help you get started, we’ve selected a few graphql-rate-limit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github teamplanes / graphql-rate-limit / example / index.js View on Github external
const { shield } = require('graphql-shield');
const { applyMiddleware } = require('graphql-middleware');
const {
  createRateLimitDirective,
  RedisStore,
  getGraphQLRateLimiter,
  createRateLimitRule
} = require('graphql-rate-limit');
const redis = require('redis');

// Option 1: Use a directive (applied in the schema below)
const rateLimitDirective = createRateLimitDirective({
  identifyContext: context => {
    return context.req.ip;
  },
  store: new RedisStore(redis.createClient())
});

// Option 2: User graphql-shield (applied in the `shield` below)
const rateLimit = createRateLimitRule({
  formatError: () => {
    return 'Stop doing that so often.';
  },
  identifyContext: context => {
    return context.req.ip;
  }
});

const permissions = shield({
  Query: {
    myId: rateLimit({
      max: 2,
github teamplanes / graphql-rate-limit / example / index.js View on Github external
RedisStore,
  getGraphQLRateLimiter,
  createRateLimitRule
} = require('graphql-rate-limit');
const redis = require('redis');

// Option 1: Use a directive (applied in the schema below)
const rateLimitDirective = createRateLimitDirective({
  identifyContext: context => {
    return context.req.ip;
  },
  store: new RedisStore(redis.createClient())
});

// Option 2: User graphql-shield (applied in the `shield` below)
const rateLimit = createRateLimitRule({
  formatError: () => {
    return 'Stop doing that so often.';
  },
  identifyContext: context => {
    return context.req.ip;
  }
});

const permissions = shield({
  Query: {
    myId: rateLimit({
      max: 2,
      window: '10s'
    })
  }
});
github teamplanes / graphql-rate-limit / example / index.js View on Github external
identifyContext: context => {
    return context.req.ip;
  }
});

const permissions = shield({
  Query: {
    myId: rateLimit({
      max: 2,
      window: '10s'
    })
  }
});

// Option 3: Manually use the rate limiter in resolvers
const rateLimiter = getGraphQLRateLimiter({
  formatError: () => {
    return 'Stop doing that.';
  },
  identifyContext: context => {
    return context.req.ip;
  }
});

const books = [
  {
    title: 'Harry Potter and the Chamber of Secrets',
    author: 'J.K. Rowling'
  },
  {
    title: 'Jurassic Park',
    author: 'Michael Crichton'
github withspectrum / spectrum / api / utils / rate-limit-directive.js View on Github external
// @flow
import { createRateLimitDirective, RedisStore } from 'graphql-rate-limit';
import { getClientIp } from 'request-ip';
import createRedis from 'shared/bull/create-redis';
import ms from 'ms';

export default createRateLimitDirective({
  identifyContext: ctx => (ctx.user && ctx.user.id) || getClientIp(ctx.request),
  store: new RedisStore(createRedis()),
  formatError: ({ fieldName, fieldIdentity, max, window }) =>
    `Slow down there partner! You've called '${fieldName}' ${max} times in the past ${ms(
      window,
      { long: true }
    )}. Relax for a bit and try again later.`,
});
github teamplanes / graphql-rate-limit / example / index.js View on Github external
const { ApolloServer, gql, makeExecutableSchema } = require('apollo-server');
const { shield } = require('graphql-shield');
const { applyMiddleware } = require('graphql-middleware');
const {
  createRateLimitDirective,
  RedisStore,
  getGraphQLRateLimiter,
  createRateLimitRule
} = require('graphql-rate-limit');
const redis = require('redis');

// Option 1: Use a directive (applied in the schema below)
const rateLimitDirective = createRateLimitDirective({
  identifyContext: context => {
    return context.req.ip;
  },
  store: new RedisStore(redis.createClient())
});

// Option 2: User graphql-shield (applied in the `shield` below)
const rateLimit = createRateLimitRule({
  formatError: () => {
    return 'Stop doing that so often.';
  },
  identifyContext: context => {
    return context.req.ip;
  }
});
github chnirt / nestjs-graphql-best-practice / src / config / graphql / schemaDirectives / index.ts View on Github external
import DeprecatedDirective from './deprecated'
// import LengthDirective from './length'
import DateFormatDirective from './date'
import UpperCaseDirective from './upper'
import ConcatDirective from './concat'
import RestDirective from './rest'
import IntlDirective from './intl'
import ValidateDirective from './validate'

export default {
	isAuthenticated: AuthDirective,
	hasPermission: PermissionDirective,
	hasPath: PathDirective,
	deprecated: DeprecatedDirective,
	// length: LengthDirective,
	rateLimit: createRateLimitDirective({
		identifyContext: ctx => ctx.currentUser._id
	}),
	date: DateFormatDirective,
	upper: UpperCaseDirective,
	concat: ConcatDirective,
	intl: IntlDirective,
	rest: RestDirective,
	validate: ValidateDirective
}

graphql-rate-limit

Add Rate Limiting To Your GraphQL Resolvers 💂‍♀

MIT
Latest version published 3 years ago

Package Health Score

53 / 100
Full package analysis

Similar packages