-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathscript.js
51 lines (46 loc) · 1.66 KB
/
script.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
const authorBio = ({parent: {name, dob}}) => `My name is ${name} and I was born on ${dob}.`
const characterBio = ({parent: {name}}) => `My name is ${name}.`
const humanBio = ({parent: {name, totalCredits}}) => `My name is ${name}. I have ${totalCredits} credits.`
const droidBio = ({parent: {name, primaryFunction}}) => `My name is ${name}. My primary function is ${primaryFunction}.`
async function authorsByName({args, dql}) {
const results = await dql.query(`{
queryAuthor(func: type(Author)) @filter(eq(Author.name, "${args.name}")) {
name: Author.name
dob: Author.dob
reputation: Author.reputation
}
}`)
return results.data.queryAuthor
}
async function newAuthor({args, graphql}) {
// lets give every new author a reputation of 3 by default
const results = await graphql(`mutation {
addAuthor(input: [{name: "${args.name}", reputation: 3.0 }]) {
author {
id
reputation
}
}
}`)
return results.data.addAuthor.author[0].id
}
self.addGraphQLResolvers({
"Author.bio": authorBio,
"Character.bio": characterBio,
"Human.bio": humanBio,
"Droid.bio": droidBio,
"Query.authorsByName": authorsByName,
"Mutation.newAuthor": newAuthor
})
async function rank({parents}) {
const idRepList = parents.map(function (parent) {
return {id: parent.id, rep: parent.reputation}
});
const idRepMap = {};
idRepList.sort((a, b) => a.rep > b.rep ? -1 : 1)
.forEach((a, i) => idRepMap[a.id] = i + 1)
return parents.map(p => idRepMap[p.id])
}
self.addMultiParentGraphQLResolvers({
"Author.rank": rank
})