UNPKG

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