DEV Community

Cover image for Build SpaceX fan site using GraphQL with React and Apollo Client -2
Nabendu
Nabendu

Posted on

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

Alt Text

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.

create-react-appcreate-react-app

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

concurrentlyconcurrently

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

package.jsonpackage.json

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

npm run devnpm run dev

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

React appReact 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

delete logo.svgdelete logo.svg

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

Reduced App.jsReduced App.js

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

BootswatchBootswatch

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.

index.htmlindex.html

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

logo.pnglogo.png

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

logo.pnglogo.png

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

SpaceX logoSpaceX 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.

apollo clientapollo 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.

App.jsApp.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.

Launches.jsLaunches.js

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

App.jsApp.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.

CORS errorCORS error

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

cors installedcors installed

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

server.jsserver.js

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

All dataAll data

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

Top comments (0)