UNPKG

graphql-mocks

Version:
114 lines (81 loc) 3.95 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var graphql = require('graphql'); var applyWrappers = require('../resolver/apply-wrappers.js'); var setResolver = require('./set-resolver.js'); var highlightAllCallback = require('./utils/highlight-all-callback.js'); var embedPackOptionsWrapper = require('../pack/utils/embed-pack-options-wrapper.js'); var getResolver = require('./get-resolver.js'); var coerceHighlight = require('../highlight/utils/coerce-highlight.js'); var getInstanceForReference = require('../highlight/utils/get-instance-for-reference.js'); var isTypeReference = require('../highlight/utils/is-type-reference.js'); var isFieldReference = require('../highlight/utils/is-field-reference.js'); require('ramda'); var combine = require('../highlight/highlighter/combine.js'); var interfaces = require('../highlight/highlighter/interfaces.js'); var resolvesTo = require('../highlight/highlighter/resolves-to.js'); var union = require('../highlight/highlighter/union.js'); function embed({ resolver: resolverOption, highlight: coercibleHighlight = highlightAllCallback.highlightAllCallback, wrappers = [], replace: replaceOption = false }) { return async (resolverMap, packOptions) => { const schema = packOptions.dependencies.graphqlSchema; if (!graphql.isSchema(schema)) { throw new Error(`"graphqlSchema" is an expected dependency, got type ${typeof schema}`); } const highlight = coerceHighlight.coerceHighlight(schema, coercibleHighlight).filter(combine.combine(resolvesTo.resolvesTo(), union.union(), interfaces.interfaces())); for (const reference of highlight.references) { const existingResolver = getResolver.getResolver(resolverMap, reference); // these MUST be kept in the local iteration // as to not replace the default option values let shouldReplace = replaceOption; let resolverToEmbed = resolverOption; if (resolverToEmbed && existingResolver && !shouldReplace) { const prettyReference = JSON.stringify(reference); throw new Error(`Tried to add a new resolver via \`embed\` at ${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; } let type; let field; if (isTypeReference.isTypeReference(reference)) { const instance = getInstanceForReference.getInstanceForReference(schema, reference); type = instance; } if (isFieldReference.isFieldReference(reference)) { const instance = getInstanceForReference.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 || !graphql.isObjectType(type) && !graphql.isAbstractType(type)) { throw new Error(`reference ${JSON.stringify(reference)} could not be resolved to a GraphQL Object, Union or Interface type`); } const wrappedResolver = await applyWrappers.applyWrappers(resolverToEmbed, [...wrappers, embedPackOptionsWrapper.embedPackOptionsWrapper], { type, field, schema, resolverMap, packOptions }); setResolver.setResolver(resolverMap, reference, wrappedResolver, { graphqlSchema: schema, replace: shouldReplace }); } return resolverMap; }; } exports.embed = embed; //# sourceMappingURL=embed.js.map