MCPcopy
hub / github.com/ardatan/graphql-tools / defaultTypeResolver

Function defaultTypeResolver

packages/executor/src/execution/execute.ts:1513–1551  ·  view source on GitHub ↗
(
  value,
  contextValue,
  info,
  abstractType,
)

Source from the content-addressed store, hash-verified

1511 * isTypeOf for the object being coerced, returning the first type that matches.
1512 */
1513export const defaultTypeResolver: GraphQLTypeResolver<unknown, unknown> = function (
1514 value,
1515 contextValue,
1516 info,
1517 abstractType,
1518) {
1519 // First, look for `__typename`.
1520 if (isObjectLike(value) && typeof value['__typename'] === 'string') {
1521 return value['__typename'];
1522 }
1523
1524 // Otherwise, test each possible type.
1525 const possibleTypes = info.schema.getPossibleTypes(abstractType);
1526 const promisedIsTypeOfResults: any[] = [];
1527
1528 for (let i = 0; i < possibleTypes.length; i++) {
1529 const type = possibleTypes[i];
1530
1531 if (type.isTypeOf) {
1532 const isTypeOfResult = type.isTypeOf(value, contextValue, info);
1533
1534 if (isPromise(isTypeOfResult)) {
1535 promisedIsTypeOfResults[i] = isTypeOfResult;
1536 } else if (isTypeOfResult) {
1537 return type.name;
1538 }
1539 }
1540 }
1541
1542 if (promisedIsTypeOfResults.length) {
1543 return Promise.all(promisedIsTypeOfResults).then(isTypeOfResults => {
1544 for (let i = 0; i < isTypeOfResults.length; i++) {
1545 if (isTypeOfResults[i]) {
1546 return possibleTypes[i].name;
1547 }
1548 }
1549 });
1550 }
1551};
1552
1553/**
1554 * If a resolve function is not given, then a default resolve behavior is used

Callers

nothing calls this directly

Calls 1

isObjectLikeFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…