Categories
JavaScript

Creating a React site with Vite and GraphQL

Earlier this year, when I attended the UC Berkeley web developer bootcamp, I learned about GraphQL. If you have been using React for a while, using GraphQL can be a game changer. There is an initial learning curve especially if you, like me are used to working with PHP and MySQL even though there are many similarities. But, once past the initial phase, it is very straight forward.

I created a few sites using React and GraphQL as part of my boot camp projects. I wanted to create a site completely from scratch with React and GraphQL using the Spoonacular API to get the recipes and save the data to the server.

There are probably a couple of different ways of doing this kind of websites. During the bootcamp, we learned about setting up multiple package.json files – one for the client, one for server and one for the overall project. This was super confusing to me. So, I decided to follow a few tutorials and get the site up and running with only one package.json where I can add all the dependencies and scripts. This was a bit challenging, but eventually I was able to get it working.

The first part was to setup the react site and the GraphQL server and have them both running at the same time on my localhost. I’ve used the create-react-app before to setup React sites which is fairly straight forward. I used Vite for the first time during the Bootcamp. Vite is a JavaScript build tool which is very fast and leverages the power of native ES modules. You can try Vite online here – StackBlitz. It runs a Vite based setup in the browser and you don’t need to install anything on your computer.

Creating the React site with Vite

It is simple and quick to setup a React site with Vite and GraphQL.

To create the React site with vite, I used the command below. I selected “React” as the framework and “JavaScript” as the variant. react-graphql-site is the name of the project.

Once the site is created, I can go into the newly created folder, and run npm install and npm run dev . I was able to view my new react site at http://localhost:5173/. The site looks like this:

Initial React site that was created using Vite.

Setting up the client and the server

Once the site setup was done, I created two new folders – server  and client. These folders are to separate the front end and back end code. After I created the folders, I followed the steps below to ensure the paths are correct and my scripts are working correctly.

  1. Moved the public and src folders into the client folder
  2. In index.html I changed the path for the vite logo and the main.jsx to point to the new path
  3. In client/src/App.jsx I changed the path of the viteLogo to point to the new path
  4. At this point I was still able to view the site by running npm run dev

Here is the folder structure for the client folder:

The next step was to install the required dependencies. I installed graphql, @apollo/client , @apollo/server , mongoose , bcrypt, concurrently

Once the dependencies were installed, I setup the server folder structure.

Here is the folder structure for the server folder.

Once the folder setup was done, I created the connection.js file in the config folder. Connection.js has the details for connecting to the database using Mongoose.

User Model with queries and mutations

I created a simple User model for my website in models/User.js.

Then I imported the User model in models/index.js. Since I am planning to have a recipes model to save and edit the recipes, it will be easier to import all of the models using the index.js file.

The next step was to create the type definitions and resolvers in the schema folder. I don’t need to write the resolvers at this point, but I do need the file.

schemas/typeDefs.js

schemas/resolvers.js

Exporting the typeDefs and resolvers in the schemas/index.js file

Apollo Server and GraphQl

The next step was to initialize the Apollo server in server/server.js. I specified the port I wanted to use for my GraphQL server. The front end was loading at port 5173. I specified 3001 as the port for the back end GraphQL server. So, when it is all setup, I should be able to see the server running on http://localhost:3001/graphql

After initializing the Apollo server, I added custom scripts in package.json. Here are the original scripts in the file:

I added the “start” and “watch” scripts.

The next step was to update vite.config.js to add the proxy to the graphql server.

At this point, running npm run watch , loads the front end site at http://localhost:5173/ and the graphql server at http://localhost:3001/graphql. The watch script runs both the scripts – npm run dev for the front end and npm run start for the backend. “Concurrently” lets me run both scripts at the same time. By setting it up this way, I can test my queries and mutations on the backend and the front end.

Here is the graphql server on my localhost showing both the queries and mutations.

Localhost graphql server showing the queries
Localhost graphql server showing the mutations

So, this was an easy and simple way to setup my React site with Vite and GraphQL.

Leave a Reply