Spaces:
Running
Running
File size: 2,778 Bytes
d4b85c0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true,
});
exports.separateOperations = separateOperations;
var _kinds = require('../language/kinds.js');
var _visitor = require('../language/visitor.js');
/**
* separateOperations accepts a single AST document which may contain many
* operations and fragments and returns a collection of AST documents each of
* which contains a single operation as well the fragment definitions it
* refers to.
*/
function separateOperations(documentAST) {
const operations = [];
const depGraph = Object.create(null); // Populate metadata and build a dependency graph.
for (const definitionNode of documentAST.definitions) {
switch (definitionNode.kind) {
case _kinds.Kind.OPERATION_DEFINITION:
operations.push(definitionNode);
break;
case _kinds.Kind.FRAGMENT_DEFINITION:
depGraph[definitionNode.name.value] = collectDependencies(
definitionNode.selectionSet,
);
break;
default: // ignore non-executable definitions
}
} // For each operation, produce a new synthesized AST which includes only what
// is necessary for completing that operation.
const separatedDocumentASTs = Object.create(null);
for (const operation of operations) {
const dependencies = new Set();
for (const fragmentName of collectDependencies(operation.selectionSet)) {
collectTransitiveDependencies(dependencies, depGraph, fragmentName);
} // Provides the empty string for anonymous operations.
const operationName = operation.name ? operation.name.value : ''; // The list of definition nodes to be included for this operation, sorted
// to retain the same order as the original document.
separatedDocumentASTs[operationName] = {
kind: _kinds.Kind.DOCUMENT,
definitions: documentAST.definitions.filter(
(node) =>
node === operation ||
(node.kind === _kinds.Kind.FRAGMENT_DEFINITION &&
dependencies.has(node.name.value)),
),
};
}
return separatedDocumentASTs;
}
// From a dependency graph, collects a list of transitive dependencies by
// recursing through a dependency graph.
function collectTransitiveDependencies(collected, depGraph, fromName) {
if (!collected.has(fromName)) {
collected.add(fromName);
const immediateDeps = depGraph[fromName];
if (immediateDeps !== undefined) {
for (const toName of immediateDeps) {
collectTransitiveDependencies(collected, depGraph, toName);
}
}
}
}
function collectDependencies(selectionSet) {
const dependencies = [];
(0, _visitor.visit)(selectionSet, {
FragmentSpread(node) {
dependencies.push(node.name.value);
},
});
return dependencies;
}
|