Uh oh!
There was an error while loading. Please reload this page.
Add loader for .graphql files - #3909
Conversation
Signed-off-by: petetnt <pete.a.nykanen@gmail.com>
Signed-off-by: petetnt <pete.a.nykanen@gmail.com>
Signed-off-by: petetnt <pete.a.nykanen@gmail.com>
Signed-off-by: petetnt <pete.a.nykanen@gmail.com>
iansu
commented
Jan 23, 2018
Nice! On first glance this looks good to me. Can you add an e2e test (in |
petetnt
commented
Jan 23, 2018
Yep! Working on the tests right now! |
| "file-loader": "1.1.6", | ||
| "fs-extra": "5.0.0", | ||
| "graphql": "^0.12.3", | ||
| "graphql-tag": "^2.6.1", |
There was a problem hiding this comment.
we need to pin these new dependencies
Signed-off-by: petetnt <pete.a.nykanen@gmail.com>
petetnt
commented
Jan 23, 2018
Added tests, running local tests with docker fail on the |
iansu
commented
Jan 23, 2018
I don't think the local e2e tests in docker are working properly right now. Let's just see what happens on CI. |
Signed-off-by: petetnt <pete.a.nykanen@gmail.com>
Signed-off-by: petetnt <pete.a.nykanen@gmail.com>
Signed-off-by: petetnt <pete.a.nykanen@gmail.com>
petetnt
commented
Jan 24, 2018
Tests now pass with 435ece0 on Travis, there's something weird going on at AppVeyor at large. |
| '^.+\\.css$': resolve('config/jest/cssTransform.js'), | ||
| '^(?!.*\\.(js|jsx|mjs|css|json)$)': resolve( | ||
| '^.+\\.(gql|graphql)$': isEjecting | ||
| ? '<rootDir>/node_modules/jest-transform-graphql' |
There was a problem hiding this comment.
Can't this line just be 'jest-transform-graphql'?
There was a problem hiding this comment.
I'm not sure about that. This is just copying the pattern used above for babel-jest.
There was a problem hiding this comment.
Hmm. Okay. Let's keep it for now.
| `node-sass-chokidar` is used here as it addresses these issues. | ||
| ## Adding GraphQL files |
There was a problem hiding this comment.
This section should be after "Adding Images, ..."
petetnt
commented
Feb 3, 2018
Thanks @iansu! 👌 |
| "html-webpack-plugin": "2.30.1", | ||
| "identity-obj-proxy": "3.0.0", | ||
| "jest": "22.1.2", | ||
| "jest-transform-graphql": "2.1.0", |
There was a problem hiding this comment.
It's kind of frustrating that everybody will "inherit" whole three GQL dependencies after ejecting even if they don't use graphql.
There was a problem hiding this comment.
I do agree, but not sure what the other option would be.
There was a problem hiding this comment.
Let's remove this dependency. All it does is literally one-line call to webpack loader, and we could do the same in our Jest transform file.
| An alternative way of handling static assets is described in the next section. | ||
| ## Adding GraphQL files | ||
There was a problem hiding this comment.
This needs to have a note about it being supported only in 2.0.0+, like other similar features.
Signed-off-by: petetnt <pete.a.nykanen@gmail.com>
paulomcnally
commented
Mar 5, 2018
Is this available in the version 1.5.2? |
petetnt
commented
Mar 6, 2018
@paulomcnally This feature will be available on the upcoming version 2 of CRA. If you want to load |
paulomcnally
commented
Mar 6, 2018
Thanks for your answer @petetnt, I'll wait for version 2.x. |
* Add graphql loader to webpack config Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * Update README.md Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * Update react-scripts README.md Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * Add graphql jest transform Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * Add integration tests, pin versions in package.json Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * Tests expect regexp matchers Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * Use strict equal test instead Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * Escaping is hard Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * 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 <pete.a.nykanen@gmail.com> * Update copyright header Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * Use .graphql extension only Signed-off-by: petetnt <pete.a.nykanen@gmail.com>
* Add graphql loader to webpack config Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * Update README.md Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * Update react-scripts README.md Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * Add graphql jest transform Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * Add integration tests, pin versions in package.json Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * Tests expect regexp matchers Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * Use strict equal test instead Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * Escaping is hard Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * 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 <pete.a.nykanen@gmail.com> * Update copyright header Signed-off-by: petetnt <pete.a.nykanen@gmail.com> * Use .graphql extension only Signed-off-by: petetnt <pete.a.nykanen@gmail.com>
I cannot seem to load .graphql files using CRA version 2.0.3. Here are my packages in package.json Here is how I import it:
The error I am getting is Thanks |
psamd
commented
Oct 17, 2018
@silentlight the support for 'graphql-tag/loader' was removed #5076 Try using any one of these macros: Or just eject your app and add the 'graphql-tag/loader' in your webpack config files (dev & prod) |
chrisdostert
commented
Oct 18, 2018
why was this reverted? this functionality was awesome |
petetnt
commented
Oct 18, 2018
Hi @chrisdostert! The implicit loader was removed in favor of the macro approach, which gives much more flexibility (for example using different You can load The loader statement gets replaced with the actual |
chrisdostert
commented
Oct 18, 2018
thx @petetnt for response. |
iansu
commented
Oct 18, 2018
As @petetnt mentioned, the main downside to this approach is that you're stuck with whatever version of While using macros requires a bit of extra work we think the flexibility is worth the tradeoff. It also means we don't have to add loaders for every conceivable file type or for different tools (CSS-in-JS libraries for example). |
Feels like someone should put an alert here - the macro version hits a bug in babel so it never reloads your graphql files :( |
masull
commented
Dec 28, 2018
graphql.macro does not seem to work with a .graphql file that contains multiple operations. ./queries.graphql import {loader} from 'graphql.macro'; Query1 and Query2 are undefined. |
Important notice
The implicit loader was removed in favor of the macro approach, which gives much more flexibility (for example using different graphql-js version instead of the one shipped with CRA) with little to no code changes.
You can load .gql and .graphql files by installing the graphql-tag.macro from npm and using it like this:
The loader statement gets replaced with the actual graphql-tag object on build time and the precomputed object also lowers performance overhead by bit, so it behaves like the loader in this PR did!
OLD PR
This PR adds the
.graphqlfile loading feature discussed in #3873.A POC of the concept can be found in https://github.com/petetnt/cra-pr-3909-poc/ (might need to link to
react-scriptsafter installing). Tested the following things:yarn startyarn buildAlso looked at the output after ejecting and it looks correct, couldn't test it yet though after
yarn ejectdue to some version issues 🤷The PR adds following things:
.graphqland.gqlfiles withgraphql-tag/loaderREADME.mdandUser guidewith descriptions and usagejesttransform so the.graphqlfiles work with tests tooTested it out with
react-apolloand it worked beautifully. Should work withRelay Moderntoo, but prerequisite for it is #2343