Skip to content
Open
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
19 changes: 16 additions & 3 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,23 @@ function expandImports(source, doc) {

module.exports = function(source) {
this.cacheable();
const doc = gql`${source}`;
const doc = gql(source, true);

if (doc.definitions) {
for (var i = 0; i < doc.definitions.length; i++) {
if (doc.definitions[i].loc) {
doc.definitions[i].loc = {
start: doc.definitions[i].loc.start,
end: doc.definitions[i].loc.end,
source: doc.definitions[i].loc.source
}
}
}
}

let headerCode = `
var doc = ${JSON.stringify(doc)};
doc.loc.source = ${JSON.stringify(doc.loc.source)};
var doc = ${JSON.stringify(doc, null, 4)};
doc.loc.source = ${JSON.stringify(doc.loc.source, null, 4)};
`;

let outputCode = "";
Expand Down
27 changes: 24 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function stripLoc(doc, removeLocAtThisLevel) {
}

var experimentalFragmentVariables = false;
function parseDocument(doc) {
function parseDocument(doc, asImport) {
var cacheKey = normalize(doc);

if (docCache[cacheKey]) {
Expand All @@ -134,7 +134,11 @@ function parseDocument(doc) {
// check that all "new" fragments inside the documents are consistent with
// existing fragments of the same name
parsed = processFragments(parsed);
parsed = stripLoc(parsed, false);
if (asImport) {
parsed.definitions = stripLoc(parsed.definitions, false);
} else {
parsed = stripLoc(parsed, false);
}
docCache[cacheKey] = parsed;

return parsed;
Expand All @@ -151,23 +155,40 @@ function disableExperimentalFragmentVariables() {
// XXX This should eventually disallow arbitrary string interpolation, like Relay does
function gql(/* arguments */) {
var args = Array.prototype.slice.call(arguments);
var asImport = args[args.length - 1] === true;
if (asImport) {
args = args.slice(0, -1);
}

var literals = args[0];

// We always get literals[0] and then matching post literals for each arg given
var result = (typeof(literals) === "string") ? literals : literals[0];
var definitions = [];

for (var i = 1; i < args.length; i++) {
if (args[i] && args[i].kind && args[i].kind === 'Document') {
result += args[i].loc.source.body;
definitions = definitions.concat(args[i].definitions)
} else {
result += args[i];
}

result += literals[i];
}

definitions = definitions.filter(function (def) {
return def.kind === 'FragmentDefinition' && def.loc
})

var doc = parseDocument(result, asImport || definitions.length);

if (definitions.length > 0) {
doc.definitions = doc.definitions.concat(definitions)
doc = processFragments(doc)
}

return parseDocument(result);
return doc;
}

// Support typescript, which isn't as nice as Babel about default exports
Expand Down
32 changes: 32 additions & 0 deletions test/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,38 @@ const assert = require('chai').assert;

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

it('correctly interpolates imports of other files through the webpack loader', () => {
const query = `#import "./fragment_definition.graphql"

fragment BooksAuthor on Book {
author {
...AuthorDetails
}
}
`;
const jsSource = loader.call({ cacheable() {} }, query);

const oldRequire = require;
const module = { exports: undefined };
const require = (path) => {
assert.equal(path, './fragment_definition.graphql');
return gql(`
fragment AuthorDetails on Author {
firstName
lastName
}`, true);
};

eval(jsSource);

const document = gql`query { ...BooksAuthor } ${module.exports}`;
assert.equal(document.kind, 'Document');
assert.equal(document.definitions.length, 3);
assert.equal(document.definitions[0].kind, 'OperationDefinition');
assert.equal(document.definitions[1].kind, 'FragmentDefinition');
assert.equal(document.definitions[2].kind, 'FragmentDefinition');
});

it('does not complain when presented with normal comments', (done) => {
assert.doesNotThrow(() => {
Expand Down