Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 8ba78a6

Browse files
committed
Hold order of fields traversal
The certain order of execution is required for mutations.
1 parent 10ee502 commit 8ba78a6

7 files changed

+57
-54
lines changed

graphql/core/execute.lua

+9-5
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,15 @@ local function getFieldEntry(objectType, object, fields, context)
114114
end
115115

116116
evaluateSelections = function(objectType, object, selections, context)
117-
local groupedFieldSet = query_util.collectFields(objectType, selections, {}, {}, context)
118-
119-
return util.map(groupedFieldSet, function(fields)
120-
return getFieldEntry(objectType, object, fields, context)
121-
end)
117+
local result = {}
118+
local fields = query_util.collectFields(objectType, selections, {}, {}, context)
119+
for _, field in ipairs(fields) do
120+
assert(result[field.name] == nil,
121+
'two selections into the one field: ' .. field.name)
122+
result[field.name] = getFieldEntry(objectType, object, {field.selection},
123+
context)
124+
end
125+
return result
122126
end
123127

124128
return function(schema, tree, rootValue, variables, operationName)

graphql/core/query_util.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ function query_util.collectFields(objectType, selections, visitedFragments, resu
6969
if selection.kind == 'field' then
7070
if shouldIncludeNode(selection, context) then
7171
local name = query_util.getFieldResponseKey(selection)
72-
result[name] = result[name] or {}
73-
table.insert(result[name], selection)
72+
table.insert(result, {name = name, selection = selection})
7473
end
7574
elseif selection.kind == 'inlineFragment' then
7675
if shouldIncludeNode(selection, context) and doesFragmentApply(selection, objectType, context) then

graphql/query_to_avro.lua

+4-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ end
115115
---
116116
--- @treturn table `result` is the corresponding Avro schema
117117
object_to_avro = function(object_type, selections, context)
118-
local groupedFieldSet = query_util.collectFields(object_type, selections,
118+
local fields = query_util.collectFields(object_type, selections,
119119
{}, {}, context)
120120
local result = {
121121
type = 'record',
@@ -126,8 +126,9 @@ object_to_avro = function(object_type, selections, context)
126126
result.namespace = table.concat(context.namespace_parts, ".")
127127
end
128128
table.insert(context.namespace_parts, result.name)
129-
for _, fields in pairs(groupedFieldSet) do
130-
local avro_field = field_to_avro(object_type, fields, context)
129+
for _, field in pairs(fields) do
130+
local avro_field = field_to_avro(object_type, {field.selection},
131+
context)
131132
table.insert(result.fields, avro_field)
132133
end
133134
context.namespace_parts[#context.namespace_parts] = nil

test/extra/to_avro_arrays.test.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ fields:
5858
fields:
5959
- name: user_id
6060
type: string
61+
- name: favorite_food
62+
type:
63+
type: array
64+
items: string
6165
- name: user_balances
6266
type:
6367
type: array
@@ -68,10 +72,6 @@ fields:
6872
type: int
6973
name: balance
7074
namespace: Query.user_collection
71-
- name: favorite_food
72-
type:
73-
type: array
74-
items: string
7575
name: user_collection
7676
namespace: Query
7777
]]
@@ -96,7 +96,7 @@ user_collection:
9696
]]
9797
result_expected = yaml.decode(result_expected)
9898
local result = gql_query:execute(variables)
99-
test:is_deeply(result, result_expected, 'graphql qury exec result')
99+
test:is_deeply(result, result_expected, 'graphql query exec result')
100100
local ok, ash, r, fs, _
101101
ok, ash = avro.create(avros)
102102
assert(ok)

test/extra/to_avro_huge.test.lua

+27-28
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ local query = [[
7373
}
7474
}
7575
}
76-
7776
]]
7877
local expected_avro_schema = [[
7978
type: record
@@ -85,14 +84,24 @@ fields:
8584
items:
8685
type: record
8786
fields:
87+
- name: id
88+
type: int
89+
- name: last_name
90+
type: string
91+
- name: first_name
92+
type: string
8893
- name: order_connection
8994
type:
9095
type: array
9196
items:
9297
type: record
9398
fields:
99+
- name: id
100+
type: int
94101
- name: user_id
95102
type: int
103+
- name: description
104+
type: string
96105
- name: order__order_item
97106
type:
98107
type: array
@@ -107,30 +116,20 @@ fields:
107116
type:
108117
type: record
109118
fields:
119+
- name: id
120+
type: int
121+
- name: name
122+
type: string
110123
- name: description
111124
type: string
112125
- name: price
113126
type: string
114-
- name: name
115-
type: string
116-
- name: id
117-
type: int
118127
name: item_collection
119128
namespace: Query.user_collection.order_collection.order_item_collection
120129
name: order_item_collection
121130
namespace: Query.user_collection.order_collection
122-
- name: description
123-
type: string
124-
- name: id
125-
type: int
126131
name: order_collection
127132
namespace: Query.user_collection
128-
- name: last_name
129-
type: string
130-
- name: first_name
131-
type: string
132-
- name: id
133-
type: int
134133
name: user_collection
135134
namespace: Query
136135
- name: order_collection
@@ -139,16 +138,28 @@ fields:
139138
items:
140139
type: record
141140
fields:
141+
- name: id
142+
type: int
143+
- name: description
144+
type: string
142145
- name: user_connection
143146
type:
144147
type: record
145148
fields:
149+
- name: id
150+
type: int
151+
- name: first_name
152+
type: string
153+
- name: last_name
154+
type: string
146155
- name: order_connection
147156
type:
148157
type: array
149158
items:
150159
type: record
151160
fields:
161+
- name: id
162+
type: int
152163
- name: order__order_item
153164
type:
154165
type: array
@@ -167,22 +178,10 @@ fields:
167178
namespace: Query.order_collection.user_collection.order_collection.order_item_collection
168179
name: order_item_collection
169180
namespace: Query.order_collection.user_collection.order_collection
170-
- name: id
171-
type: int
172181
name: order_collection
173182
namespace: Query.order_collection.user_collection
174-
- name: last_name
175-
type: string
176-
- name: first_name
177-
type: string
178-
- name: id
179-
type: int
180183
name: user_collection
181184
namespace: Query.order_collection
182-
- name: id
183-
type: int
184-
- name: description
185-
type: string
186185
name: order_collection
187186
namespace: Query
188187
@@ -249,7 +248,7 @@ order_collection:
249248
]]
250249
result_expected = yaml.decode(result_expected)
251250
local result = gql_query:execute(variables)
252-
test:is_deeply(result, result_expected, 'graphql qury exec result')
251+
test:is_deeply(result, result_expected, 'graphql query exec result')
253252
local ok, ash, r, fs, _
254253
ok, ash = avro.create(avros)
255254
assert(ok)

test/extra/to_avro_nested.test.lua

+7-7
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,20 @@ fields:
5555
items:
5656
type: record
5757
fields:
58-
- name: p2
59-
type: string
60-
- name: p1
61-
type: string
6258
- name: uid
6359
type: long
60+
- name: p1
61+
type: string
62+
- name: p2
63+
type: string
6464
- name: nested
6565
type:
6666
type: record
6767
fields:
68-
- name: y
69-
type: long
7068
- name: x
7169
type: long
70+
- name: y
71+
type: long
7272
name: nested
7373
namespace: Query.user
7474
name: user
@@ -94,7 +94,7 @@ user:
9494
]]
9595
result_expected = yaml.decode(result_expected)
9696
local result = gql_query:execute(variables)
97-
test:is_deeply(result, result_expected, 'graphql qury exec result')
97+
test:is_deeply(result, result_expected, 'graphql query exec result')
9898
local ok, ash, r, fs, _
9999
ok, ash = avro.create(avros)
100100
assert(ok)

test/extra/to_avro_nullable.test.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ fields:
5454
items:
5555
type: record
5656
fields:
57+
- name: id
58+
type: string
5759
- name: id_or_null_1
5860
type: string*
59-
- name: id_or_null_3
60-
type: string*
6161
- name: id_or_null_2
6262
type: string*
63-
- name: id
64-
type: string
63+
- name: id_or_null_3
64+
type: string*
6565
name: bar
6666
namespace: Query
6767
]]

0 commit comments

Comments
 (0)