-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (76 loc) · 1.74 KB
/
index.js
File metadata and controls
84 lines (76 loc) · 1.74 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
import { ApolloServer } from '@apollo/server'
import { startStandaloneServer } from '@apollo/server/standalone'
import db from './_db.js'
import { typeDefs } from './schema.js'
const resolvers = {
Query: {
games() {
return db.games
},
game(_, args) {
return db.games.find((game) => game.id === args.id)
},
reviews() {
return db.reviews
},
review(_, args) {
return db.reviews.find((review) => review.id === args.id)
},
authors() {
return db.authors
},
author(_, args) {
return db.authors.find((author) => author.id === args.id)
}
},
Game: {
reviews(parent) {
return db.reviews.filter((r) => r.game_id === parent.id)
}
},
Author: {
reviews(parent) {
return db.reviews.filter((r) => r.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)
return db.games
},
addGame(_, args) {
let game = {
...args.game,
id: Math.floor(Math.random() * 10000).toString()
}
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 port', 4000)