@@ -2,6 +2,7 @@ local path = (...):gsub('%.[^%.]+$', '')
2
2
local types = require (path .. ' .types' )
3
3
local util = require (path .. ' .util' )
4
4
local introspection = require (path .. ' .introspection' )
5
+ local query_util = require (path .. ' .query_util' )
5
6
6
7
local function typeFromAST (node , schema )
7
8
local innerType
@@ -63,89 +64,10 @@ local function doesFragmentApply(fragment, type, context)
63
64
end
64
65
end
65
66
66
- local function mergeSelectionSets (fields )
67
- local selections = {}
68
-
69
- for i = 1 , # fields do
70
- local selectionSet = fields [i ].selectionSet
71
- if selectionSet then
72
- for j = 1 , # selectionSet .selections do
73
- table.insert (selections , selectionSet .selections [j ])
74
- end
75
- end
76
- end
77
-
78
- return selections
79
- end
80
-
81
67
local function defaultResolver (object , arguments , info )
82
68
return object [info .fieldASTs [1 ].name .value ]
83
69
end
84
70
85
- local function buildContext (schema , tree , rootValue , variables , operationName )
86
- local context = {
87
- schema = schema ,
88
- rootValue = rootValue ,
89
- variables = variables ,
90
- operation = nil ,
91
- fragmentMap = {},
92
- -- The field is passed to resolve function within info attribute.
93
- -- Can be used to store any data within one query.
94
- qcontext = {}
95
- }
96
-
97
- for _ , definition in ipairs (tree .definitions ) do
98
- if definition .kind == ' operation' then
99
- if not operationName and context .operation then
100
- error (' Operation name must be specified if more than one operation exists.' )
101
- end
102
-
103
- if not operationName or definition .name .value == operationName then
104
- context .operation = definition
105
- end
106
- elseif definition .kind == ' fragmentDefinition' then
107
- context .fragmentMap [definition .name .value ] = definition
108
- end
109
- end
110
-
111
- if not context .operation then
112
- if operationName then
113
- error (' Unknown operation "' .. operationName .. ' "' )
114
- else
115
- error (' Must provide an operation' )
116
- end
117
- end
118
-
119
- return context
120
- end
121
-
122
- local function collectFields (objectType , selections , visitedFragments , result , context )
123
- for _ , selection in ipairs (selections ) do
124
- if selection .kind == ' field' then
125
- if shouldIncludeNode (selection , context ) then
126
- local name = getFieldResponseKey (selection )
127
- result [name ] = result [name ] or {}
128
- table.insert (result [name ], selection )
129
- end
130
- elseif selection .kind == ' inlineFragment' then
131
- if shouldIncludeNode (selection , context ) and doesFragmentApply (selection , objectType , context ) then
132
- collectFields (objectType , selection .selectionSet .selections , visitedFragments , result , context )
133
- end
134
- elseif selection .kind == ' fragmentSpread' then
135
- local fragmentName = selection .name .value
136
- if shouldIncludeNode (selection , context ) and not visitedFragments [fragmentName ] then
137
- visitedFragments [fragmentName ] = true
138
- local fragment = context .fragmentMap [fragmentName ]
139
- if fragment and shouldIncludeNode (fragment , context ) and doesFragmentApply (fragment , objectType , context ) then
140
- collectFields (objectType , fragment .selectionSet .selections , visitedFragments , result , context )
141
- end
142
- end
143
- end
144
- end
145
-
146
- return result
147
- end
148
-
149
71
local evaluateSelections
150
72
151
73
local function completeValue (fieldType , result , subSelections , context )
@@ -230,21 +152,24 @@ local function getFieldEntry(objectType, object, fields, context)
230
152
}
231
153
232
154
local resolvedObject = (fieldType .resolve or defaultResolver )(object , arguments , info )
233
- local subSelections = mergeSelectionSets (fields )
155
+ local subSelections = query_util . mergeSelectionSets (fields )
234
156
235
157
return completeValue (fieldType .kind , resolvedObject , subSelections , context )
236
158
end
237
159
238
160
evaluateSelections = function (objectType , object , selections , context )
239
- local groupedFieldSet = collectFields (objectType , selections , {}, {}, context )
161
+ local groupedFieldSet = query_util . collectFields (objectType , selections , {}, {}, context )
240
162
241
163
return util .map (groupedFieldSet , function (fields )
242
164
return getFieldEntry (objectType , object , fields , context )
243
165
end )
244
166
end
245
167
246
168
return function (schema , tree , rootValue , variables , operationName )
247
- local context = buildContext (schema , tree , rootValue , variables , operationName )
169
+ local context = query_util .buildContext (schema , tree , rootValue , variables , operationName )
170
+ -- The field is passed to resolve function within info attribute.
171
+ -- Can be used to store any data within one query.
172
+ context .qcontext = {}
248
173
local rootType = schema [context .operation .operation ]
249
174
250
175
if not rootType then
0 commit comments