-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (86 loc) · 2.36 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
91
const { ApolloServer, gql } = require('apollo-server')
const threeHouses = require('./info')
// String! means its non-nullable
// [Member!]! means you can expect stuff coming back because Member is non-nullable and the array will be non-nullable as well
//gql is graphql language
const typeDefs = gql`
type House {
houseName: String!,
members: [Member!]!
}
type Member {
# member name
name: String!,
# whether they are the leader or not
leader: Boolean,
# to show how to add properties that are not in the info
favorite: Boolean,
}
type Query {
allHousesInfo: [House],
member(name: String!): Member,
getLeaders: [Member]
}
`
const resolvers = {
Query: {
// query {
// allHousesInfo {
// houseName,
// members {
// name,
// leader
// }
// }
// }
allHousesInfo: () => threeHouses,
// query {
// member(name: "Claude von Riegan") {
// name,
// leader
// }
// }
member: (parent, params, ctx) => {
if (params.name) {
const member = threeHouses.map(house => {
return house.members.filter(member => {
return member.name.includes(params.name)
})
}).flat()[0]
if (!member) return 'member'
if (member) return member
}
},
// query {
// getLeaders {
// name
// }
// }
getLeaders: () => {
return threeHouses.map(house => {
return house.members.filter(member => {
return member.leader
})
}).flat()
},
},
// you can add static or dynamic values to your query here
// query {
// member(name: "Claude von Riegan") {
// name,
// leader,
// favorite
// }
// }
Member: {
favorite: (parent) => {
console.log(parent)
return true
}
}
}
const server = new ApolloServer({
typeDefs,
resolvers
})
server.listen().then(data => console.log(`server started at port ${data.port}, ${data.url}`))