private static function completeValue(ExecutionContext $exeContext, Type $returnType, $fieldNodes, ResolveInfo $info, $path, &$result)
{
// If result is a Promise, apply-lift over completeValue.
if (self::$promiseAdapter->isPromise($result)) {
return $result->then(function (&$resolved) use($exeContext, $returnType, $fieldNodes, $info, $path) {
return self::completeValue($exeContext, $returnType, $fieldNodes, $info, $path, $resolved);
});
}
if ($result instanceof \Exception) {
throw $result;
}
// If field type is NonNull, complete for inner type, and throw field error
// if result is null.
if ($returnType instanceof NonNull) {
$completed = self::completeValue($exeContext, $returnType->getWrappedType(), $fieldNodes, $info, $path, $result);
if ($completed === null) {
throw new InvariantViolation('Cannot return null for non-nullable field ' . $info->parentType . '.' . $info->fieldName . '.');
}
return $completed;
}
// If result is null-like, return null.
if (null === $result) {
return null;
}
// If field type is List, complete each item in the list with the inner type
if ($returnType instanceof ListOfType) {
return self::completeListValue($exeContext, $returnType, $fieldNodes, $info, $path, $result);
}
// If field type is Scalar or Enum, serialize to a valid value, returning
// null if serialization is not possible.
if ($returnType instanceof LeafType) {
return self::completeLeafValue($returnType, $result);
}
if ($returnType instanceof AbstractType) {
return self::completeAbstractValue($exeContext, $returnType, $fieldNodes, $info, $path, $result);
}
// Field type must be Object, Interface or Union and expect sub-selections.
if ($returnType instanceof ObjectType) {
return self::completeObjectValue($exeContext, $returnType, $fieldNodes, $info, $path, $result);
}
throw new \RuntimeException("Cannot complete value of unexpected type \"{$returnType}\".");
}