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

Commit a95dacd

Browse files
committed
style fixes during review 2
1 parent 3c9d651 commit a95dacd

File tree

6 files changed

+39
-40
lines changed

6 files changed

+39
-40
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ lint:
88
test/testdata/*.lua \
99
test/common/*.test.lua test/common/lua/*.lua \
1010
test/extra/*.test.lua \
11+
test/*.lua \
1112
--no-redefined --no-unused-args
1213

1314
.PHONY: test

graphql/core/execute.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ end
7070

7171
local evaluateSelections
7272

73-
local function completeValue(fieldType, result, subSelections, context)
73+
local function completeValue(fieldType, result, subSelections, context, resolvedType)
7474
local fieldTypeName = fieldType.__type
7575

7676
if fieldTypeName == 'NonNull' then
@@ -111,7 +111,12 @@ local function completeValue(fieldType, result, subSelections, context)
111111
local fields = evaluateSelections(fieldType, result, subSelections, context)
112112
return next(fields) and fields or context.schema.__emptyObject
113113
elseif fieldTypeName == 'Interface' or fieldTypeName == 'Union' then
114-
local objectType = fieldType.resolveType(result)
114+
115+
local objectType = resolvedType or fieldType.resolveType(result)
116+
if objectType.__type == 'NonNull' then
117+
objectType = objectType.ofType
118+
end
119+
115120
return evaluateSelections(objectType, result, subSelections, context)
116121
end
117122

@@ -151,10 +156,10 @@ local function getFieldEntry(objectType, object, fields, context)
151156
qcontext = context.qcontext
152157
}
153158

154-
local resolvedObject = (fieldType.resolve or defaultResolver)(object, arguments, info)
159+
local resolvedObject, resolvedType = (fieldType.resolve or defaultResolver)(object, arguments, info)
155160
local subSelections = query_util.mergeSelectionSets(fields)
156161

157-
return completeValue(fieldType.kind, resolvedObject, subSelections, context)
162+
return completeValue(fieldType.kind, resolvedObject, subSelections, context, resolvedType)
158163
end
159164

160165
evaluateSelections = function(objectType, object, selections, context)

graphql/core/rules.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,9 @@ function rules.fragmentSpreadIsPossible(node, context)
326326
local kind = kind
327327
-- Graphql-lua does not this check correctly in the case of NonNull
328328
-- wrapped fragment types. Next line fixes that
329-
if kind.__type == 'NonNull' then kind = kind.ofType end
329+
if kind.__type == 'NonNull' then
330+
kind = kind.ofType
331+
end
330332
return fragmentTypes[kind]
331333
end)
332334

graphql/core/types.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ end
155155
function types.union(config)
156156
assert(type(config.name) == 'string', 'type name must be provided as a string')
157157
assert(type(config.types) == 'table', 'types table must be provided')
158-
assert(type(config.resolveType) == 'function', 'must provide resolveType as a function')
158+
if config.resolveType then
159+
assert(type(config.resolveType) == 'function', 'must provide resolveType as a function')
160+
end
159161

160162
local instance = {
161163
__type = 'Union',

graphql/tarantool_graphql.lua

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -300,17 +300,17 @@ local parent_args_values = function(parent, connection_parts)
300300
local destination_args_values = {}
301301
for _, part in ipairs(connection_parts) do
302302
assert(type(part.source_field) == 'string',
303-
'part.source_field must be a string, got ' ..
304-
type(part.destination_field))
303+
'part.source_field must be a string, got ' ..
304+
type(part.destination_field))
305305
assert(type(part.destination_field) == 'string',
306-
'part.destination_field must be a string, got ' ..
307-
type(part.destination_field))
306+
'part.destination_field must be a string, got ' ..
307+
type(part.destination_field))
308308

309309
destination_args_names[#destination_args_names + 1] =
310-
part.destination_field
310+
part.destination_field
311311
local value = parent[part.source_field]
312312
destination_args_values[#destination_args_values + 1] =
313-
value
313+
value
314314
end
315315

316316
return destination_args_names, destination_args_values
@@ -475,8 +475,6 @@ local convert_union_connection = function(state, connection, collection_name)
475475
local collection_to_arguments = {}
476476
local collection_to_list_arguments = {}
477477

478-
local determinant_to_variant = {}
479-
480478
for _, v in ipairs(c.variants) do
481479
assert(v.determinant, 'each variant should have a determinant')
482480
assert(type(v.determinant) == 'table', 'variant\'s determinant ' ..
@@ -493,13 +491,12 @@ local convert_union_connection = function(state, connection, collection_name)
493491
('destination_type (named %s) must not be nil'):format(
494492
v.destination_collection))
495493

496-
determinant_to_variant[v.determinant] = v
497-
498-
499494
local v_args = args_from_destination_collection(state,
500495
v.destination_collection, c.type)
501496
destination_type = specify_destination_type(destination_type, c.type)
502497

498+
499+
503500
local v_list_args = state.list_arguments[v.destination_collection]
504501

505502
union_types[#union_types + 1] = destination_type
@@ -508,30 +505,21 @@ local convert_union_connection = function(state, connection, collection_name)
508505
collection_to_list_arguments[v.destination_collection] = v_list_args
509506
end
510507

511-
512-
local resolveType = function(result)
513-
for _, v in pairs(c.variants) do
514-
local dest_collection =
515-
state.nullable_collection_types[v.destination_collection]
516-
if utils.do_have_keys(result, utils.get_keys(dest_collection.fields)) then
517-
return dest_collection
518-
end
519-
end
520-
end
521-
522508
local determinant_keys = utils.get_keys(c.variants[1].determinant)
523509

524-
local resolve_variant = function(parent)
510+
local resolve_variant = function (parent)
525511
assert(utils.do_have_keys(parent, determinant_keys),
526512
('Parent object of union object doesn\'t have determinant ' ..
527513
'fields which are necessary to determine which resolving ' ..
528514
'variant should be used. Union parent object:\n"%s"\n' ..
529515
'Determinant keys:\n"%s"'):
530516
format(yaml.encode(parent), yaml.encode(determinant_keys)))
531517

518+
local variant_num
532519
local resulting_variant
533-
for determinant, variant in pairs(determinant_to_variant) do
534-
local is_match = utils.is_subtable(parent, determinant)
520+
for i, variant in ipairs(c.variants) do
521+
variant_num = i
522+
local is_match = utils.is_subtable(parent, variant.determinant)
535523

536524
if is_match then
537525
resulting_variant = variant
@@ -541,16 +529,19 @@ local convert_union_connection = function(state, connection, collection_name)
541529

542530
assert(resulting_variant, ('Variant resolving failed.'..
543531
'Parent object: "%s"\n'):format(yaml.encode(parent)))
544-
return resulting_variant
532+
return resulting_variant, variant_num
545533
end
546534

547535
local field = {
548536
name = c.name,
549-
kind = types.union({name = c.name, types = union_types,
550-
resolveType = resolveType}),
537+
kind = types.union({
538+
name = c.name,
539+
types = union_types,
540+
}),
551541
arguments = nil,
552542
resolve = function(parent, args_instance, info)
553-
local v = resolve_variant(parent)
543+
local v, variant_num = resolve_variant(parent)
544+
local destination_type = union_types[variant_num]
554545
local destination_collection =
555546
state.nullable_collection_types[v.destination_collection]
556547
local destination_args_names, destination_args_values =
@@ -571,7 +562,7 @@ local convert_union_connection = function(state, connection, collection_name)
571562
'collection "%s"'):format(json.encode(parent),
572563
tostring(collection_name)))
573564
end
574-
return c.type == '1:N' and {} or nil
565+
return c.type == '1:N' and {} or nil, destination_type
575566
end
576567

577568
local from = {
@@ -605,9 +596,9 @@ local convert_union_connection = function(state, connection, collection_name)
605596
-- situation above
606597
assert(#objs == 1, 'expect one matching object, got ' ..
607598
tostring(#objs))
608-
return objs[1]
599+
return objs[1], destination_type
609600
else -- c.type == '1:N'
610-
return objs
601+
return objs, destination_type
611602
end
612603
end
613604
}

graphql/utils.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ function utils.show_trace(func, ...)
1313
function(err)
1414
log.info('ERROR: ' .. tostring(err))
1515
log.info(debug.traceback())
16-
print('ERROR: ' .. tostring(err))
17-
print(debug.traceback())
1816
end
1917
))
2018
end

0 commit comments

Comments
 (0)