-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (79 loc) · 1.84 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
// DB
import DB from './_db.js';
// Types
import { typeDefs } from './schema.js';
// Resolvers
const resolvers = {
Query: {
games() {
return DB.games;
},
game(_, args) {
return DB.games.find(game => game.id === args.id);
},
authors() {
return DB.authors;
},
author(_, args) {
return DB.authors.find(author => author.id === args.id);
},
reviews() {
return DB.reviews;
},
review(_, args) {
return DB.reviews.find(review => review.id === args.id);
},
},
Game: {
reviews(parent) {
return DB.reviews.filter(review => review.game_id === parent.id);
}
},
Author: {
reviews(parent) {
return DB.reviews.filter(review => review.author_id === parent.id);
}
},
Review: {
game(parent) {
return DB.games.find(game => game.id === parent.game_id);
},
author(parent) {
return DB.authors.find(author => author.id === parent.author_id);
}
},
Mutation: {
addGame(_, args) {
const newGame = {
id: Math.floor(Math.random() * 1000),
...args.game
};
DB.games.push(newGame);
return newGame;
},
updateGame(_, args) {
DB.games = DB.games.map(game => {
if (game.id === args.id) {
return { ...game, ...args.updateGame };
}
return game;
});
return DB.games.find(game => game.id === args.id);
},
deleteGame(_, args) {
DB.games = DB.games.filter(game => game.id !== args.id);
return DB.games;
}
}
};
// Start server
const server = new ApolloServer({
typeDefs,
resolvers,
});
const { url } = await startStandaloneServer(server, {
listen: { port: 3000 }
});
console.log(`Server start at ${url}`);