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

Commit 9d471d0

Browse files
committed
Clarify our approach to union type resolving
1 parent c29493d commit 9d471d0

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

graphql/accessor_general.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,6 @@ local function set_connection_index(c, c_name, c_type, collection_name,
614614
}
615615
end
616616

617-
618617
--- Build `connection_indexes` table (part of `index_cache`) to use in the
619618
--- @{get_index_name} function.
620619
---
@@ -734,11 +733,11 @@ local function validate_collections(collections, schemas)
734733
assert(type(v.parts) == 'table',
735734
'variant.parts must be a table, got ' .. type(v.parts))
736735
assert(type(v.index_name) == 'string',
737-
'variant.index_name must be a string, got ' ..
738-
type(v.index_name))
736+
'variant.index_name must be a string, got ' ..
737+
type(v.index_name))
739738
end
740739
else
741-
assert(false, ('connection "%s" of collection "%s" doesn\'t ' ..
740+
assert(false, ('connection "%s" of collection "%s" does not ' ..
742741
'have neither destination collection nor variants field'):
743742
format(connection.name, collection_name))
744743
end

graphql/core/execute.lua

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

7171
local evaluateSelections
7272

73-
--@todo resolveType optional comments
73+
-- @param[opt] resolvedType a type to be used instead of one returned by
74+
-- `fieldType.resolveType(result)` in case when the `fieldType` is Interface or
75+
-- Union; that is needed to increase flexibility of an union type resolving
76+
-- (e.g. resolving by a parent object instead of a current object) via
77+
-- returning it from the `fieldType.resolve` function, which called before
78+
-- `resolvedType` and may need to determine the type itself for its needs
7479
local function completeValue(fieldType, result, subSelections, context, resolvedType)
7580
local fieldTypeName = fieldType.__type
7681

@@ -112,7 +117,6 @@ local function completeValue(fieldType, result, subSelections, context, resolved
112117
local fields = evaluateSelections(fieldType, result, subSelections, context)
113118
return next(fields) and fields or context.schema.__emptyObject
114119
elseif fieldTypeName == 'Interface' or fieldTypeName == 'Union' then
115-
116120
local objectType = resolvedType or fieldType.resolveType(result)
117121
while objectType.__type == 'NonNull' do
118122
objectType = objectType.ofType
@@ -156,11 +160,11 @@ local function getFieldEntry(objectType, object, fields, context)
156160
variableValues = context.variables,
157161
qcontext = context.qcontext
158162
}
159-
--@todo add comment
163+
164+
-- resolvedType is optional return value
160165
local resolvedObject, resolvedType = (fieldType.resolve or defaultResolver)(object, arguments, info)
161166
local subSelections = query_util.mergeSelectionSets(fields)
162167

163-
--@todo add comment
164168
return completeValue(fieldType.kind, resolvedObject, subSelections, context, resolvedType)
165169
end
166170

graphql/tarantool_graphql.lua

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,24 @@
1111
---
1212
--- * Unions: as GraphQL specification says "...no fields may be queried on
1313
--- Union type without the use of typed fragments." Tarantool_graphql
14-
--- behaves this way. So 'common fields' are not supported. This does NOT work:
14+
--- behaves this way. So 'common fields' are not supported. This does NOT
15+
--- work:
1516
---
16-
--- hero {
17-
--- hero_id -- common field
18-
--- ... on human {
19-
--- name
20-
--- }
21-
--- ... on droid {
22-
--- model
23-
--- }
24-
--- }
25-
--- (GraphQL spec: http://facebook.github.io/graphql/October2016/#sec-Unions)
26-
--- Also, no arguments are currently allowed for fragments.
27-
--- See issue about this (https://github.com/facebook/graphql/issues/204)
17+
--- ```
18+
--- hero {
19+
--- hero_id -- common field; does NOT work
20+
--- ... on human {
21+
--- name
22+
--- }
23+
--- ... on droid {
24+
--- model
25+
--- }
26+
--- }
27+
--- ```
2828
---
29+
--- (GraphQL spec: http://facebook.github.io/graphql/October2016/#sec-Unions)
30+
--- Also, no arguments are currently allowed for fragments.
31+
--- See issue about this (https://github.com/facebook/graphql/issues/204)
2932

3033
local json = require('json')
3134
local yaml = require('yaml')
@@ -536,7 +539,7 @@ local function convert_union_connection(state, connection, collection_name)
536539
name = c.name,
537540
types = union_types,
538541
}),
539-
arguments = nil,
542+
arguments = nil, -- see Border cases/Unions at the top of the file
540543
resolve = function(parent, args_instance, info)
541544
local v, variant_num = resolve_variant(parent)
542545
local destination_type = union_types[variant_num]

0 commit comments

Comments
 (0)