Skip to content

Commit 9a2100e

Browse files
petetntakstuhl
authored andcommitted
Add loader for .graphql files (facebook#3909)
* Add graphql loader to webpack config Signed-off-by: petetnt <[email protected]> * Update README.md Signed-off-by: petetnt <[email protected]> * Update react-scripts README.md Signed-off-by: petetnt <[email protected]> * Add graphql jest transform Signed-off-by: petetnt <[email protected]> * Add integration tests, pin versions in package.json Signed-off-by: petetnt <[email protected]> * Tests expect regexp matchers Signed-off-by: petetnt <[email protected]> * Use strict equal test instead Signed-off-by: petetnt <[email protected]> * Escaping is hard Signed-off-by: petetnt <[email protected]> * Add comment for signifying a different file * Update docs * Fix jest config * Remove node_modules exclusion * Update README.md * Inline graphql jest transform Signed-off-by: petetnt <[email protected]> * Update copyright header Signed-off-by: petetnt <[email protected]> * Use .graphql extension only Signed-off-by: petetnt <[email protected]>
1 parent b5041a7 commit 9a2100e

File tree

11 files changed

+113
-1
lines changed

11 files changed

+113
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @remove-on-eject-begin
2+
/**
3+
* Copyright (c) 2018-present, Facebook, Inc.
4+
* Copyright (c) 2016 Remind
5+
*
6+
* This source code is licensed under the MIT license found in the
7+
* LICENSE file in the root directory of this source tree.
8+
*/
9+
// @remove-on-eject-end
10+
'use strict';
11+
12+
const loader = require('graphql-tag/loader');
13+
14+
module.exports = {
15+
process(src) {
16+
return loader.call({ cacheable() {} }, src);
17+
},
18+
};

packages/react-scripts/config/webpack.config.dev.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ module.exports = {
278278
},
279279
],
280280
},
281+
// The GraphQL loader preprocesses GraphQL queries in .graphql files.
282+
{
283+
test: /\.(graphql)$/,
284+
loader: 'graphql-tag/loader',
285+
},
281286
// "file" loader makes sure those assets get served by WebpackDevServer.
282287
// When you `import` an asset, you get its (virtual) filename.
283288
// In production, they would get copied to the `build` folder.

packages/react-scripts/config/webpack.config.prod.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ module.exports = {
320320
),
321321
// Note: this won't work without `new ExtractTextPlugin()` in `plugins`.
322322
},
323+
// The GraphQL loader preprocesses GraphQL queries in .graphql files.
324+
{
325+
test: /\.(graphql)$/,
326+
loader: 'graphql-tag/loader',
327+
},
323328
// "file" loader makes sure assets end up in the `build` folder.
324329
// When you `import` an asset, you get its filename.
325330
// This loader doesn't use a "test" so it will catch all modules

packages/react-scripts/fixtures/kitchensink/integration/webpack.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ describe('Integration', () => {
3131
);
3232
});
3333

34+
it('graphql files inclusion', async () => {
35+
const doc = await initDOM('graphql-inclusion');
36+
const children = doc.getElementById('graphql-inclusion').children;
37+
38+
// .graphql
39+
expect(children[0].textContent.replace(/\s/g, '')).to.equal(
40+
'{"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","variableDefinitions":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"test"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"test"},"value":{"kind":"StringValue","value":"test","block":false}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"test"},"arguments":[],"directives":[]}]}}]}}],"loc":{"start":0,"end":40,"source":{"body":"{\\ntest(test:\\"test\\"){\\ntest\\n}\\n}\\n","name":"GraphQLrequest","locationOffset":{"line":1,"column":1}}}}'
41+
);
42+
});
43+
3444
it('image inclusion', async () => {
3545
const doc = await initDOM('image-inclusion');
3646

packages/react-scripts/fixtures/kitchensink/src/App.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ class App extends Component {
111111
this.setFeature(f.default)
112112
);
113113
break;
114+
case 'graphql-inclusion':
115+
import('./features/webpack/GraphQLInclusion').then(f =>
116+
this.setFeature(f.default)
117+
);
118+
break;
114119
case 'image-inclusion':
115120
import('./features/webpack/ImageInclusion').then(f =>
116121
this.setFeature(f.default)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright (c) 2018-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import React from 'react';
9+
import A from './assets/graphql.graphql';
10+
11+
export default () => (
12+
<p id="graphql-inclusion">
13+
<span>{JSON.stringify(A)}</span>
14+
</p>
15+
);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) 2018-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import React from 'react';
9+
import ReactDOM from 'react-dom';
10+
import GraphQLInclusion from './GraphQLInclusion';
11+
12+
describe('graphql files inclusion', () => {
13+
it('renders without crashing', () => {
14+
const div = document.createElement('div');
15+
ReactDOM.render(<GraphQLInclusion />, div);
16+
});
17+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
test(test: "test") {
3+
test
4+
}
5+
}

packages/react-scripts/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
"find-pkg": "1.0.0",
4848
"fs-extra": "5.0.0",
4949
"globby": "7.1.1",
50+
"graphql": "0.12.3",
51+
"graphql-tag": "2.6.1",
5052
"html-webpack-plugin": "2.30.1",
5153
"identity-obj-proxy": "3.0.0",
5254
"jest": "22.1.2",

packages/react-scripts/scripts/utils/createJestConfig.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ module.exports = (resolve, rootDir, srcRoots) => {
3838
transform: {
3939
'^.+\\.(js|jsx|mjs)$': resolve('config/jest/babelTransform.js'),
4040
'^.+\\.css$': resolve('config/jest/cssTransform.js'),
41-
'^(?!.*\\.(js|jsx|mjs|css|json)$)': resolve(
41+
'^.+\\.(graphql)$': resolve('config/jest/graphqlTransform.js'),
42+
'^(?!.*\\.(js|jsx|mjs|css|json|graphql)$)': resolve(
4243
'config/jest/fileTransform.js'
4344
),
4445
},

0 commit comments

Comments
 (0)