diff --git a/src/assets/GraphqlAsset.js b/src/assets/GraphqlAsset.js index 62e7a3563d9..da8459f1579 100644 --- a/src/assets/GraphqlAsset.js +++ b/src/assets/GraphqlAsset.js @@ -1,6 +1,10 @@ const Asset = require('../Asset'); const localRequire = require('../utils/localRequire'); +const dummyLoaderContext = { + cacheable() {} +}; + class GraphqlAsset extends Asset { constructor(name, options) { super(name, options); @@ -8,12 +12,12 @@ class GraphqlAsset extends Asset { } async parse(code) { - let gql = await localRequire('graphql-tag', this.name); - return gql(code); + const loader = await localRequire('graphql-tag/loader', this.name); + return loader.call(dummyLoaderContext, code); } generate() { - return `module.exports=${JSON.stringify(this.ast, false, 2)};`; + return this.ast; } } diff --git a/test/graphql.js b/test/graphql.js index ed6eacc93af..02bac6bdadd 100644 --- a/test/graphql.js +++ b/test/graphql.js @@ -35,4 +35,51 @@ describe('graphql', function() { `.definitions ); }); + + it('should support requiring graphql files that use fragments #import', async function() { + let b = await bundle( + __dirname + '/integration/graphql/withFragmentImport.js' + ); + + await assertBundleTree(b, { + name: 'withFragmentImport.js', + assets: [ + 'withFragmentImport.js', + 'withFragmentImport.graphql', + 'projectFragment.graphql' + ], + childBundles: [ + { + type: 'map' + } + ] + }); + + let output = await run(b); + assert.equal(typeof output, 'function'); + assert.deepEqual( + output().definitions, + gql` + { + user(id: 5) { + ...UserFragment + projects { + ...ProjectFragment + } + } + } + + fragment UserFragment on User { + firstName + lastName + } + + fragment ProjectFragment on Project { + internalReference + name + } + + `.definitions + ); + }); }); diff --git a/test/integration/graphql/fragments/projectFragment.graphql b/test/integration/graphql/fragments/projectFragment.graphql new file mode 100644 index 00000000000..4010c65c075 --- /dev/null +++ b/test/integration/graphql/fragments/projectFragment.graphql @@ -0,0 +1,4 @@ +fragment ProjectFragment on Project { + internalReference + name +} \ No newline at end of file diff --git a/test/integration/graphql/withFragmentImport.graphql b/test/integration/graphql/withFragmentImport.graphql new file mode 100644 index 00000000000..5e7e18bc02b --- /dev/null +++ b/test/integration/graphql/withFragmentImport.graphql @@ -0,0 +1,14 @@ +#import './fragments/projectFragment.graphql' + +{ + user(id: 5) { + ...UserFragment + projects { + ...ProjectFragment + } + } +} +fragment UserFragment on User { + firstName + lastName +} diff --git a/test/integration/graphql/withFragmentImport.js b/test/integration/graphql/withFragmentImport.js new file mode 100644 index 00000000000..543cf1c00aa --- /dev/null +++ b/test/integration/graphql/withFragmentImport.js @@ -0,0 +1,5 @@ +var withFragmentImport = require('./withFragmentImport.graphql'); + +module.exports = function () { + return withFragmentImport; +};