Welcome to part-2 of the series.
This series is based on the youtube series by Brad Traversy and you can find it here.
It’s time to create the front-end of our app and we will use create-react-app for the same. So, head over to terminal and give below command inside the ReactApolloClient folder.
Next, we will npm install concurrently as we don’t want to run two different commands to run server and client.
Now, open your package.json and add below lines.
Next, head over to terminal and run npm run dev to run both client and server.
npm run dev
If everything is ok, react app will open on http://localhost:3000/
Now, we will work on our React app. But time for some clean-up first. Open App.css inside client -> src folder and delete everything. Also, delete the file logo.svg
Next, open App.js and remove everything and only keep the below code.
We will be using a free boostrap theme from the site https://bootswatch.com/
I will be using Cyborg theme, so i got the hosted link to it. Next go to index.html inside the public folder and add reference to the link. Also change the title in the file.
Next, i have also added a logo.png file for our logo in the src folder.
Next, let’s display the logo in the top-middle of the page.
It will show the beautiful logo in the web-page.
Then we will install some dependencies for using apollo client in our project to fetch data. Do the below npm installs in the client folder.
Now, it’s time to use Apollo client in our App.js. We first do our imports and then use a client variable, where we point to our graphql endpoint. After that we wrap our whole App with ApolloProvider.
Then we will create a folder components and inside it a file Launches.js. Here we are using gql and then using the query we created in Graphiql. Next, we have to use Query inside our render() to get the data.
We also need to add this component to our App.js for it to render.
But, when we try to see the data in our console, we get the famous CORS error. This occurs because our client is at 3000 and server at 5000.
So, in our main folder let’s first install cors.
Next, in server.js inclue and use cors as below.
Now, when we goto http://localhost:3000/ we can see all the data coming from graphql
This completes the part-2 of the series. You can find the code for the same in my github repo here.