@graphql-tools/stitch
Version:
A set of utils for faster development of GraphQL tools
28 lines (27 loc) • 1.22 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.forwardArgsToSelectionSet = void 0;
const graphql_1 = require("graphql");
const utils_1 = require("@graphql-tools/utils");
const forwardArgsToSelectionSet = (selectionSet, mapping) => {
const selectionSetDef = (0, utils_1.parseSelectionSet)(selectionSet, { noLocation: true });
return (field) => {
const selections = selectionSetDef.selections.map((selectionNode) => {
if (selectionNode.kind === graphql_1.Kind.FIELD) {
if (!mapping) {
return { ...selectionNode, arguments: field.arguments?.slice() };
}
else if (selectionNode.name.value in mapping) {
const selectionArgs = mapping[selectionNode.name.value];
return {
...selectionNode,
arguments: field.arguments?.filter((arg) => selectionArgs.includes(arg.name.value)),
};
}
}
return selectionNode;
});
return { ...selectionSetDef, selections };
};
};
exports.forwardArgsToSelectionSet = forwardArgsToSelectionSet;
;