UNPKG

@graphql-tools/stitch

Version:

A set of utils for faster development of GraphQL tools

24 lines (23 loc) 1.02 kB
import { Kind } from 'graphql'; import { parseSelectionSet } from '@graphql-tools/utils'; export const forwardArgsToSelectionSet = (selectionSet, mapping) => { const selectionSetDef = parseSelectionSet(selectionSet, { noLocation: true }); return (field) => { const selections = selectionSetDef.selections.map((selectionNode) => { if (selectionNode.kind === 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 }; }; };