-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (58 loc) · 2.25 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
const { ApolloServer } = require('apollo-server')
const { Graph } = require('nemesis-db')
const { collect } = require('streaming-iterables')
const typeDefs = require('./types')
const graph = new Graph('redis://localhost/1')
const resolvers = {
Person: {
__resolveType: () => 'Person'
},
Queen: {
appearsIn: async (obj) => {
const appearsIn = await graph.findEdges({ subject: obj.id, predicate: 'AppearsIn' })
return appearsIn.map(async a => graph.findNode(a.object))
}
},
Mutation: {
createQueen: async (obj, { input }) => graph.createNode({ ...input, type: 'Queen' }),
updateQueen: async (obj, { input }) => graph.updateNode(input),
addQueenToSeason: async (obj, { input: { queenId, seasonId } }) => {
await graph.createEdge({ subject: queenId, predicate: 'AppearsIn', object: seasonId })
return graph.findNode(seasonId)
}
},
Query: {
seasons: async () => {
const nodes = await collect(graph.allNodes())
// ideally here we'd be looking into redis directly and index by name
return nodes.filter(node => node.type === 'Season')
},
queens: async (obj, args) => {
const nodes = await collect(graph.allNodes())
if (args.appearsIn) {
const season = await findByName(args.appearsIn, 'Season')
const appearsIn = await graph.findEdges({ predicate: 'AppearsIn', object: season.id })
return appearsIn.map(async a => graph.findNode(a.subject))
}
return nodes.filter(node => node.type === 'Queen')
},
judges: async () => {
const nodes = await collect(graph.allNodes())
return nodes.filter(node => node.type === 'Judge')
},
season: async (obj, args) => {
const nodes = await collect(graph.allNodes())
return nodes.find(node => node.type === 'Season' && node.name === args.name)
},
queen: async (obj, args) => findByName(args.name, 'Queen'),
judge: async (obj, args) => findByName(args.name, 'Judge')
}
}
async function findByName (name, type) {
const nodes = await collect(graph.allNodes())
return nodes.find(node => node.type === type && node.name === name)
}
const server = new ApolloServer({ typeDefs, resolvers })
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`)
})