@graphql-mesh/utils
Version:
45 lines (44 loc) • 1.54 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.selectionSetOfData = selectionSetOfData;
/* eslint-disable no-labels */
const graphql_1 = require("graphql");
function selectionSetOfData(data) {
const selSet = {
kind: graphql_1.Kind.SELECTION_SET,
selections: [],
};
for (const fieldName of Object.keys(data)) {
const fieldValue = data[fieldName];
const selNode = {
kind: graphql_1.Kind.FIELD,
name: { kind: graphql_1.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;
}
;