Build SpaceX fan site using GraphQL with React and Apollo Client -2

Image for post
Image for post
Photo by SpaceX on Unsplash

Welcome to part-2 of the series. You can find part-1 here.

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.

Image for post
Image for post
create-react-app

Next, we will npm install concurrently as we don’t want to run two different commands to run server and client.

Image for post
Image for post
concurrently

Now, open your package.json and add below lines.

Image for post
Image for post
package.json

Next, head over to terminal and run npm run dev to run both client and server.

Image for post
Image for post
npm run dev

If everything is ok, react app will open on http://localhost:3000/

Image for post
Image for post
React app

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

Image for post
Image for post
delete logo.svg

Next, open App.js and remove everything and only keep the below code.

Image for post
Image for post
Reduced App.js

We will be using a free boostrap theme from the site https://bootswatch.com/

Image for post
Image for post
Bootswatch

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.

Image for post
Image for post
index.html

Next, i have also added a logo.png file for our logo in the src folder.

logo.png

Next, let’s display the logo in the top-middle of the page.

Image for post
Image for post
logo.png

It will show the beautiful logo in the web-page.

Image for post
Image for post
SpaceX logo

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.

Image for post
Image for post
apollo client

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.

Image for post
Image for post
App.js

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.

Image for post
Image for post
Launches.js

We also need to add this component to our App.js for it to render.

Image for post
Image for post
App.js

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.

Image for post
Image for post
CORS error

So, in our main folder let’s first install cors.

Image for post
Image for post
cors installed

Next, in server.js inclue and use cors as below.

Image for post
Image for post
server.js

Now, when we goto http://localhost:3000/ we can see all the data coming from graphql

Image for post
Image for post
All data

This completes the part-2 of the series. You can find the code for the same in my github repo here.

You can find part-3 here.

Written by

UI Lead- ReactJS, JavaScript and everything in-between.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store