UNPKG

graphql-mocks

Version:

Tools for setting up graphql test resolvers

95 lines (94 loc) 4.87 kB
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } import { isSchema, isObjectType, isAbstractType } from 'graphql'; import { applyWrappers } from '../resolver/apply-wrappers.mjs'; import { setResolver } from './set-resolver.mjs'; import { highlightAllCallback } from './utils/highlight-all-callback.mjs'; import { getResolver } from './get-resolver.mjs'; import { coerceHighlight } from '../highlight/utils/coerce-highlight.mjs'; import { getInstanceForReference } from '../highlight/utils/get-instance-for-reference.mjs'; import { isTypeReference } from '../highlight/utils/is-type-reference.mjs'; import { isFieldReference } from '../highlight/utils/is-field-reference.mjs'; import { combine } from '../highlight/highlighter/combine.mjs'; import { interfaces } from '../highlight/highlighter/interfaces.mjs'; import { resolvesTo } from '../highlight/highlighter/resolves-to.mjs'; import { union } from '../highlight/highlighter/union.mjs'; import { embedPackOptionsWrapper } from '../pack/utils/embed-pack-options-wrapper.mjs'; function embed(_ref) { var { resolver: resolverOption, highlight: coercibleHighlight = highlightAllCallback, wrappers = [], replace: replaceOption = false } = _ref; return /*#__PURE__*/function () { var _ref2 = _asyncToGenerator(function* (resolverMap, packOptions) { wrappers = [...wrappers]; 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 _wrappers; 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")); } if ((_wrappers = wrappers) !== null && _wrappers !== void 0 && _wrappers.length) { // if there is at least one wrapper, the embedPackOptionsWrapper should also // be included in order to better manage the context and ensure that the `pack` // property is included wrappers = [embedPackOptionsWrapper, ...wrappers]; resolverToEmbed = yield applyWrappers(resolverToEmbed, [...wrappers], { type, field, schema, resolverMap, packOptions }); } setResolver(resolverMap, reference, resolverToEmbed, { graphqlSchema: schema, replace: shouldReplace }); } return resolverMap; }); return function (_x, _x2) { return _ref2.apply(this, arguments); }; }(); } export { embed }; //# sourceMappingURL=embed.mjs.map