-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Using create react app with Graphql #1328
Comments
It should be as simple as installing Create React App, then running If with "start graphql" you mean how to start its server, you just have to follow its instructions, as CRA only handles the boilerplate with webpack, babel etc for serving/building/testing the frontend part. 😉 |
Hi EnoahNetzach, How to connect CRA to Graphql? I think we need to use middleware to connect like apollo client. |
What do you mean by "connecting"? As long as GraphQL doesn't require you to add a webpack plugin (I might remember it wrong, but it doesn't), you should be able to just follow the instructions it provides. If what you mean is a way to run the GraphQL server, again, you have to follow the instructions on how to run graphql-server (or any other server). Create React App doesn't prevent you from adding other servers apart from the one it provides. |
Up to now, we are not able to load plain graphql files with the .graphql extension as such, as explained here, as this requires a 'graphql-tag/loader' entry in the webpack configuration: There might be a work around by placing those files into .js files and defining the graphql tags inline as you would do it with gql`query something`, but so the editors (or at least my sublime) doesn't recognize the graphql syntax highlighting within the grapqhl tag string. It'll be nice feature, if a create_react_app can load .graphql files by default. I've tried to do it like follows, .... cd node_modules/react_scripts
npm install --save graphql-tag
vi config/webpack.config.dev.js Find the default url loader exclusions... {
exclude: [
/\.html$/,
/\.(js|jsx)$/,
/\.css$/,
/\.json$/,
/\.svg$/
], Add graphql and gql as exclusion to the default url loader ... exclude: [
/\.html$/,
/\.(js|jsx)$/,
/\.css$/,
/\.json$/,
/\.svg$/,
/\.graphql$/,
/\.gql$/,
], Now adding the graphql-tag/loader for .graphql and .gql files: // "file" loader for svg
{
test: /\.svg$/,
loader: 'file',
query: {
name: 'static/media/[name].[hash:8].[ext]'
}
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: 'graphql-tag/loader'
} ...but the graphql-tag/loader doesn't resolve and lead to an Error in my App: Module not found "graphql-tag/loader" Has anyone an idea how to resolve this? |
Ok, resolved.. if I do this instead yarn eject
yarn add graphql-tag
vi config/webpack.config.dev.js
vi config/webpack.config.prod.js in the config files default loader I've added the following exclusions: exclude: [
/\.html$/,
/\.(js|jsx)$/,
/\.css$/,
/\.json$/,
/\.svg$/,
/\.graphql$/,
/\.gql$/
], ..in the loader if added the graphql-tag/loader: {
test: /\.(graphql|gql)$/,
include: paths.appSrc,
exclude: /node_modules/,
loader: 'graphql-tag/loader'
} So I can import my graphql files now by.... import gql_query from './_query.graphql';
...
const ContainerWithMutations = compose(
graphql(gql_query, {
options: ({ selected }) => ({
variables: { processId: selected},
})
})
)(Container); |
Closing since we don't currently support any special integration with GraphQL without ejecting. |
@anhdn there is an alternative version of relay-scripts with embedded Relay support, which does not require to eject: https://github.com/Valentin-Seehausen/create-react-app |
i am not sure about this but i got CRA to work with apollo without ejecting. is it not supposed to work? i dont think to have done anything special. server on :4000 client on :3000 |
@cezarneaga Can you please share your implementation in a fork? Or, at least a Readme in a gist? |
it's a bit more, as i have auth there too and i am currently refactoring it but: https://github.com/cezarneaga/apollo-cra-authenticated |
Just a heads up: you can use react-app-rewired to configure graphql-tag to load .graphql files without ejecting from CRA. (https://github.com/timarney/react-app-rewired) I created a package for use with react-app-rewire to make configuration easy:
|
CRA does not expose a way to extend webpack config. But here is a hack way to do it. The basic idea is to change the webpack config file in
So I wrote a simple
app/scripts/start.js
config/webpack.config.dev.js
And it is similiar to extend With this hack way, the all of packages, config and scripts ejected go away, and easy to upgrade along with And in this way, you can extend any webpack config as you want. |
@shendepu thank you so much for this workaround. I think this is the way to go for tweaking the settings of CRA ATM, not just for the sake of using GraphQL. |
Yet another way is to use Here is the result of my patch against
|
Hello,
I'll start new project with new react app, new api. And I love using create-react-app and Graphql. So how can i start with both create-react-app and graphql on my project?
Thanks!
The text was updated successfully, but these errors were encountered: