Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loader for .graphql files #3909

Merged
merged 17 commits into from
Feb 3, 2018
Merged
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Your environment will have everything you need to build a modern single-page Rea
* Autoprefixed CSS, so you don’t need `-webkit-` or other prefixes.
* A fast interactive unit test runner with built-in support for coverage reporting.
* A live development server that warns about common mistakes.
* A build script to bundle JS, CSS, and images for production, with hashes and sourcemaps.
* A build script to bundle JS, CSS, GraphQL files and images for production, with hashes and sourcemaps.
* An offline-first [service worker](https://developers.google.com/web/fundamentals/getting-started/primers/service-workers) and a [web app manifest](https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/), meeting all the [Progressive Web App](https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#making-a-progressive-web-app) criteria.
* Hassle-free updates for the above tools with a single dependency.

Expand Down
13 changes: 13 additions & 0 deletions packages/react-scripts/config/jest/graphqlTransform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @remove-on-eject-begin
/**
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// @remove-on-eject-end
'use strict';

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add Copyright (c) 2016 Remind to the license header of this file, per
https://unpkg.com/[email protected]/LICENSE

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does 39d39bc look correct? Not 100% sure how derivative works should be marked.

const graphqlTransform = require('jest-transform-graphql');

module.exports = graphqlTransform;
7 changes: 7 additions & 0 deletions packages/react-scripts/config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@ module.exports = {
},
],
},
// The graphql loader saves GraphQL ASTs processing time on client-side
// and enables queries to be separated from script over .graphql and .gql files.
{
test: /\.(graphql|gql)$/,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer us to be opinionated here and pick .graphql as the extension. It's not obvious what .gql is, it has to be repeated everywhere, syntax highlighters have to support both, etc.

Copy link
Contributor Author

@petetnt petetnt Feb 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I agree there as both extensions have been adopted by the userland at large from my observations. For example the Apollo references both even while usually using .graphql and VSCode syntax highlighter uses gql in its examples.

I do agree that .graphql is a better extension though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see from graphql/graphql-spec#203 that .graphql is used by FB internally so I guess being opinionated is alright there. It will be trivial to add the other extension if needed later

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in d239f56

exclude: /node_modules/,
loader: 'graphql-tag/loader',
},
// "file" loader makes sure those assets get served by WebpackDevServer.
// When you `import` an asset, you get its (virtual) filename.
// In production, they would get copied to the `build` folder.
Expand Down
7 changes: 7 additions & 0 deletions packages/react-scripts/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,13 @@ module.exports = {
},
],
},
// The graphql loader saves GraphQL ASTs processing time on client-side
// and enables queries to be separated from script over .graphql and .gql files.
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: 'graphql-tag/loader',
},
// "file" loader makes sure assets end up in the `build` folder.
// When you `import` an asset, you get its filename.
// This loader doesn't use a "test" so it will catch all modules
Expand Down
3 changes: 3 additions & 0 deletions packages/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@
"extract-text-webpack-plugin": "3.0.2",
"file-loader": "1.1.6",
"fs-extra": "5.0.0",
"graphql": "^0.12.3",
"graphql-tag": "^2.6.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to pin these new dependencies

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pinned in d1dd2a8

"html-webpack-plugin": "2.30.1",
"identity-obj-proxy": "3.0.0",
"jest": "22.1.2",
"jest-transform-graphql": "^2.1.0",
"object-assign": "4.1.1",
"postcss-flexbugs-fixes": "3.2.0",
"postcss-loader": "2.0.10",
Expand Down
5 changes: 4 additions & 1 deletion packages/react-scripts/scripts/utils/createJestConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ module.exports = (resolve, rootDir, isEjecting) => {
? '<rootDir>/node_modules/babel-jest'
: resolve('config/jest/babelTransform.js'),
'^.+\\.css$': resolve('config/jest/cssTransform.js'),
'^(?!.*\\.(js|jsx|mjs|css|json)$)': resolve(
'^.+\\.(gql|graphql)$': isEjecting
? '<rootDir>/node_modules/jest-transform-graphql'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this line just be 'jest-transform-graphql'?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about that. This is just copying the pattern used above for babel-jest.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Okay. Let's keep it for now.

: resolve('config/jest/graphqlTransform.js'),
'^(?!.*\\.(js|jsx|mjs|css|json|graphql|gql)$)': resolve(
'config/jest/fileTransform.js'
),
},
Expand Down
25 changes: 25 additions & 0 deletions packages/react-scripts/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ You can find the most recent version of this guide [here](https://github.com/fac
- [Post-Processing CSS](#post-processing-css)
- [Adding a CSS Preprocessor (Sass, Less etc.)](#adding-a-css-preprocessor-sass-less-etc)
- [Adding Images, Fonts, and Files](#adding-images-fonts-and-files)
- [Adding GraphQL files](#adding-graphql-files)
- [Using the `public` Folder](#using-the-public-folder)
- [Changing the HTML](#changing-the-html)
- [Adding Assets Outside of the Module System](#adding-assets-outside-of-the-module-system)
Expand Down Expand Up @@ -687,6 +688,30 @@ Now running `npm start` and `npm run build` also builds Sass files.

`node-sass-chokidar` is used here as it addresses these issues.

## Adding GraphQL files
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section should be after "Adding Images, ..."


If you are using GraphQL, you can **import a your GraphQL queries directly in a JavaScript module**.

By preprocessing the GraphQL queries by importing them instead of using (for example) a [template tag](https://github.com/apollographql/graphql-tag), you save GraphQL ASTs processing time on client-side and enable queries to be separated from script over `.graphql` or `.gql` files.

Here is an example:

```js
// query.graphql
{
githubStats(repository: "facebook/react") {
stars
}
}

import query from './query.graphql';

console.log(query);
// {
// "kind": "Document",
// ...
```

## Adding Images, Fonts, and Files

With Webpack, using static assets like images and fonts works similarly to CSS.
Expand Down