GraphQL gained popularity in terms of front-end development due to the various advantages it offers over REST APIs. However, setting up your own GraphQL server is both error-prone and complicated. Due to this, managed services such as Prisma have been made to manage your GraphQL server, allowing you to focus on the development of your app.
In this tutorial, we will be building a fully functional recipe app using React and Prisma to manage GraphQL.
Install the Prisma CLI client globally by running the following command:
We will be using create-react-app to bootstrap our React app, so run the following command to install it globally:
To use Prisma locally, you need to have Docker installed on your machine. If you don’t have Docker yet, you can download the Docker Community Edition.
To use the Prisma CLI, you will need to have a Prisma account. You can create an account at the Prisma website, then login to the Prisma CLI by running the following command:
Now that we have all the required dependencies, create a folder for the project and navigate into the folder by running the following commands:
Then initialize your Prisma server in the folder:
A prompt will appear with a few options on which method you’ll want to use to set up your prisma server. We will be working with the server locally for now and then deploy it later. Choose Create new database
to have Prisma create a database locally with Docker.
Next, you’ll get a prompt to choose a database. For this tutorial we will be using Postgres ,so choose PostgreSQL
:
Next we have to choose a programming language for our generated prisma client. Choose Prisma Javascript Client
:
You will have the following files generated by Prisma based on the selected options:
Now that we have our Prisma server set up, make sure docker is running. Then, run the following command to start the server:
Docker compose is used to run multiple containers as a single service. The previous command will start our Prisma server and the Postgres database. Head over to 127.0.0.1:4466
in your browser to view the Prisma playground.
If you want to stop your server, run docker-compose stop
.
Next, open your datamodel.prisma
file and replace the demo content with the following:
Then run the following command to deploy to a demo server:
You will get a response showing the created models and your Prisma endpoint as follows:
To view the deployed server, open you Prisma dashboard at https://app.prisma.io/
and navigate to services. You will have the following showing in your dashboard:
To deploy to your local server, open the prisma.yml
file and change the endpoint to http://localhost:4466
, then run prisma deploy
Now that our Prisma server is ready, we can set up our React app to consume thePrisma GraphQL endpoint.
In the project folder, run the following command to bootstrap our client app using create-react-app
:
To work with GraphQL, we require a few dependencies. Navigate into the client folder and run the following commands to install them:
For the UI, we will be using Ant Design:
Our app folder structure will be as follows:
Here we will do the apollo config. This will be the main entry file for our app:
Query to fetch all recipes:
Query to fetch a recipe by the recipe id:
The mutation for creating a new recipe:
The mutation for updating a recipe:
This is where our logic for the CRUD
operations is based. The file is quite large, so we’ve included only the crucial parts. You can view the rest of the code on GitHub.
In order to use our queries and mutations, we need to import them and then use the react-apollo's
graphql that allows us to create a higher-order component
that can execute queries and update reactively based on the data we have in our app.
Here is an example of how we can fetch and display all published recipes:
The resulting view would look as follows:
Note: Styling for the components will not be included due to file size. The code is available in the GitHub repo.
Since we require more than one enhancer in our component, we will use compose to incorporate all needed enhancers for the component:
We also require the withApollo
enhancer, which provides direct access to your ApolloClient
instance. This will be useful, since we need to carry out one-off queries for fetching data for a recipe.
After capturing the data from the following form:
We then execute the following handleSubmit
callback, which runs the addNewRecipeMutation
mutation:
In order to edit a recipe, we re-use the form used to create a new recipe and then pass the recipe data. When a user clicks on the edit icon, the form pops up with the data pre-filled as follows:
We then run a different handleSubmit
handler to run the update mutation as follows:
As for the delete functionality, we will be doing a soft-delete
on the deleted recipe, which means we will be changing the published
attribute to false since when fetching the articles we filter to make sure we only get published
articles.
We will use the same function as before and pass in published as false, as shown in the following example:
In this tutorial, you built a recipe app with React and GraphQL, using Prisma to manage your GraphQL server. Prisma is a reliable service that allows you to focus on implementing you business logic.
You can access the code at GitHub.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!