UNPKG

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