UNPKG

@graphql-mesh/utils

Version:
42 lines (41 loc) 1.38 kB
/* eslint-disable no-labels */ import { Kind } from 'graphql'; export function selectionSetOfData(data) { const selSet = { kind: Kind.SELECTION_SET, selections: [], }; for (const fieldName of Object.keys(data)) { const fieldValue = data[fieldName]; const selNode = { kind: Kind.FIELD, name: { kind: Kind.NAME, value: fieldName }, }; if (fieldValue && typeof fieldValue === 'object') { if (Array.isArray(fieldValue)) { // we assume that all items in the array are of the same shape, so we look at the first one const firstItem = fieldValue[0]; if (firstItem && typeof firstItem === 'object') { selSet.selections.push({ ...selNode, selectionSet: selectionSetOfData(firstItem), }); } else { // is an array of scalars selSet.selections.push(selNode); } } else { selSet.selections.push({ ...selNode, selectionSet: selectionSetOfData(fieldValue), }); } } else { selSet.selections.push(selNode); } } return selSet; }