-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
94 lines (88 loc) · 2.18 KB
/
index.js
File metadata and controls
94 lines (88 loc) · 2.18 KB
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
91
92
93
94
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import { typeDefs } from "./schema.js";
import db from "./_db.js";
// resolvers for each different type (Query, Mutation,...)
const resolvers = {
Query: {
// specify what data should be returned for each query
reviews() {
return db.reviews;
},
games() {
return db.games;
},
authors() {
return db.authors;
},
review(_, args) {
return db.reviews.find((review) => review.id === args.id);
},
game(_, args) {
return db.games.find((game) => game.id === args.id);
},
author(_, args) {
return db.authors.find((author) => author.id === args.id);
},
},
Game: {
reviews(parent) {
// parent is a reference to the value returned by the previous resolver
// console.log(parent);
return db.reviews.filter((review) => review.game_id === parent.id);
},
},
Author: {
reviews(parent) {
// console.log(parent);
return db.reviews.filter((review) => review.author_id === parent.id);
},
},
Review: {
game(parent) {
// console.log(parent);
return db.games.find((game) => game.id === parent.game_id);
},
author(parent) {
// console.log(parent);
return db.authors.find((author) => author.id === parent.author_id);
},
},
Mutation: {
deleteGame(_, args) {
db.games = db.games.filter((game) => game.id !== args.id);
return db.games;
},
addGame(_, args) {
let game = {
...args.game,
id: Math.floor(Math.random() * 1000).toString(),
};
db.games.push(game);
return game;
},
updateGame(_, args) {
db.games = db.games.map((game) => {
if (game.id === args.id) {
return {
...game,
...args.edits,
};
}
return game;
});
return db.games.find((game) => game.id === args.id);
},
},
};
// setup server
const server = new ApolloServer({
typeDefs,
resolvers,
});
const { url } = await startStandaloneServer(server, {
listen: {
port: 4000,
},
});
console.log(`🚀 Server ready at ${url}`);