MCPcopy Create free account
hub / github.com/ardatan/graphql-tools / ensureValidRuntimeType

Function ensureValidRuntimeType

packages/executor/src/execution/execute.ts:1343–1399  ·  view source on GitHub ↗
(
  runtimeTypeName: unknown,
  exeContext: ExecutionContext,
  returnType: GraphQLAbstractType,
  fieldNodes: Array<FieldNode>,
  info: GraphQLResolveInfo,
  result: unknown,
)

Source from the content-addressed store, hash-verified

1341}
1342
1343function ensureValidRuntimeType(
1344 runtimeTypeName: unknown,
1345 exeContext: ExecutionContext,
1346 returnType: GraphQLAbstractType,
1347 fieldNodes: Array<FieldNode>,
1348 info: GraphQLResolveInfo,
1349 result: unknown,
1350): GraphQLObjectType {
1351 if (runtimeTypeName == null) {
1352 throw createGraphQLError(
1353 `Abstract type "${returnType.name}" must resolve to an Object type at runtime for field "${info.parentType.name}.${info.fieldName}". Either the "${returnType.name}" type should provide a "resolveType" function or each possible type should provide an "isTypeOf" function.`,
1354 { nodes: fieldNodes },
1355 );
1356 }
1357
1358 // releases before 16.0.0 supported returning `GraphQLObjectType` from `resolveType`
1359 // TODO: remove in 17.0.0 release
1360 if (isObjectType(runtimeTypeName)) {
1361 if (versionInfo.major >= 16) {
1362 throw createGraphQLError(
1363 'Support for returning GraphQLObjectType from resolveType was removed in graphql-js@16.0.0 please return type name instead.',
1364 );
1365 }
1366 runtimeTypeName = runtimeTypeName.name;
1367 }
1368
1369 if (typeof runtimeTypeName !== 'string') {
1370 throw createGraphQLError(
1371 `Abstract type "${returnType.name}" must resolve to an Object type at runtime for field "${info.parentType.name}.${info.fieldName}" with ` +
1372 `value ${inspect(result)}, received "${inspect(runtimeTypeName)}".`,
1373 );
1374 }
1375
1376 const runtimeType = exeContext.schema.getType(runtimeTypeName);
1377 if (runtimeType == null) {
1378 throw createGraphQLError(
1379 `Abstract type "${returnType.name}" was resolved to a type "${runtimeTypeName}" that does not exist inside the schema.`,
1380 { nodes: fieldNodes },
1381 );
1382 }
1383
1384 if (!isObjectType(runtimeType)) {
1385 throw createGraphQLError(
1386 `Abstract type "${returnType.name}" was resolved to a non-object type "${runtimeTypeName}".`,
1387 { nodes: fieldNodes },
1388 );
1389 }
1390
1391 if (!exeContext.schema.isSubType(returnType, runtimeType)) {
1392 throw createGraphQLError(
1393 `Runtime Object type "${runtimeType.name}" is not a possible type for "${returnType.name}".`,
1394 { nodes: fieldNodes },
1395 );
1396 }
1397
1398 return runtimeType;
1399}
1400

Callers 1

completeAbstractValueFunction · 0.85

Calls 3

createGraphQLErrorFunction · 0.90
inspectFunction · 0.85
getTypeMethod · 0.80

Tested by

no test coverage detected