How to use the graphql-rate-limit-directive.createRateLimitDirective function in graphql-rate-limit-directive

To help you get started, we’ve selected a few graphql-rate-limit-directive 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 ravangen / graphql-rate-limit / examples / onlimit-object / index.js View on Github external
resetAt.setTime(resetAt.getTime() + msBeforeNextReset);

    this.limit = limit;
    this.resetAt = resetAt.toISOString();
  }
}

// IMPORTANT: Specify how a rate limited field should behave when a limit has been exceeded
const onLimit = (resource, directiveArgs, obj, args, context, info) =>
  new RateLimit(directiveArgs.limit, resource.msBeforeNext);

const server = new ApolloServer({
  typeDefs: [createRateLimitTypeDef(), typeDefs],
  resolvers,
  schemaDirectives: {
    rateLimit: createRateLimitDirective({
      onLimit,
    }),
  },
});
server
  .listen()
  .then(({ url }) => {
    console.log(`πŸš€  Server ready at ${url}`);
  })
  .catch(error => {
    console.error(error);
  });
github ravangen / graphql-rate-limit / examples / points / index.js View on Github external
return context.query && context.query.token === 'secret' ? 0 : 2;
  } else {
    return defaultPointsCalculator(directiveArgs, obj, args, context, info);
  }
};

const server = new ApolloServer({
  typeDefs: [createRateLimitTypeDef(), typeDefs],
  resolvers,
  // IMPORTANT: Build GraphQL context from request data (like userId and/or ip)
  context: ({ req }) => ({
    // See https://expressjs.com/en/api.html#req.query
    query: req.query, // object containing a property for each query string parameter in the route
  }),
  schemaDirectives: {
    rateLimit: createRateLimitDirective({
      pointsCalculator,
    }),
  },
});
server
  .listen()
  .then(({ url }) => {
    console.log(`πŸš€  Server ready at ${url}`);
  })
  .catch(error => {
    console.error(error);
  });
github ravangen / graphql-rate-limit / examples / onlimit-error / index.js View on Github external
code: 'RATE_LIMITED',
      resetAt,
    };
  }
}

// IMPORTANT: Specify how a rate limited field should behave when a limit has been exceeded
const onLimit = (resource, directiveArgs, obj, args, context, info) => {
  throw new RateLimitError(resource.msBeforeNext);
};

const server = new ApolloServer({
  typeDefs: [createRateLimitTypeDef(), typeDefs],
  resolvers,
  schemaDirectives: {
    rateLimit: createRateLimitDirective({
      onLimit,
    }),
  },
});
server
  .listen()
  .then(({ url }) => {
    console.log(`πŸš€  Server ready at ${url}`);
  })
  .catch(error => {
    console.error(error);
  });
github ravangen / graphql-rate-limit / examples / multiple / index.js View on Github external
quote: () =>
      'The future is something which everyone reaches at the rate of sixty minutes an hour, whatever he does, whoever he is. ― C.S. Lewis',
  },
};

const server = new ApolloServer({
  // IMPORTANT: Include directive type definitions for both @burstRateLimit and @sustainedRateLimit
  typeDefs: [
    createRateLimitTypeDef('burstRateLimit'),
    createRateLimitTypeDef('sustainedRateLimit'),
    typeDefs,
  ],
  resolvers,
  schemaDirectives: {
    // IMPORTANT: Include directive implementations (directive name is determined by the key)
    burstRateLimit: createRateLimitDirective(),
    sustainedRateLimit: createRateLimitDirective(),
  },
});
server
  .listen()
  .then(({ url }) => {
    console.log(`πŸš€  Server ready at ${url}`);
  })
  .catch(error => {
    console.error(error);
  });
github ravangen / graphql-rate-limit / examples / multiple / index.js View on Github external
'The future is something which everyone reaches at the rate of sixty minutes an hour, whatever he does, whoever he is. ― C.S. Lewis',
  },
};

const server = new ApolloServer({
  // IMPORTANT: Include directive type definitions for both @burstRateLimit and @sustainedRateLimit
  typeDefs: [
    createRateLimitTypeDef('burstRateLimit'),
    createRateLimitTypeDef('sustainedRateLimit'),
    typeDefs,
  ],
  resolvers,
  schemaDirectives: {
    // IMPORTANT: Include directive implementations (directive name is determined by the key)
    burstRateLimit: createRateLimitDirective(),
    sustainedRateLimit: createRateLimitDirective(),
  },
});
server
  .listen()
  .then(({ url }) => {
    console.log(`πŸš€  Server ready at ${url}`);
  })
  .catch(error => {
    console.error(error);
  });
github ravangen / graphql-rate-limit / examples / redis / index.js View on Github external
// IMPORTANT: Create a client to provide into createRateLimitDirective
const redisClient = redis.createClient({
  url: process.env.REDIS_URL,
  password: process.env.REDIS_PASSWORD,
  enable_offline_queue: false, // must be created with offline queue switched off
});
redisClient.on('error', error => {
  console.log(error);
});

const server = new ApolloServer({
  typeDefs: [createRateLimitTypeDef(), typeDefs],
  resolvers,
  schemaDirectives: {
    rateLimit: createRateLimitDirective({
      // IMPORTANT: Tell the directive's limiter to use RateLimiterRedis along with specific options
      limiterClass: RateLimiterRedis,
      limiterOptions: {
        storeClient: redisClient,
      },
    }),
  },
});
server
  .listen()
  .then(({ url }) => {
    console.log(`πŸš€  Server ready at ${url}`);
  })
  .catch(error => {
    console.error(error);
  });
github ravangen / graphql-rate-limit / examples / context / index.js View on Github external
obj,
    args,
    context,
    info,
  )}`;

const server = new ApolloServer({
  typeDefs: [createRateLimitTypeDef(), typeDefs],
  resolvers,
  // IMPORTANT: Build GraphQL context from request data (like userId and/or ip)
  context: ({ req }) => ({
    // See https://expressjs.com/en/api.html#req.ip
    ip: req.ip, // Express uses IPv6 by default
  }),
  schemaDirectives: {
    rateLimit: createRateLimitDirective({
      keyGenerator,
    }),
  },
});
server
  .listen()
  .then(({ url }) => {
    console.log(`πŸš€  Server ready at ${url}`);
  })
  .catch(error => {
    console.error(error);
  });

graphql-rate-limit-directive

Fixed window rate-limiting directive for GraphQL. Use to limit repeated requests to queries and mutations.

MIT
Latest version published 11 months ago

Package Health Score

64 / 100
Full package analysis