Automattic\WooCommerce\Vendor\GraphQL\Executor
ReferenceExecutor::defaultTypeResolver
If a resolveType function is not given, then a default resolve behavior is used which attempts two strategies:.
First, See if the provided value has a __typename field defined, if so, use that value as name of the resolved type.
Otherwise, test each possible type for the abstract type by calling isTypeOf for the object being coerced, returning the first type that matches.
Method of the class: ReferenceExecutor{}
No Hooks.
Returns
Promise|Type|String|null.
Usage
// protected - for code of main (parent) or child class $result = $this->defaultTypeResolver( $value, $contextValue, $info, $abstractType );
- $value(mixed|null) (required)
- .
- $contextValue(mixed|null) (required)
- .
- $info(ResolveInfo) (required)
- .
- $abstractType(AbstractType&Type) (required)
- .
ReferenceExecutor::defaultTypeResolver() ReferenceExecutor::defaultTypeResolver code WC 10.9.1
protected function defaultTypeResolver($value, $contextValue, ResolveInfo $info, AbstractType $abstractType)
{
$typename = Utils::extractKey($value, '__typename');
if (is_string($typename)) {
return $typename;
}
if ($abstractType instanceof InterfaceType && isset($info->schema->getConfig()->typeLoader)) {
$safeValue = Utils::printSafe($value);
Warning::warnOnce(
"Automattic\WooCommerce\Vendor\GraphQL Interface Type `{$abstractType->name}` returned `null` from its `resolveType` function for value: {$safeValue}. Switching to slow resolution method using `isTypeOf` of all possible implementations. It requires full schema scan and degrades query performance significantly. Make sure your `resolveType` function always returns a valid implementation or throws.",
Warning::WARNING_FULL_SCHEMA_SCAN
);
}
$possibleTypes = $info->schema->getPossibleTypes($abstractType);
$promisedIsTypeOfResults = [];
foreach ($possibleTypes as $index => $type) {
$isTypeOfResult = $type->isTypeOf($value, $contextValue, $info);
if ($isTypeOfResult === null) {
continue;
}
$promise = $this->getPromise($isTypeOfResult);
if ($promise !== null) {
$promisedIsTypeOfResults[$index] = $promise;
} elseif ($isTypeOfResult === true) {
return $type;
}
}
if ($promisedIsTypeOfResults !== []) {
return $this->exeContext->promiseAdapter
->all($promisedIsTypeOfResults)
->then(static function ($isTypeOfResults) use ($possibleTypes): ?ObjectType {
foreach ($isTypeOfResults as $index => $result) {
if ($result) {
return $possibleTypes[$index];
}
}
return null;
});
}
return null;
}