-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.lua
109 lines (97 loc) · 2.33 KB
/
server.lua
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
(function ()
-- START APM
require('.graphql.runtime')
require('.graphql.server')
-- END APM
local graphql = require('@tilla/graphql')
local server = require('@tilla/graphql_server')
local _schema, types = graphql.schema, graphql.types
local Bob = {
id = 'id-1',
firstName = 'Bob',
lastName = 'Ross',
age = 52
}
local Jane = {
id = 'id-2',
firstName = 'Jane',
lastName = 'Doe',
age = 40,
friends = {Bob.id}
}
local John = {
id = 'id-3',
firstName = 'John',
lastName = 'Locke',
age = 44
}
-- Mock db
local db = { Bob, Jane, John }
-- primary index
local dbById = {}
for _, v in ipairs(db) do dbById[v.id] = v end
local function findPersonById (id)
return dbById[id]
end
local person
person = types.object({
name = 'Person',
fields = function ()
return {
id = types.id.nonNull,
firstName = types.string.nonNull,
middleName = types.string,
lastName = types.string.nonNull,
age = types.int.nonNull,
friends = {
kind = types.list(person.nonNull).nonNull,
resolve = function (parent, _, contextValue)
local findPersonById = contextValue.findPersonById
local p = findPersonById(parent.id)
local friends = {}
for _, id in ipairs(p.friends) do
table.insert(friends, findPersonById(id))
end
return friends
end
}
}
end
})
local schema = _schema.create({
query = types.object({
name = 'Query',
fields = {
person = {
kind = person,
arguments = {
id = {
kind = types.id,
defaultValue = 'id-1'
}
},
resolve = function (_, arguments, contextValue)
local findPersonById = contextValue.findPersonById
return findPersonById(arguments.id)
end
},
people = {
kind = types.list(person.nonNull).nonNull,
resolve = function (_, _, contextValue)
local db = contextValue.db
return db
end
}
}
})
})
Gql = server.aos({
schema = schema,
context = function ()
return {
findPersonById = findPersonById,
db = db
}
end
})
end)()