graphql-map-selections
Version:
A package to map selections from graphql resolver info object
52 lines • 1.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const mapFieldNode = (node) => {
if (!node)
return;
const { kind, name, selectionSet } = node;
if (kind !== "Field" || name.value.startsWith("__"))
return {};
if (!selectionSet)
return { [name.value]: true };
return { [name.value]: mapSelectionNodes(selectionSet.selections) };
};
const mapFragmentSpreadNode = (node) => {
console.log(`${node.kind} parser method not available`);
return {};
};
const mapInlineFragmentNode = (node) => {
if (!node)
return;
const { kind, typeCondition, selectionSet } = node;
const { name } = typeCondition;
if (kind !== "InlineFragment")
return {};
if (!selectionSet)
return { [name.value]: true };
return { [name.value]: mapSelectionNodes(selectionSet.selections) };
};
const mapNodeStrategies = {
Field: mapFieldNode,
FragmentSpread: mapFragmentSpreadNode,
InlineFragment: mapInlineFragmentNode,
};
const mapSelectionNodes = (selections) => {
if (!selections)
return;
let select = {};
selections.forEach((s) => {
const node = mapNodeStrategies[s.kind](s);
if (node)
select = Object.assign(Object.assign({}, select), node);
});
return select;
};
const mapSelections = (info) => {
if (!info)
return;
const { fieldName, fieldNodes } = info;
const { selectionSet } = fieldNodes.find((n) => n.name.value === fieldName);
return selectionSet ? mapSelectionNodes(selectionSet.selections) : {};
};
exports.default = mapSelections;
//# sourceMappingURL=mapSelections.js.map