function separateOperations(documentAST) {

  var operations = [];
  var fragments = Object.create(null);
  var positions = new Map();
  var depGraph = Object.create(null);
  var fromName = void 0;
  var idx = 0;

  // Populate metadata and build a dependency graph.
  (0, _visitor.visit)(documentAST, {
    OperationDefinition: function OperationDefinition(node) {
      fromName = opName(node);
      operations.push(node);
      positions.set(node, idx++);
    },
    FragmentDefinition: function FragmentDefinition(node) {
      fromName = node.name.value;
      fragments[fromName] = node;
      positions.set(node, idx++);
    },
    FragmentSpread: function FragmentSpread(node) {
      var toName = node.name.value;
      (depGraph[fromName] || (depGraph[fromName] = Object.create(null)))[toName] = true;
    }
  });

  // For each operation, produce a new synthesized AST which includes only what
  // is necessary for completing that operation.
  var separatedDocumentASTs = Object.create(null);
  operations.forEach(function (operation) {
    var operationName = opName(operation);
    var dependencies = Object.create(null);
    collectTransitiveDependencies(dependencies, depGraph, operationName);

    // The list of definition nodes to be included for this operation, sorted
    // to retain the same order as the original document.
    var definitions = [operation];
    Object.keys(dependencies).forEach(function (name) {
      definitions.push(fragments[name]);
    });
    definitions.sort(function (n1, n2) {
      return (positions.get(n1) || 0) - (positions.get(n2) || 0);
    });

    separatedDocumentASTs[operationName] = {
      kind: 'Document',
      definitions: definitions
    };
  });

  return separatedDocumentASTs;
}
/**
 * separateOperations accepts a single AST document which may contain many
 * operations and fragments and returns a collection of AST documents each of
 * which contains a single operation as well the fragment definitions it
 * refers to.
 */
separateOperations accepts a single AST document which may contain many
operations and fragments and returns a collection of AST documents each of
which contains a single operation as well the fragment definitions it
refers to.
separateOperations accepts a single AST document which may contain many operations and fragments and returns a collection of AST documents each of which contains a single operation as well the fragment definitions it refers to .
function separateOperations(documentAST) {
  var operations = [];
  var fragments = Object.create(null);
  var positions = new Map();
  var depGraph = Object.create(null);
  var fromName = void 0;
  var idx = 0;
  (0, _visitor.visit)(documentAST, {
    OperationDefinition: function OperationDefinition(node) {
      fromName = opName(node);
      operations.push(node);
      positions.set(node, idx++);
    },
    FragmentDefinition: function FragmentDefinition(node) {
      fromName = node.name.value;
      fragments[fromName] = node;
      positions.set(node, idx++);
    },
    FragmentSpread: function FragmentSpread(node) {
      var toName = node.name.value;
      (depGraph[fromName] || (depGraph[fromName] = Object.create(null)))[toName] = true;
    }
  });
  var separatedDocumentASTs = Object.create(null);
  operations.forEach(function (operation) {
    var operationName = opName(operation);
    var dependencies = Object.create(null);
    collectTransitiveDependencies(dependencies, depGraph, operationName);
    var definitions = [operation];
    Object.keys(dependencies).forEach(function (name) {
      definitions.push(fragments[name]);
    });
    definitions.sort(function (n1, n2) {
      return (positions.get(n1) || 0) - (positions.get(n2) || 0);
    });
    separatedDocumentASTs[operationName] = {
      kind: 'Document',
      definitions: definitions
    };
  });
  return separatedDocumentASTs;
}
function separateOperations ( documentAST ) { var operations = [ ] ; var fragments = Object . create ( null ) ; var positions = new Map ( ) ; var depGraph = Object . create ( null ) ; var fromName = void 0 ; var idx = 0 ; ( 0 , _visitor . visit ) ( documentAST , { OperationDefinition : function OperationDefinition ( node ) { fromName = opName ( node ) ; operations . push ( node ) ; positions . set ( node , idx ++ ) ; } , FragmentDefinition : function FragmentDefinition ( node ) { fromName = node . name . value ; fragments [ fromName ] = node ; positions . set ( node , idx ++ ) ; } , FragmentSpread : function FragmentSpread ( node ) { var toName = node . name . value ; ( depGraph [ fromName ] || ( depGraph [ fromName ] = Object . create ( null ) ) ) [ toName ] = true ; } } ) ; var separatedDocumentASTs = Object . create ( null ) ; operations . forEach ( function ( operation ) { var operationName = opName ( operation ) ; var dependencies = Object . create ( null ) ; collectTransitiveDependencies ( dependencies , depGraph , operationName ) ; var definitions = [ operation ] ; Object . keys ( dependencies ) . forEach ( function ( name ) { definitions . push ( fragments [ name ] ) ; } ) ; definitions . sort ( function ( n1 , n2 ) { return ( positions . get ( n1 ) || 0 ) - ( positions . get ( n2 ) || 0 ) ; } ) ; separatedDocumentASTs [ operationName ] = { kind : 'Document' , definitions : definitions } ; } ) ; return separatedDocumentASTs ; }
separateOperations accepts a single AST document which may contain many
 operations and fragments and returns a collection of AST documents each of
 which contains a single operation as well the fragment definitions it
 refers to.
separateOperations accepts a single AST document which may contain many operations and fragments and returns a collection of AST documents each of which contains a single operation as well the fragment definitions it refers to .
// Populate metadata and build a dependency graph. // For each operation, produce a new synthesized AST which includes only what // is necessary for completing that operation. // The list of definition nodes to be included for this operation, sorted // to retain the same order as the original document.