separateOperations.js 2.8 KB

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