Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
});
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);
});
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);
});
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);
});
'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);
});
// 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);
});
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);
});
},
{
title: 'The Hobbit',
author: 'J. R. R. Tolkien',
},
],
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);
author: 'George R. R. Martin',
},
{
title: 'The Hobbit',
author: 'J. R. R. Tolkien',
},
],
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 => {
const pointsCalculator = (directiveArgs, obj, args, context, info) => {
// If a specific resolver for a type and field, use custom logic
if (info.parentType.name === 'Query' && info.fieldName === 'books') {
// When query string `token=secret` is used, allow unlimited usage, else costs 2 for `books`
return context.query && context.query.token === 'secret' ? 0 : 2;
} else {
return defaultPointsCalculator(directiveArgs, obj, args, context, info);
}
};