apollo-client
Version:
A simple yet functional GraphQL client.
39 lines • 1.38 kB
JavaScript
import { checkDocument, } from './getFromAST';
import { cloneDeep } from '../util/cloneDeep';
var TYPENAME_FIELD = {
kind: 'Field',
name: {
kind: 'Name',
value: '__typename',
},
};
function addTypenameToSelectionSet(selectionSet, isRoot) {
if (isRoot === void 0) { isRoot = false; }
if (selectionSet.selections) {
if (!isRoot) {
var alreadyHasThisField = selectionSet.selections.some(function (selection) {
return selection.kind === 'Field' && selection.name.value === '__typename';
});
if (!alreadyHasThisField) {
selectionSet.selections.push(TYPENAME_FIELD);
}
}
selectionSet.selections.forEach(function (selection) {
if (selection.kind === 'Field' || selection.kind === 'InlineFragment') {
if (selection.selectionSet) {
addTypenameToSelectionSet(selection.selectionSet);
}
}
});
}
}
export function addTypenameToDocument(doc) {
checkDocument(doc);
var docClone = cloneDeep(doc);
docClone.definitions.forEach(function (definition) {
var isRoot = definition.kind === 'OperationDefinition';
addTypenameToSelectionSet(definition.selectionSet, isRoot);
});
return docClone;
}
//# sourceMappingURL=queryTransform.js.map