Skip to content
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

Fix duplicate queries in named exports #170

Merged
merged 2 commits into from
Apr 19, 2018
Merged
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
11 changes: 7 additions & 4 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,13 @@ module.exports = function(source) {

function oneQuery(doc, operationName) {
// Copy the DocumentNode, but clear out the definitions
var newDoc = Object.assign({}, doc);

var op = findOperation(doc, operationName);
newDoc.definitions = [op];
var newDoc = {
kind: doc.kind,
definitions: [findOperation(doc, operationName)]
};
if (doc.hasOwnProperty("loc")) {
newDoc.loc = doc.loc;
}

// Now, for the operation we're running, find any fragments referenced by
// it or the fragments it references
Expand Down
15 changes: 15 additions & 0 deletions test/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ const assert = require('chai').assert;
assert.equal(module.exports.Q2.definitions.length, 1);
});

// see https://github.com/apollographql/graphql-tag/issues/168
it('does not nest queries needlessly in named exports', () => {
const jsSource = loader.call({ cacheable() {} }, `
query Q1 { testQuery }
query Q2 { testQuery2 }
query Q3 { test Query3 }
`);
const module = { exports: undefined };
eval(jsSource);

assert.notExists(module.exports.Q2.Q1);
assert.notExists(module.exports.Q3.Q1);
assert.notExists(module.exports.Q3.Q2);
});

it('tracks fragment dependencies from multiple queries through webpack loader', () => {
const jsSource = loader.call({ cacheable() {} }, `
fragment F1 on F { testQuery }
Expand Down