diff --git a/package.json b/package.json index cc2f6a4a59e..ce599fc3cea 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,8 @@ "coffeescript": "^2.0.3", "cross-env": "^5.1.1", "eslint": "^4.13.0", + "graphql": "^0.11.7", + "graphql-tag": "^2.6.0", "husky": "^0.14.3", "less": "^2.7.2", "lint-staged": "^6.0.0", diff --git a/src/Parser.js b/src/Parser.js index 92365e2299f..9f28518503e 100644 --- a/src/Parser.js +++ b/src/Parser.js @@ -20,6 +20,8 @@ class Parser { this.registerExtension('json', './assets/JSONAsset'); this.registerExtension('yaml', './assets/YAMLAsset'); this.registerExtension('yml', './assets/YAMLAsset'); + this.registerExtension('gql', './assets/GraphqlAsset'); + this.registerExtension('graphql', './assets/GraphqlAsset'); this.registerExtension('css', './assets/CSSAsset'); this.registerExtension('pcss', './assets/CSSAsset'); diff --git a/src/assets/GraphqlAsset.js b/src/assets/GraphqlAsset.js new file mode 100644 index 00000000000..f38ae980ea9 --- /dev/null +++ b/src/assets/GraphqlAsset.js @@ -0,0 +1,22 @@ +const Asset = require('../Asset'); +const localRequire = require('../utils/localRequire'); + +class GraphqlAsset extends Asset { + constructor(name, pkg, options) { + super(name, pkg, options); + this.type = 'js'; + } + + async parse(code) { + let gql = await localRequire('graphql-tag', this.name); + return gql(code); + } + + generate() { + return { + js: `module.exports=${JSON.stringify(this.ast, false, 2)};` + }; + } +} + +module.exports = GraphqlAsset; diff --git a/test/graphql.js b/test/graphql.js new file mode 100644 index 00000000000..007ee80f0be --- /dev/null +++ b/test/graphql.js @@ -0,0 +1,33 @@ +const assert = require('assert'); +const gql = require('graphql-tag'); +const {bundle, run, assertBundleTree} = require('./utils'); + +describe('graphql', function() { + it('should support requiring graphql files', async function() { + let b = await bundle(__dirname + '/integration/graphql/index.js'); + + assertBundleTree(b, { + name: 'index.js', + assets: ['index.js', 'local.graphql'], + childBundles: [] + }); + + let output = run(b); + assert.equal(typeof output, 'function'); + assert.deepEqual( + output().definitions, + gql` + { + user(id: 5) { + ...UserFragment + } + } + + fragment UserFragment on User { + firstName + lastName + } + `.definitions + ); + }); +}); diff --git a/test/integration/graphql/index.js b/test/integration/graphql/index.js new file mode 100644 index 00000000000..7f14ed613bd --- /dev/null +++ b/test/integration/graphql/index.js @@ -0,0 +1,5 @@ +var local = require('./local.graphql'); + +module.exports = function () { + return local; +}; diff --git a/test/integration/graphql/local.graphql b/test/integration/graphql/local.graphql new file mode 100644 index 00000000000..b47420001e9 --- /dev/null +++ b/test/integration/graphql/local.graphql @@ -0,0 +1,9 @@ +{ + user(id: 5) { + ...UserFragment + } +} +fragment UserFragment on User { + firstName + lastName +} diff --git a/yarn.lock b/yarn.lock index c55bb245edc..a4e7b78ba3c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2188,6 +2188,16 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.4: version "1.0.1" resolved "https://registry.yarnpkg.com/graceful-readlink/-/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725" +graphql-tag@^2.6.0: + version "2.6.0" + resolved "https://registry.yarnpkg.com/graphql-tag/-/graphql-tag-2.6.0.tgz#0fb1b9f6d6651263c47a3420e827910e6fed3952" + +graphql@^0.11.7: + version "0.11.7" + resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.11.7.tgz#e5abaa9cb7b7cccb84e9f0836bf4370d268750c6" + dependencies: + iterall "1.1.3" + growl@1.9.2: version "1.9.2" resolved "https://registry.yarnpkg.com/growl/-/growl-1.9.2.tgz#0ea7743715db8d8de2c5ede1775e1b45ac85c02f" @@ -2784,6 +2794,10 @@ istanbul-reports@^1.1.3: dependencies: handlebars "^4.0.3" +iterall@1.1.3: + version "1.1.3" + resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.1.3.tgz#1cbbff96204056dde6656e2ed2e2226d0e6d72c9" + jest-get-type@^21.2.0: version "21.2.0" resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-21.2.0.tgz#f6376ab9db4b60d81e39f30749c6c466f40d4a23"