-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
69 lines (65 loc) · 1.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require('dotenv').config();
const { ApolloServer, gql } = require('apollo-server');
const {
createRateLimitDirective,
createRateLimitTypeDef,
} = require('graphql-rate-limit-directive');
const { RateLimiterRedis } = require('rate-limiter-flexible');
const redis = require('redis');
const typeDefs = gql`
# Allow each field to be queried once every 15 seconds
type Query @rateLimit(limit: 1, duration: 15) {
books: [Book!]
quote: String
}
type Book {
title: String
author: String
}
`;
const resolvers = {
Query: {
books: () => [
{
title: 'A Game of Thrones',
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',
},
};
// 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);
});