Skip to content

Improve perf of separateOperations #710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/utilities/separateOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,23 @@ export function separateOperations(
): { [operationName: string]: DocumentNode } {

const operations = [];
const fragments = Object.create(null);
const positions = new Map();
const depGraph: DepGraph = Object.create(null);
let fromName;
let idx = 0;

// Populate the list of operations and build a dependency graph.
// Populate metadata and build a dependency graph.
visit(documentAST, {
OperationDefinition(node) {
operations.push(node);
fromName = opName(node);
operations.push(node);
positions.set(node, idx++);
},
FragmentDefinition(node) {
fromName = node.name.value;
fragments[fromName] = node;
positions.set(node, idx++);
},
FragmentSpread(node) {
const toName = node.name.value;
Expand All @@ -52,12 +58,19 @@ export function separateOperations(
const 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.
const definitions = [ operation ];
Object.keys(dependencies).forEach(name => {
definitions.push(fragments[name]);
});
definitions.sort(
(n1, n2) => (positions.get(n1) || 0) - (positions.get(n2) || 0)
);

separatedDocumentASTs[operationName] = {
kind: 'Document',
definitions: documentAST.definitions.filter(def =>
def === operation ||
def.kind === 'FragmentDefinition' && dependencies[def.name.value]
)
definitions
};
});

Expand Down