Value Completion
After resolving the value for a field, it is completed by ensuring it adheres to the expected return type. If the return type is another Object type, then the field execution process continues recursively.
(
fieldType
,
fields
,
result
,
variableValues
)
- If the
fieldType
is a Non‐Null type:
- Let innerType be the inner type of fieldType .
- Let completedResult be the result of calling CompleteValue ( innerType , fields , result , variableValues ) .
- If completedResult is null , throw a field error.
- Return completedResult .
- If result is null (or another internal value similar to null such as undefined or NaN ), return null .
- If
fieldType
is a List type:
- If result is not a collection of values, throw a field error.
- Let innerType be the inner type of fieldType .
- Return a list where each list item is the result of calling CompleteValue ( innerType , fields , resultItem , variableValues ) , where resultItem is each item in result .
- If
fieldType
is a Scalar or Enum type:
- Return the result of “coercing” result , ensuring it is a legal value of fieldType , otherwise null .
- If
fieldType
is an Object, Interface, or Union type:
- If
fieldType
is an Object type.
- Let objectType be fieldType .
- Otherwise if
fieldType
is an Interface or Union type.
- Let objectType be ResolveAbstractType( fieldType , result ).
- Let subSelectionSet be the result of calling MergeSelectionSets ( fields ) .
- Return the result of evaluating ExecuteSelectionSet(subSelectionSet, objectType, result, variableValues) normally (allowing for parallelization).
- If
fieldType
is an Object type.
Resolving Abstract Types
When completing a field with an abstract return type, that is an Interface or Union return type, first the abstract type must be resolved to a relevant Object type. This determination is made by the internal system using whatever means appropriate.
A common method of determining the Object type for an
objectValue
in object‐oriented environments, such as Java or C#, is to use the class name of the
objectValue
.
(
abstractType
,
objectValue
)
- Return the result of calling the internal method provided by the type system for determining the Object type of abstractType given the value objectValue .
Merging Selection Sets
When more than one fields of the same name are executed in parallel, their selection sets are merged together when completing the value in order to continue execution of the sub‐selection sets.
An example query illustrating parallel fields with the same name with sub‐selections.
{
me {
firstName
}
me {
lastName
}
}
After resolving the value forme
, the selection sets are merged together sofirstName
andlastName
can be resolved for one value.
(
fields
)
- Let selectionSet be an empty list.
- For each
field
in
fields
:
- Let fieldSelectionSet be the selection set of field .
- If fieldSelectionSet is null or empty, continue to the next field.
- Append all selections in fieldSelectionSet to selectionSet .
- Return selectionSet .