Skip to content

Commit

Permalink
Add graphql support (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
tb authored and devongovett committed Dec 28, 2017
1 parent 463c570 commit 8f1f497
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
22 changes: 22 additions & 0 deletions src/assets/GraphqlAsset.js
Original file line number Diff line number Diff line change
@@ -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;
33 changes: 33 additions & 0 deletions test/graphql.js
Original file line number Diff line number Diff line change
@@ -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
);
});
});
5 changes: 5 additions & 0 deletions test/integration/graphql/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var local = require('./local.graphql');

module.exports = function () {
return local;
};
9 changes: 9 additions & 0 deletions test/integration/graphql/local.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
user(id: 5) {
...UserFragment
}
}
fragment UserFragment on User {
firstName
lastName
}
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 8f1f497

Please sign in to comment.