separateOperations.js 2.9 KB

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