UNPKG

2.14 kBJavaScriptView Raw
1"use strict";
2var getFromAST_1 = require('./getFromAST');
3var cloneDeep = require('lodash.clonedeep');
4function addFieldToSelectionSet(fieldName, selectionSet) {
5 var fieldAst = {
6 kind: 'Field',
7 alias: null,
8 name: {
9 kind: 'Name',
10 value: fieldName,
11 },
12 };
13 if (selectionSet && selectionSet.selections) {
14 var alreadyHasThisField_1 = false;
15 selectionSet.selections.forEach(function (selection) {
16 if (selection.kind === 'Field' && selection.name.value === fieldName) {
17 alreadyHasThisField_1 = true;
18 }
19 });
20 if (!alreadyHasThisField_1) {
21 selectionSet.selections.push(fieldAst);
22 }
23 }
24}
25exports.addFieldToSelectionSet = addFieldToSelectionSet;
26function addTypenameToSelectionSet(selectionSet) {
27 return addFieldToSelectionSet('__typename', selectionSet);
28}
29exports.addTypenameToSelectionSet = addTypenameToSelectionSet;
30function traverseSelectionSet(selectionSet, queryTransformers, isRoot) {
31 if (isRoot === void 0) { isRoot = false; }
32 if (selectionSet && selectionSet.selections) {
33 queryTransformers.forEach(function (transformer) {
34 if (!isRoot) {
35 transformer(selectionSet);
36 }
37 selectionSet.selections.forEach(function (selection) {
38 if (selection.kind === 'Field' || selection.kind === 'InlineFragment') {
39 traverseSelectionSet(selection.selectionSet, queryTransformers);
40 }
41 });
42 });
43 }
44}
45function applyTransformers(doc, queryTransformers) {
46 getFromAST_1.checkDocument(doc);
47 var docClone = cloneDeep(doc);
48 docClone.definitions.forEach(function (definition) {
49 if (definition.kind === 'OperationDefinition') {
50 traverseSelectionSet(definition.selectionSet, queryTransformers, true);
51 }
52 else {
53 traverseSelectionSet(definition.selectionSet, queryTransformers);
54 }
55 });
56 return docClone;
57}
58exports.applyTransformers = applyTransformers;
59//# sourceMappingURL=queryTransform.js.map
\No newline at end of file