-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (83 loc) · 1.95 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
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
// data
import db from "./_db.js";
// types
import { typeDefs } from "./schema.js";
// resolvers
const resolvers = {
Query: {
games() {
return db.games;
},
//first for parent class second for args and last for auth
game(_, args) {
return db.games.find((game) => game.id === args.id);
},
authors() {
return db.authors;
},
author(_, args) {
return db.authors.find((auther) => auther.id === args.id);
},
reviews() {
return db.reviews;
},
review(_, args) {
return db.reviews.find((r) => r.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: {
author(parent) {
return db.authors.find((a) => a.id === parent.author_id);
},
game(parent) {
return db.games.find((g) => g.id == parent.game_id);
},
},
Mutation: {
deleteGame(_, args) {
db.games = db.games.filter((g) => g.id !== args.id);
console.log(db.games);
return db.games;
},
addGame(_, args) {
let game = {
id: Math.floor(Math.random() * 1000).toString(),
...args.game,
};
console.log(game);
console.log(game.id);
db.games.push(game);
return game;
},
updateGame(_, args) {
db.games = db.games.map((g) => {
if (g.id === args.id) {
return { ...g, ...args.edits };
}
return g;
});
return db.games.find((g) => g.id === args.id);
},
},
};
// server setup
const server = new ApolloServer({
typeDefs,
resolvers,
});
const { url } = await startStandaloneServer(server, {
listen: { port: 4000 },
});
console.log(`🚀 Server ready at: ${url}`);