/
demo.ts
51 lines (45 loc) 路 1013 Bytes
/
demo.ts
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
import { ApolloServer, gql } from 'apollo-server';
import { makeExecutableSchema } from 'graphql-tools';
import { createRateLimitDirective, RateLimitTypeDefs } from './index';
const books = [
{
title: 'Harry Potter and the Chamber of Secrets',
author: 'J.K. Rowling',
},
{
title: 'Jurassic Park',
author: 'Michael Crichton',
},
];
const typeDefs = gql`
type Book {
title: String @rateLimit(period: DAY)
author: String
}
type Query @rateLimit {
books: [Book]
greeting: String @rateLimit(max: 15)
}
`;
const resolvers = {
Query: {
books: () => books,
greeting: () => 'Hello!',
},
};
const schema = makeExecutableSchema({
typeDefs: [RateLimitTypeDefs, typeDefs],
resolvers,
schemaDirectives: {
rateLimit: createRateLimitDirective<any>(),
},
});
const server = new ApolloServer({ schema });
server
.listen()
.then(({ url }) => {
console.log(`馃殌 Server ready at ${url}`);
})
.catch(error => {
console.error(error);
});