Skip to content

separateOperations: stop unused fragments from being included #2853

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
49 changes: 49 additions & 0 deletions src/utilities/__tests__/separateOperations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,53 @@ describe('separateOperations', () => {
}
`);
});

it('only includes fragments that are used', () => {
const ast = parse(`
fragment One on T {
oneField
...Three
}

fragment Two on T {
twoField
}

fragment Three on T {
threeField
}

query One {
...Two
}

query Ones {
...One
}

query Twos {
...One
...Two
}
`);

const separatedASTs = separateOperations(ast);

expect(separatedASTs).to.have.all.keys('One', 'Ones', 'Twos');

expect(print(separatedASTs.Ones)).to.equal(dedent`
fragment One on T {
oneField
...Three
}

fragment Three on T {
threeField
}

query Ones {
...One
}
`);
});
});
24 changes: 19 additions & 5 deletions src/utilities/separateOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ export function separateOperations(
// Populate metadata and build a dependency graph.
visit(documentAST, {
OperationDefinition(node) {
fromName = opName(node);
fromName = opSuffix(opName(node));
operations.push(node);
},
FragmentDefinition(node) {
fromName = node.name.value;
fromName = fragSuffix(node.name.value);
},
FragmentSpread(node) {
const toName = node.name.value;
const toName = fragSuffix(node.name.value);
let dependents = depGraph[fromName];
if (dependents === undefined) {
dependents = depGraph[fromName] = Object.create(null);
Expand All @@ -42,7 +42,11 @@ export function separateOperations(
for (const operation of operations) {
const operationName = opName(operation);
const dependencies = Object.create(null);
collectTransitiveDependencies(dependencies, depGraph, operationName);
collectTransitiveDependencies(
dependencies,
depGraph,
opSuffix(operationName),
);

// The list of definition nodes to be included for this operation, sorted
// to retain the same order as the original document.
Expand All @@ -52,7 +56,7 @@ export function separateOperations(
(node) =>
node === operation ||
(node.kind === Kind.FRAGMENT_DEFINITION &&
dependencies[node.name.value]),
dependencies[fragSuffix(node.name.value)]),
),
};
}
Expand All @@ -67,6 +71,16 @@ function opName(operation: OperationDefinitionNode): string {
return operation.name ? operation.name.value : '';
}

// Adds a suffix to differentiate operations
function opSuffix(operationName: string): string {
return `${operationName}${Kind.OPERATION_DEFINITION}`;
}

// Adds a suffix to differentiate fragments
function fragSuffix(fragmentName: string): string {
return `${fragmentName}${Kind.FRAGMENT_DEFINITION}`;
}

// From a dependency graph, collects a list of transitive dependencies by
// recursing through a dependency graph.
function collectTransitiveDependencies(
Expand Down