UNPKG

2.99 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 = void 0;
22 var idx = 0;
23
24 // Populate metadata and build a dependency graph.
25 (0, _visitor.visit)(documentAST, {
26 OperationDefinition: function OperationDefinition(node) {
27 fromName = opName(node);
28 operations.push(node);
29 positions.set(node, idx++);
30 },
31 FragmentDefinition: function FragmentDefinition(node) {
32 fromName = node.name.value;
33 fragments[fromName] = node;
34 positions.set(node, idx++);
35 },
36 FragmentSpread: function FragmentSpread(node) {
37 var toName = node.name.value;
38 (depGraph[fromName] || (depGraph[fromName] = Object.create(null)))[toName] = true;
39 }
40 });
41
42 // For each operation, produce a new synthesized AST which includes only what
43 // is necessary for completing that operation.
44 var separatedDocumentASTs = Object.create(null);
45 operations.forEach(function (operation) {
46 var operationName = opName(operation);
47 var dependencies = Object.create(null);
48 collectTransitiveDependencies(dependencies, depGraph, operationName);
49
50 // The list of definition nodes to be included for this operation, sorted
51 // to retain the same order as the original document.
52 var definitions = [operation];
53 Object.keys(dependencies).forEach(function (name) {
54 definitions.push(fragments[name]);
55 });
56 definitions.sort(function (n1, n2) {
57 return (positions.get(n1) || 0) - (positions.get(n2) || 0);
58 });
59
60 separatedDocumentASTs[operationName] = {
61 kind: 'Document',
62 definitions: definitions
63 };
64 });
65
66 return separatedDocumentASTs;
67} /**
68 * Copyright (c) 2015-present, Facebook, Inc.
69 *
70 * This source code is licensed under the MIT license found in the
71 * LICENSE file in the root directory of this source tree.
72 *
73 *
74 */
75
76// Provides the empty string for anonymous operations.
77function opName(operation) {
78 return operation.name ? operation.name.value : '';
79}
80
81// From a dependency graph, collects a list of transitive dependencies by
82// recursing through a dependency graph.
83function collectTransitiveDependencies(collected, depGraph, fromName) {
84 var immediateDeps = depGraph[fromName];
85 if (immediateDeps) {
86 Object.keys(immediateDeps).forEach(function (toName) {
87 if (!collected[toName]) {
88 collected[toName] = true;
89 collectTransitiveDependencies(collected, depGraph, toName);
90 }
91 });
92 }
93}
\No newline at end of file