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 1 commit
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
8 changes: 7 additions & 1 deletion loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ module.exports = function(source) {
module.exports = doc;
`

// Make copy before named exports are added to the doc
// see https://github.com/apollographql/graphql-tag/issues/168
outputCode += `
var docCopy = Object.assign({}, doc);
Copy link
Contributor

@jnwng jnwng Apr 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is partially my fault for not establishing what our compatibility matrix is, but this almost certain will break for ie 11. is it possible to use an alternative method here to assign / clone this object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. The only properties on GraphQL AST's are kind, definitions, and loc, right? Would you like me to change the other instances of Object.assign() while I'm at it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, yes, i didnt notice that we had those in there as well. if its not too much trouble, that would be great! otherwise im happy to merge this and we can address the compatibility issue afterwards (since its already an issue)

`

for (const op of doc.definitions) {
if (op.kind === "OperationDefinition") {
if (!op.name) {
Expand All @@ -165,7 +171,7 @@ module.exports = function(source) {

const opName = op.name.value;
outputCode += `
module.exports["${opName}"] = oneQuery(doc, "${opName}");
module.exports["${opName}"] = oneQuery(docCopy, "${opName}");
`
}
}
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