diff --git a/src/index.js b/src/index.js index f52711e4..ad380fe7 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,7 @@ var parser = require('graphql/language/parser'); - +var printer = require('graphql/language/printer'); var parse = parser.parse; +var print = printer.print; // Strip insignificant whitespace // Note that this could do a lot more, such as reorder fields etc. @@ -159,7 +160,7 @@ function gql(/* arguments */) { for (var i = 1; i < args.length; i++) { if (args[i] && args[i].kind && args[i].kind === 'Document') { - result += args[i].loc.source.body; + result += print(args[i]); } else { result += args[i]; } diff --git a/test/graphql.js b/test/graphql.js index 726e3918..4c605abb 100644 --- a/test/graphql.js +++ b/test/graphql.js @@ -210,6 +210,37 @@ const assert = require('chai').assert; assert.equal(definitions[1].kind, 'FragmentDefinition'); }); + 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 + }`; + }; + + 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('tracks fragment dependencies across fragments loaded via the webpack loader', () => { const query = `#import "./fragment_definition.graphql" fragment F111 on F {