UNPKG

graphql-mocks

Version:
106 lines (85 loc) 4.63 kB
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } import { isSchema, isObjectType, isAbstractType } from 'graphql'; import { applyWrappers } from '../resolver/apply-wrappers.js'; import { setResolver } from './set-resolver.js'; import { highlightAllCallback } from './utils/highlight-all-callback.js'; import { embedPackOptionsWrapper } from '../pack/utils/embed-pack-options-wrapper.js'; import { getResolver } from './get-resolver.js'; import { coerceHighlight } from '../highlight/utils/coerce-highlight.js'; import { getInstanceForReference } from '../highlight/utils/get-instance-for-reference.js'; import { isTypeReference } from '../highlight/utils/is-type-reference.js'; import { isFieldReference } from '../highlight/utils/is-field-reference.js'; import 'ramda'; import { combine } from '../highlight/highlighter/combine.js'; import { interfaces } from '../highlight/highlighter/interfaces.js'; import { resolvesTo } from '../highlight/highlighter/resolves-to.js'; import { union } from '../highlight/highlighter/union.js'; function embed(_ref) { var { resolver: resolverOption, highlight: coercibleHighlight = highlightAllCallback, wrappers = [], replace: replaceOption = false } = _ref; return /*#__PURE__*/function () { var _ref2 = _asyncToGenerator(function* (resolverMap, packOptions) { var schema = packOptions.dependencies.graphqlSchema; if (!isSchema(schema)) { throw new Error("\"graphqlSchema\" is an expected dependency, got type ".concat(typeof schema)); } var highlight = coerceHighlight(schema, coercibleHighlight).filter(combine(resolvesTo(), union(), interfaces())); for (var reference of highlight.references) { var existingResolver = getResolver(resolverMap, reference); // these MUST be kept in the local iteration // as to not replace the default option values var shouldReplace = replaceOption; var resolverToEmbed = resolverOption; if (resolverToEmbed && existingResolver && !shouldReplace) { var prettyReference = JSON.stringify(reference); throw new Error("Tried to add a new resolver via `embed` at ".concat(prettyReference, " but a resolver already ") + "exists there. If this was intended use the `replace` option to replace the existing resolver."); } if (typeof resolverToEmbed !== 'function') { // we are using the existing resolver to wrap and to put it back // in the resolver map. we will need to replace the original // with the wrapped shouldReplace = true; resolverToEmbed = existingResolver; } // no resolver to operate on; skip. if (!resolverToEmbed) { continue; } var type = void 0; var field = void 0; if (isTypeReference(reference)) { var instance = getInstanceForReference(schema, reference); type = instance; } if (isFieldReference(reference)) { var _instance = getInstanceForReference(schema, reference); type = _instance === null || _instance === void 0 ? void 0 : _instance[0]; field = _instance === null || _instance === void 0 ? void 0 : _instance[1]; } if (!type || !isObjectType(type) && !isAbstractType(type)) { throw new Error("reference ".concat(JSON.stringify(reference), " could not be resolved to a GraphQL Object, Union or Interface type")); } var wrappedResolver = yield applyWrappers(resolverToEmbed, [...wrappers, embedPackOptionsWrapper], { type, field, schema, resolverMap, packOptions }); setResolver(resolverMap, reference, wrappedResolver, { graphqlSchema: schema, replace: shouldReplace }); } return resolverMap; }); return function (_x, _x2) { return _ref2.apply(this, arguments); }; }(); } export { embed }; //# sourceMappingURL=embed.js.map