execute.mjs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. import { devAssert } from '../jsutils/devAssert.mjs';
  2. import { inspect } from '../jsutils/inspect.mjs';
  3. import { invariant } from '../jsutils/invariant.mjs';
  4. import { isIterableObject } from '../jsutils/isIterableObject.mjs';
  5. import { isObjectLike } from '../jsutils/isObjectLike.mjs';
  6. import { isPromise } from '../jsutils/isPromise.mjs';
  7. import { memoize3 } from '../jsutils/memoize3.mjs';
  8. import { addPath, pathToArray } from '../jsutils/Path.mjs';
  9. import { promiseForObject } from '../jsutils/promiseForObject.mjs';
  10. import { promiseReduce } from '../jsutils/promiseReduce.mjs';
  11. import { GraphQLError } from '../error/GraphQLError.mjs';
  12. import { locatedError } from '../error/locatedError.mjs';
  13. import { OperationTypeNode } from '../language/ast.mjs';
  14. import { Kind } from '../language/kinds.mjs';
  15. import {
  16. isAbstractType,
  17. isLeafType,
  18. isListType,
  19. isNonNullType,
  20. isObjectType,
  21. } from '../type/definition.mjs';
  22. import {
  23. SchemaMetaFieldDef,
  24. TypeMetaFieldDef,
  25. TypeNameMetaFieldDef,
  26. } from '../type/introspection.mjs';
  27. import { assertValidSchema } from '../type/validate.mjs';
  28. import {
  29. collectFields,
  30. collectSubfields as _collectSubfields,
  31. } from './collectFields.mjs';
  32. import { getArgumentValues, getVariableValues } from './values.mjs';
  33. /**
  34. * A memoized collection of relevant subfields with regard to the return
  35. * type. Memoizing ensures the subfields are not repeatedly calculated, which
  36. * saves overhead when resolving lists of values.
  37. */
  38. const collectSubfields = memoize3((exeContext, returnType, fieldNodes) =>
  39. _collectSubfields(
  40. exeContext.schema,
  41. exeContext.fragments,
  42. exeContext.variableValues,
  43. returnType,
  44. fieldNodes,
  45. ),
  46. );
  47. /**
  48. * Terminology
  49. *
  50. * "Definitions" are the generic name for top-level statements in the document.
  51. * Examples of this include:
  52. * 1) Operations (such as a query)
  53. * 2) Fragments
  54. *
  55. * "Operations" are a generic name for requests in the document.
  56. * Examples of this include:
  57. * 1) query,
  58. * 2) mutation
  59. *
  60. * "Selections" are the definitions that can appear legally and at
  61. * single level of the query. These include:
  62. * 1) field references e.g `a`
  63. * 2) fragment "spreads" e.g. `...c`
  64. * 3) inline fragment "spreads" e.g. `...on Type { a }`
  65. */
  66. /**
  67. * Data that must be available at all points during query execution.
  68. *
  69. * Namely, schema of the type system that is currently executing,
  70. * and the fragments defined in the query document
  71. */
  72. /**
  73. * Implements the "Executing requests" section of the GraphQL specification.
  74. *
  75. * Returns either a synchronous ExecutionResult (if all encountered resolvers
  76. * are synchronous), or a Promise of an ExecutionResult that will eventually be
  77. * resolved and never rejected.
  78. *
  79. * If the arguments to this function do not result in a legal execution context,
  80. * a GraphQLError will be thrown immediately explaining the invalid input.
  81. */
  82. export function execute(args) {
  83. // Temporary for v15 to v16 migration. Remove in v17
  84. arguments.length < 2 ||
  85. devAssert(
  86. false,
  87. 'graphql@16 dropped long-deprecated support for positional arguments, please pass an object instead.',
  88. );
  89. const { schema, document, variableValues, rootValue } = args; // If arguments are missing or incorrect, throw an error.
  90. assertValidExecutionArguments(schema, document, variableValues); // If a valid execution context cannot be created due to incorrect arguments,
  91. // a "Response" with only errors is returned.
  92. const exeContext = buildExecutionContext(args); // Return early errors if execution context failed.
  93. if (!('schema' in exeContext)) {
  94. return {
  95. errors: exeContext,
  96. };
  97. } // Return a Promise that will eventually resolve to the data described by
  98. // The "Response" section of the GraphQL specification.
  99. //
  100. // If errors are encountered while executing a GraphQL field, only that
  101. // field and its descendants will be omitted, and sibling fields will still
  102. // be executed. An execution which encounters errors will still result in a
  103. // resolved Promise.
  104. //
  105. // Errors from sub-fields of a NonNull type may propagate to the top level,
  106. // at which point we still log the error and null the parent field, which
  107. // in this case is the entire response.
  108. try {
  109. const { operation } = exeContext;
  110. const result = executeOperation(exeContext, operation, rootValue);
  111. if (isPromise(result)) {
  112. return result.then(
  113. (data) => buildResponse(data, exeContext.errors),
  114. (error) => {
  115. exeContext.errors.push(error);
  116. return buildResponse(null, exeContext.errors);
  117. },
  118. );
  119. }
  120. return buildResponse(result, exeContext.errors);
  121. } catch (error) {
  122. exeContext.errors.push(error);
  123. return buildResponse(null, exeContext.errors);
  124. }
  125. }
  126. /**
  127. * Also implements the "Executing requests" section of the GraphQL specification.
  128. * However, it guarantees to complete synchronously (or throw an error) assuming
  129. * that all field resolvers are also synchronous.
  130. */
  131. export function executeSync(args) {
  132. const result = execute(args); // Assert that the execution was synchronous.
  133. if (isPromise(result)) {
  134. throw new Error('GraphQL execution failed to complete synchronously.');
  135. }
  136. return result;
  137. }
  138. /**
  139. * Given a completed execution context and data, build the `{ errors, data }`
  140. * response defined by the "Response" section of the GraphQL specification.
  141. */
  142. function buildResponse(data, errors) {
  143. return errors.length === 0
  144. ? {
  145. data,
  146. }
  147. : {
  148. errors,
  149. data,
  150. };
  151. }
  152. /**
  153. * Essential assertions before executing to provide developer feedback for
  154. * improper use of the GraphQL library.
  155. *
  156. * @internal
  157. */
  158. export function assertValidExecutionArguments(
  159. schema,
  160. document,
  161. rawVariableValues,
  162. ) {
  163. document || devAssert(false, 'Must provide document.'); // If the schema used for execution is invalid, throw an error.
  164. assertValidSchema(schema); // Variables, if provided, must be an object.
  165. rawVariableValues == null ||
  166. isObjectLike(rawVariableValues) ||
  167. devAssert(
  168. false,
  169. 'Variables must be provided as an Object where each property is a variable value. Perhaps look to see if an unparsed JSON string was provided.',
  170. );
  171. }
  172. /**
  173. * Constructs a ExecutionContext object from the arguments passed to
  174. * execute, which we will pass throughout the other execution methods.
  175. *
  176. * Throws a GraphQLError if a valid execution context cannot be created.
  177. *
  178. * @internal
  179. */
  180. export function buildExecutionContext(args) {
  181. var _definition$name, _operation$variableDe;
  182. const {
  183. schema,
  184. document,
  185. rootValue,
  186. contextValue,
  187. variableValues: rawVariableValues,
  188. operationName,
  189. fieldResolver,
  190. typeResolver,
  191. subscribeFieldResolver,
  192. } = args;
  193. let operation;
  194. const fragments = Object.create(null);
  195. for (const definition of document.definitions) {
  196. switch (definition.kind) {
  197. case Kind.OPERATION_DEFINITION:
  198. if (operationName == null) {
  199. if (operation !== undefined) {
  200. return [
  201. new GraphQLError(
  202. 'Must provide operation name if query contains multiple operations.',
  203. ),
  204. ];
  205. }
  206. operation = definition;
  207. } else if (
  208. ((_definition$name = definition.name) === null ||
  209. _definition$name === void 0
  210. ? void 0
  211. : _definition$name.value) === operationName
  212. ) {
  213. operation = definition;
  214. }
  215. break;
  216. case Kind.FRAGMENT_DEFINITION:
  217. fragments[definition.name.value] = definition;
  218. break;
  219. default: // ignore non-executable definitions
  220. }
  221. }
  222. if (!operation) {
  223. if (operationName != null) {
  224. return [new GraphQLError(`Unknown operation named "${operationName}".`)];
  225. }
  226. return [new GraphQLError('Must provide an operation.')];
  227. } // FIXME: https://github.com/graphql/graphql-js/issues/2203
  228. /* c8 ignore next */
  229. const variableDefinitions =
  230. (_operation$variableDe = operation.variableDefinitions) !== null &&
  231. _operation$variableDe !== void 0
  232. ? _operation$variableDe
  233. : [];
  234. const coercedVariableValues = getVariableValues(
  235. schema,
  236. variableDefinitions,
  237. rawVariableValues !== null && rawVariableValues !== void 0
  238. ? rawVariableValues
  239. : {},
  240. {
  241. maxErrors: 50,
  242. },
  243. );
  244. if (coercedVariableValues.errors) {
  245. return coercedVariableValues.errors;
  246. }
  247. return {
  248. schema,
  249. fragments,
  250. rootValue,
  251. contextValue,
  252. operation,
  253. variableValues: coercedVariableValues.coerced,
  254. fieldResolver:
  255. fieldResolver !== null && fieldResolver !== void 0
  256. ? fieldResolver
  257. : defaultFieldResolver,
  258. typeResolver:
  259. typeResolver !== null && typeResolver !== void 0
  260. ? typeResolver
  261. : defaultTypeResolver,
  262. subscribeFieldResolver:
  263. subscribeFieldResolver !== null && subscribeFieldResolver !== void 0
  264. ? subscribeFieldResolver
  265. : defaultFieldResolver,
  266. errors: [],
  267. };
  268. }
  269. /**
  270. * Implements the "Executing operations" section of the spec.
  271. */
  272. function executeOperation(exeContext, operation, rootValue) {
  273. const rootType = exeContext.schema.getRootType(operation.operation);
  274. if (rootType == null) {
  275. throw new GraphQLError(
  276. `Schema is not configured to execute ${operation.operation} operation.`,
  277. {
  278. nodes: operation,
  279. },
  280. );
  281. }
  282. const rootFields = collectFields(
  283. exeContext.schema,
  284. exeContext.fragments,
  285. exeContext.variableValues,
  286. rootType,
  287. operation.selectionSet,
  288. );
  289. const path = undefined;
  290. switch (operation.operation) {
  291. case OperationTypeNode.QUERY:
  292. return executeFields(exeContext, rootType, rootValue, path, rootFields);
  293. case OperationTypeNode.MUTATION:
  294. return executeFieldsSerially(
  295. exeContext,
  296. rootType,
  297. rootValue,
  298. path,
  299. rootFields,
  300. );
  301. case OperationTypeNode.SUBSCRIPTION:
  302. // TODO: deprecate `subscribe` and move all logic here
  303. // Temporary solution until we finish merging execute and subscribe together
  304. return executeFields(exeContext, rootType, rootValue, path, rootFields);
  305. }
  306. }
  307. /**
  308. * Implements the "Executing selection sets" section of the spec
  309. * for fields that must be executed serially.
  310. */
  311. function executeFieldsSerially(
  312. exeContext,
  313. parentType,
  314. sourceValue,
  315. path,
  316. fields,
  317. ) {
  318. return promiseReduce(
  319. fields.entries(),
  320. (results, [responseName, fieldNodes]) => {
  321. const fieldPath = addPath(path, responseName, parentType.name);
  322. const result = executeField(
  323. exeContext,
  324. parentType,
  325. sourceValue,
  326. fieldNodes,
  327. fieldPath,
  328. );
  329. if (result === undefined) {
  330. return results;
  331. }
  332. if (isPromise(result)) {
  333. return result.then((resolvedResult) => {
  334. results[responseName] = resolvedResult;
  335. return results;
  336. });
  337. }
  338. results[responseName] = result;
  339. return results;
  340. },
  341. Object.create(null),
  342. );
  343. }
  344. /**
  345. * Implements the "Executing selection sets" section of the spec
  346. * for fields that may be executed in parallel.
  347. */
  348. function executeFields(exeContext, parentType, sourceValue, path, fields) {
  349. const results = Object.create(null);
  350. let containsPromise = false;
  351. try {
  352. for (const [responseName, fieldNodes] of fields.entries()) {
  353. const fieldPath = addPath(path, responseName, parentType.name);
  354. const result = executeField(
  355. exeContext,
  356. parentType,
  357. sourceValue,
  358. fieldNodes,
  359. fieldPath,
  360. );
  361. if (result !== undefined) {
  362. results[responseName] = result;
  363. if (isPromise(result)) {
  364. containsPromise = true;
  365. }
  366. }
  367. }
  368. } catch (error) {
  369. if (containsPromise) {
  370. // Ensure that any promises returned by other fields are handled, as they may also reject.
  371. return promiseForObject(results).finally(() => {
  372. throw error;
  373. });
  374. }
  375. throw error;
  376. } // If there are no promises, we can just return the object
  377. if (!containsPromise) {
  378. return results;
  379. } // Otherwise, results is a map from field name to the result of resolving that
  380. // field, which is possibly a promise. Return a promise that will return this
  381. // same map, but with any promises replaced with the values they resolved to.
  382. return promiseForObject(results);
  383. }
  384. /**
  385. * Implements the "Executing fields" section of the spec
  386. * In particular, this function figures out the value that the field returns by
  387. * calling its resolve function, then calls completeValue to complete promises,
  388. * serialize scalars, or execute the sub-selection-set for objects.
  389. */
  390. function executeField(exeContext, parentType, source, fieldNodes, path) {
  391. var _fieldDef$resolve;
  392. const fieldDef = getFieldDef(exeContext.schema, parentType, fieldNodes[0]);
  393. if (!fieldDef) {
  394. return;
  395. }
  396. const returnType = fieldDef.type;
  397. const resolveFn =
  398. (_fieldDef$resolve = fieldDef.resolve) !== null &&
  399. _fieldDef$resolve !== void 0
  400. ? _fieldDef$resolve
  401. : exeContext.fieldResolver;
  402. const info = buildResolveInfo(
  403. exeContext,
  404. fieldDef,
  405. fieldNodes,
  406. parentType,
  407. path,
  408. ); // Get the resolve function, regardless of if its result is normal or abrupt (error).
  409. try {
  410. // Build a JS object of arguments from the field.arguments AST, using the
  411. // variables scope to fulfill any variable references.
  412. // TODO: find a way to memoize, in case this field is within a List type.
  413. const args = getArgumentValues(
  414. fieldDef,
  415. fieldNodes[0],
  416. exeContext.variableValues,
  417. ); // The resolve function's optional third argument is a context value that
  418. // is provided to every resolve function within an execution. It is commonly
  419. // used to represent an authenticated user, or request-specific caches.
  420. const contextValue = exeContext.contextValue;
  421. const result = resolveFn(source, args, contextValue, info);
  422. let completed;
  423. if (isPromise(result)) {
  424. completed = result.then((resolved) =>
  425. completeValue(exeContext, returnType, fieldNodes, info, path, resolved),
  426. );
  427. } else {
  428. completed = completeValue(
  429. exeContext,
  430. returnType,
  431. fieldNodes,
  432. info,
  433. path,
  434. result,
  435. );
  436. }
  437. if (isPromise(completed)) {
  438. // Note: we don't rely on a `catch` method, but we do expect "thenable"
  439. // to take a second callback for the error case.
  440. return completed.then(undefined, (rawError) => {
  441. const error = locatedError(rawError, fieldNodes, pathToArray(path));
  442. return handleFieldError(error, returnType, exeContext);
  443. });
  444. }
  445. return completed;
  446. } catch (rawError) {
  447. const error = locatedError(rawError, fieldNodes, pathToArray(path));
  448. return handleFieldError(error, returnType, exeContext);
  449. }
  450. }
  451. /**
  452. * @internal
  453. */
  454. export function buildResolveInfo(
  455. exeContext,
  456. fieldDef,
  457. fieldNodes,
  458. parentType,
  459. path,
  460. ) {
  461. // The resolve function's optional fourth argument is a collection of
  462. // information about the current execution state.
  463. return {
  464. fieldName: fieldDef.name,
  465. fieldNodes,
  466. returnType: fieldDef.type,
  467. parentType,
  468. path,
  469. schema: exeContext.schema,
  470. fragments: exeContext.fragments,
  471. rootValue: exeContext.rootValue,
  472. operation: exeContext.operation,
  473. variableValues: exeContext.variableValues,
  474. };
  475. }
  476. function handleFieldError(error, returnType, exeContext) {
  477. // If the field type is non-nullable, then it is resolved without any
  478. // protection from errors, however it still properly locates the error.
  479. if (isNonNullType(returnType)) {
  480. throw error;
  481. } // Otherwise, error protection is applied, logging the error and resolving
  482. // a null value for this field if one is encountered.
  483. exeContext.errors.push(error);
  484. return null;
  485. }
  486. /**
  487. * Implements the instructions for completeValue as defined in the
  488. * "Value Completion" section of the spec.
  489. *
  490. * If the field type is Non-Null, then this recursively completes the value
  491. * for the inner type. It throws a field error if that completion returns null,
  492. * as per the "Nullability" section of the spec.
  493. *
  494. * If the field type is a List, then this recursively completes the value
  495. * for the inner type on each item in the list.
  496. *
  497. * If the field type is a Scalar or Enum, ensures the completed value is a legal
  498. * value of the type by calling the `serialize` method of GraphQL type
  499. * definition.
  500. *
  501. * If the field is an abstract type, determine the runtime type of the value
  502. * and then complete based on that type
  503. *
  504. * Otherwise, the field type expects a sub-selection set, and will complete the
  505. * value by executing all sub-selections.
  506. */
  507. function completeValue(exeContext, returnType, fieldNodes, info, path, result) {
  508. // If result is an Error, throw a located error.
  509. if (result instanceof Error) {
  510. throw result;
  511. } // If field type is NonNull, complete for inner type, and throw field error
  512. // if result is null.
  513. if (isNonNullType(returnType)) {
  514. const completed = completeValue(
  515. exeContext,
  516. returnType.ofType,
  517. fieldNodes,
  518. info,
  519. path,
  520. result,
  521. );
  522. if (completed === null) {
  523. throw new Error(
  524. `Cannot return null for non-nullable field ${info.parentType.name}.${info.fieldName}.`,
  525. );
  526. }
  527. return completed;
  528. } // If result value is null or undefined then return null.
  529. if (result == null) {
  530. return null;
  531. } // If field type is List, complete each item in the list with the inner type
  532. if (isListType(returnType)) {
  533. return completeListValue(
  534. exeContext,
  535. returnType,
  536. fieldNodes,
  537. info,
  538. path,
  539. result,
  540. );
  541. } // If field type is a leaf type, Scalar or Enum, serialize to a valid value,
  542. // returning null if serialization is not possible.
  543. if (isLeafType(returnType)) {
  544. return completeLeafValue(returnType, result);
  545. } // If field type is an abstract type, Interface or Union, determine the
  546. // runtime Object type and complete for that type.
  547. if (isAbstractType(returnType)) {
  548. return completeAbstractValue(
  549. exeContext,
  550. returnType,
  551. fieldNodes,
  552. info,
  553. path,
  554. result,
  555. );
  556. } // If field type is Object, execute and complete all sub-selections.
  557. if (isObjectType(returnType)) {
  558. return completeObjectValue(
  559. exeContext,
  560. returnType,
  561. fieldNodes,
  562. info,
  563. path,
  564. result,
  565. );
  566. }
  567. /* c8 ignore next 6 */
  568. // Not reachable, all possible output types have been considered.
  569. false ||
  570. invariant(
  571. false,
  572. 'Cannot complete value of unexpected output type: ' + inspect(returnType),
  573. );
  574. }
  575. /**
  576. * Complete a list value by completing each item in the list with the
  577. * inner type
  578. */
  579. function completeListValue(
  580. exeContext,
  581. returnType,
  582. fieldNodes,
  583. info,
  584. path,
  585. result,
  586. ) {
  587. if (!isIterableObject(result)) {
  588. throw new GraphQLError(
  589. `Expected Iterable, but did not find one for field "${info.parentType.name}.${info.fieldName}".`,
  590. );
  591. } // This is specified as a simple map, however we're optimizing the path
  592. // where the list contains no Promises by avoiding creating another Promise.
  593. const itemType = returnType.ofType;
  594. let containsPromise = false;
  595. const completedResults = Array.from(result, (item, index) => {
  596. // No need to modify the info object containing the path,
  597. // since from here on it is not ever accessed by resolver functions.
  598. const itemPath = addPath(path, index, undefined);
  599. try {
  600. let completedItem;
  601. if (isPromise(item)) {
  602. completedItem = item.then((resolved) =>
  603. completeValue(
  604. exeContext,
  605. itemType,
  606. fieldNodes,
  607. info,
  608. itemPath,
  609. resolved,
  610. ),
  611. );
  612. } else {
  613. completedItem = completeValue(
  614. exeContext,
  615. itemType,
  616. fieldNodes,
  617. info,
  618. itemPath,
  619. item,
  620. );
  621. }
  622. if (isPromise(completedItem)) {
  623. containsPromise = true; // Note: we don't rely on a `catch` method, but we do expect "thenable"
  624. // to take a second callback for the error case.
  625. return completedItem.then(undefined, (rawError) => {
  626. const error = locatedError(
  627. rawError,
  628. fieldNodes,
  629. pathToArray(itemPath),
  630. );
  631. return handleFieldError(error, itemType, exeContext);
  632. });
  633. }
  634. return completedItem;
  635. } catch (rawError) {
  636. const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
  637. return handleFieldError(error, itemType, exeContext);
  638. }
  639. });
  640. return containsPromise ? Promise.all(completedResults) : completedResults;
  641. }
  642. /**
  643. * Complete a Scalar or Enum by serializing to a valid value, returning
  644. * null if serialization is not possible.
  645. */
  646. function completeLeafValue(returnType, result) {
  647. const serializedResult = returnType.serialize(result);
  648. if (serializedResult == null) {
  649. throw new Error(
  650. `Expected \`${inspect(returnType)}.serialize(${inspect(result)})\` to ` +
  651. `return non-nullable value, returned: ${inspect(serializedResult)}`,
  652. );
  653. }
  654. return serializedResult;
  655. }
  656. /**
  657. * Complete a value of an abstract type by determining the runtime object type
  658. * of that value, then complete the value for that type.
  659. */
  660. function completeAbstractValue(
  661. exeContext,
  662. returnType,
  663. fieldNodes,
  664. info,
  665. path,
  666. result,
  667. ) {
  668. var _returnType$resolveTy;
  669. const resolveTypeFn =
  670. (_returnType$resolveTy = returnType.resolveType) !== null &&
  671. _returnType$resolveTy !== void 0
  672. ? _returnType$resolveTy
  673. : exeContext.typeResolver;
  674. const contextValue = exeContext.contextValue;
  675. const runtimeType = resolveTypeFn(result, contextValue, info, returnType);
  676. if (isPromise(runtimeType)) {
  677. return runtimeType.then((resolvedRuntimeType) =>
  678. completeObjectValue(
  679. exeContext,
  680. ensureValidRuntimeType(
  681. resolvedRuntimeType,
  682. exeContext,
  683. returnType,
  684. fieldNodes,
  685. info,
  686. result,
  687. ),
  688. fieldNodes,
  689. info,
  690. path,
  691. result,
  692. ),
  693. );
  694. }
  695. return completeObjectValue(
  696. exeContext,
  697. ensureValidRuntimeType(
  698. runtimeType,
  699. exeContext,
  700. returnType,
  701. fieldNodes,
  702. info,
  703. result,
  704. ),
  705. fieldNodes,
  706. info,
  707. path,
  708. result,
  709. );
  710. }
  711. function ensureValidRuntimeType(
  712. runtimeTypeName,
  713. exeContext,
  714. returnType,
  715. fieldNodes,
  716. info,
  717. result,
  718. ) {
  719. if (runtimeTypeName == null) {
  720. throw new GraphQLError(
  721. `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.`,
  722. fieldNodes,
  723. );
  724. } // releases before 16.0.0 supported returning `GraphQLObjectType` from `resolveType`
  725. // TODO: remove in 17.0.0 release
  726. if (isObjectType(runtimeTypeName)) {
  727. throw new GraphQLError(
  728. 'Support for returning GraphQLObjectType from resolveType was removed in graphql-js@16.0.0 please return type name instead.',
  729. );
  730. }
  731. if (typeof runtimeTypeName !== 'string') {
  732. throw new GraphQLError(
  733. `Abstract type "${returnType.name}" must resolve to an Object type at runtime for field "${info.parentType.name}.${info.fieldName}" with ` +
  734. `value ${inspect(result)}, received "${inspect(runtimeTypeName)}".`,
  735. );
  736. }
  737. const runtimeType = exeContext.schema.getType(runtimeTypeName);
  738. if (runtimeType == null) {
  739. throw new GraphQLError(
  740. `Abstract type "${returnType.name}" was resolved to a type "${runtimeTypeName}" that does not exist inside the schema.`,
  741. {
  742. nodes: fieldNodes,
  743. },
  744. );
  745. }
  746. if (!isObjectType(runtimeType)) {
  747. throw new GraphQLError(
  748. `Abstract type "${returnType.name}" was resolved to a non-object type "${runtimeTypeName}".`,
  749. {
  750. nodes: fieldNodes,
  751. },
  752. );
  753. }
  754. if (!exeContext.schema.isSubType(returnType, runtimeType)) {
  755. throw new GraphQLError(
  756. `Runtime Object type "${runtimeType.name}" is not a possible type for "${returnType.name}".`,
  757. {
  758. nodes: fieldNodes,
  759. },
  760. );
  761. }
  762. return runtimeType;
  763. }
  764. /**
  765. * Complete an Object value by executing all sub-selections.
  766. */
  767. function completeObjectValue(
  768. exeContext,
  769. returnType,
  770. fieldNodes,
  771. info,
  772. path,
  773. result,
  774. ) {
  775. // Collect sub-fields to execute to complete this value.
  776. const subFieldNodes = collectSubfields(exeContext, returnType, fieldNodes); // If there is an isTypeOf predicate function, call it with the
  777. // current result. If isTypeOf returns false, then raise an error rather
  778. // than continuing execution.
  779. if (returnType.isTypeOf) {
  780. const isTypeOf = returnType.isTypeOf(result, exeContext.contextValue, info);
  781. if (isPromise(isTypeOf)) {
  782. return isTypeOf.then((resolvedIsTypeOf) => {
  783. if (!resolvedIsTypeOf) {
  784. throw invalidReturnTypeError(returnType, result, fieldNodes);
  785. }
  786. return executeFields(
  787. exeContext,
  788. returnType,
  789. result,
  790. path,
  791. subFieldNodes,
  792. );
  793. });
  794. }
  795. if (!isTypeOf) {
  796. throw invalidReturnTypeError(returnType, result, fieldNodes);
  797. }
  798. }
  799. return executeFields(exeContext, returnType, result, path, subFieldNodes);
  800. }
  801. function invalidReturnTypeError(returnType, result, fieldNodes) {
  802. return new GraphQLError(
  803. `Expected value of type "${returnType.name}" but got: ${inspect(result)}.`,
  804. {
  805. nodes: fieldNodes,
  806. },
  807. );
  808. }
  809. /**
  810. * If a resolveType function is not given, then a default resolve behavior is
  811. * used which attempts two strategies:
  812. *
  813. * First, See if the provided value has a `__typename` field defined, if so, use
  814. * that value as name of the resolved type.
  815. *
  816. * Otherwise, test each possible type for the abstract type by calling
  817. * isTypeOf for the object being coerced, returning the first type that matches.
  818. */
  819. export const defaultTypeResolver = function (
  820. value,
  821. contextValue,
  822. info,
  823. abstractType,
  824. ) {
  825. // First, look for `__typename`.
  826. if (isObjectLike(value) && typeof value.__typename === 'string') {
  827. return value.__typename;
  828. } // Otherwise, test each possible type.
  829. const possibleTypes = info.schema.getPossibleTypes(abstractType);
  830. const promisedIsTypeOfResults = [];
  831. for (let i = 0; i < possibleTypes.length; i++) {
  832. const type = possibleTypes[i];
  833. if (type.isTypeOf) {
  834. const isTypeOfResult = type.isTypeOf(value, contextValue, info);
  835. if (isPromise(isTypeOfResult)) {
  836. promisedIsTypeOfResults[i] = isTypeOfResult;
  837. } else if (isTypeOfResult) {
  838. return type.name;
  839. }
  840. }
  841. }
  842. if (promisedIsTypeOfResults.length) {
  843. return Promise.all(promisedIsTypeOfResults).then((isTypeOfResults) => {
  844. for (let i = 0; i < isTypeOfResults.length; i++) {
  845. if (isTypeOfResults[i]) {
  846. return possibleTypes[i].name;
  847. }
  848. }
  849. });
  850. }
  851. };
  852. /**
  853. * If a resolve function is not given, then a default resolve behavior is used
  854. * which takes the property of the source object of the same name as the field
  855. * and returns it as the result, or if it's a function, returns the result
  856. * of calling that function while passing along args and context value.
  857. */
  858. export const defaultFieldResolver = function (
  859. source,
  860. args,
  861. contextValue,
  862. info,
  863. ) {
  864. // ensure source is a value for which property access is acceptable.
  865. if (isObjectLike(source) || typeof source === 'function') {
  866. const property = source[info.fieldName];
  867. if (typeof property === 'function') {
  868. return source[info.fieldName](args, contextValue, info);
  869. }
  870. return property;
  871. }
  872. };
  873. /**
  874. * This method looks up the field on the given type definition.
  875. * It has special casing for the three introspection fields,
  876. * __schema, __type and __typename. __typename is special because
  877. * it can always be queried as a field, even in situations where no
  878. * other fields are allowed, like on a Union. __schema and __type
  879. * could get automatically added to the query type, but that would
  880. * require mutating type definitions, which would cause issues.
  881. *
  882. * @internal
  883. */
  884. export function getFieldDef(schema, parentType, fieldNode) {
  885. const fieldName = fieldNode.name.value;
  886. if (
  887. fieldName === SchemaMetaFieldDef.name &&
  888. schema.getQueryType() === parentType
  889. ) {
  890. return SchemaMetaFieldDef;
  891. } else if (
  892. fieldName === TypeMetaFieldDef.name &&
  893. schema.getQueryType() === parentType
  894. ) {
  895. return TypeMetaFieldDef;
  896. } else if (fieldName === TypeNameMetaFieldDef.name) {
  897. return TypeNameMetaFieldDef;
  898. }
  899. return parentType.getFields()[fieldName];
  900. }