@graphql-mesh/transport-rest
Version:
68 lines (67 loc) • 2.82 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.processLinkFieldAnnotations = void 0;
const dset_1 = require("dset");
const string_interpolation_1 = require("@graphql-mesh/string-interpolation");
const utils_1 = require("@graphql-tools/utils");
function processLinkFieldAnnotations(field, defaultRootTypeName, defaultFieldName) {
field.resolve = function linkDirectiveHandler(root, args, context, info) {
const linkResolverMap = findLinkResolverMap({
schema: info.schema,
defaultRootTypeName,
defaultFieldName,
parentFieldName: root.$field,
operationType: info.operation.operation,
});
const linkResolverOpts = linkResolverMap[field.name];
return linkResolver(linkResolverOpts, { root, args, context, info, env: process.env });
};
}
exports.processLinkFieldAnnotations = processLinkFieldAnnotations;
function findLinkResolverMap({ schema, operationType, defaultRootTypeName, defaultFieldName, }) {
const parentType = schema.getRootType(operationType);
const parentField = parentType.getFields()[operationType];
if (parentField) {
const linkResolverMap = getLinkResolverMap(schema, parentField);
if (linkResolverMap) {
return linkResolverMap;
}
}
const defaultRootType = schema.getType(defaultRootTypeName);
if (defaultRootType) {
const defaultField = defaultRootType.getFields()[defaultFieldName];
if (defaultField) {
const linkResolverMap = getLinkResolverMap(schema, defaultField);
if (linkResolverMap) {
return linkResolverMap;
}
}
}
}
function getLinkResolverMap(schema, field) {
const parentFieldLinkResolverDirectives = (0, utils_1.getDirective)(schema, field, 'linkResolver');
if (parentFieldLinkResolverDirectives?.length) {
const linkResolverMap = parentFieldLinkResolverDirectives[0].linkResolverMap;
if (linkResolverMap) {
return linkResolverMap;
}
}
}
function linkResolver({ linkObjArgs, targetTypeName, targetFieldName }, { root, args, context, info, env }) {
for (const argKey in linkObjArgs) {
const argInterpolation = linkObjArgs[argKey];
const actualValue = typeof argInterpolation === 'string'
? string_interpolation_1.stringInterpolator.parse(argInterpolation, {
root,
args,
context,
info,
env,
})
: argInterpolation;
(0, dset_1.dset)(args, argKey, actualValue);
}
const type = info.schema.getType(targetTypeName);
const field = type.getFields()[targetFieldName];
return field.resolve(root, args, context, info);
}
;