Skip to content

Commit

Permalink
Update webpack loader to support single query export (#154)
Browse files Browse the repository at this point in the history
* Update webpack loader to support single query export

* Adding a test to make sure named and default exports are the same
  • Loading branch information
stonexer authored and jnwng committed Feb 19, 2018
1 parent cf75a00 commit 39a3ff5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
16 changes: 10 additions & 6 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ module.exports = function(source) {
return accum;
}, 0);

if (operationCount <= 1) {
if (operationCount < 1) {
outputCode += `
module.exports = doc;
`
} else {
outputCode +=`
outputCode += `
// Collect any fragment/type references from a node, adding them to the refs Set
function collectFragmentReferences(node, refs) {
if (node.kind === "FragmentSpread") {
Expand Down Expand Up @@ -112,14 +112,14 @@ module.exports = function(source) {
return op.name ? op.name.value == name : false;
});
}
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];
// Now, for the operation we're running, find any fragments referenced by
// it or the fragments it references
var opRefs = definitionRefs[operationName] || new Set();
Expand All @@ -146,7 +146,7 @@ module.exports = function(source) {
newDoc.definitions.push(op);
}
});
return newDoc;
}
Expand All @@ -156,7 +156,11 @@ module.exports = function(source) {
for (const op of doc.definitions) {
if (op.kind === "OperationDefinition") {
if (!op.name) {
throw "Query/mutation names are required for a document with multiple definitions";
if (operationCount > 1) {
throw "Query/mutation names are required for a document with multiple definitions";
} else {
continue;
}
}

const opName = op.name.value;
Expand Down
45 changes: 34 additions & 11 deletions test/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ const assert = require('chai').assert;
assert.equal(module.exports.kind, 'Document');
});

it('parses single query through webpack loader', () => {
const jsSource = loader.call({ cacheable() {} }, `
query Q1 { testQuery }
`);
const module = { exports: undefined };
eval(jsSource);

assert.equal(module.exports.kind, 'Document');
assert.exists(module.exports.Q1);
assert.equal(module.exports.Q1.kind, 'Document');
assert.equal(module.exports.Q1.definitions.length, 1);
});

it('parses single query and exports as default', () => {
const jsSource = loader.call({ cacheable() {} }, `
query Q1 { testQuery }
`);
const module = { exports: undefined };
eval(jsSource);

assert.deepEqual(module.exports.definitions, module.exports.Q1.definitions);
});

it('parses multiple queries through webpack loader', () => {
const jsSource = loader.call({ cacheable() {} }, `
query Q1 { testQuery }
Expand All @@ -67,7 +90,7 @@ const assert = require('chai').assert;
fragment F3 on F { testQuery3 }
query Q1 { ...F1 }
query Q2 { ...F2 }
query Q3 {
query Q3 {
...F1
...F2
}
Expand All @@ -94,26 +117,26 @@ const assert = require('chai').assert;
assert.equal(Q3[0].name.value, 'Q3');
assert.equal(Q3[1].name.value, 'F1');
assert.equal(Q3[2].name.value, 'F2');

});

it('tracks fragment dependencies across nested fragments', () => {
const jsSource = loader.call({ cacheable() {} }, `
fragment F11 on F { testQuery }
fragment F22 on F {
fragment F22 on F {
...F11
testQuery2
testQuery2
}
fragment F33 on F {
...F22
testQuery3
}
query Q1 {
query Q1 {
...F33
}
query Q2 {
query Q2 {
id
}
`);
Expand All @@ -123,7 +146,7 @@ const assert = require('chai').assert;

assert.exists(module.exports.Q1);
assert.exists(module.exports.Q2);

const Q1 = module.exports.Q1.definitions;
const Q2 = module.exports.Q2.definitions;

Expand All @@ -132,7 +155,7 @@ const assert = require('chai').assert;
assert.equal(Q1[1].name.value, 'F33');
assert.equal(Q1[2].name.value, 'F22');
assert.equal(Q1[3].name.value, 'F11');

assert.equal(Q2.length, 1);
});

Expand Down Expand Up @@ -191,15 +214,15 @@ const assert = require('chai').assert;

assert.exists(module.exports.Q1);
assert.exists(module.exports.Q2);

const Q1 = module.exports.Q1.definitions;
const Q2 = module.exports.Q2.definitions;

assert.equal(Q1.length, 3);
assert.equal(Q1[0].name.value, 'Q1');
assert.equal(Q1[1].name.value, 'F111');
assert.equal(Q1[2].name.value, 'F222');

assert.equal(Q2.length, 1);
});

Expand Down

0 comments on commit 39a3ff5

Please sign in to comment.