-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
74 lines (67 loc) · 1.85 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
70
71
72
73
74
const { ApolloServer, gql } = require('apollo-server');
const {
createRateLimitDirective,
createRateLimitTypeDef,
} = require('graphql-rate-limit-directive');
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',
},
};
class RateLimitError extends Error {
constructor(msBeforeNextReset) {
super('Too many requests, please try again shortly.');
// Determine when the rate limit will be reset so the client can try again
const resetAt = new Date();
resetAt.setTime(resetAt.getTime() + msBeforeNextReset);
// GraphQL will automatically use this field to return extensions data in the GraphQLError
// See https://github.com/graphql/graphql-js/pull/928
this.extensions = {
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);
});