Frontend Serverless with React and GraphQL
Start at the beginning and work your way through this project. The code for each step as well as the finished project can be found in the Github repository.
10. Add GraphCMS backend
Objective: Set up our GraphCMS backend and add the environmental variables to the Next.js app.
First, we need to create an account with GraphCMS. Head over to https://www.graphcms.com
and do the following steps:
- Sign up for a new GraphCMS account. You will need to enter your email, password, and name.
- You will need to verify your email. They will send you an email and you can open it and click on the verify button.
- Now that you are verified, you should log in on the GraphCMS website.
- Click on the (+) Create a new project button.
- Give a name and description, we will use Next Chop but you can use whatever you want.
- Select the region closest to you and click "Create Project".
- You can select the free plan to start and then you should have a blank project.
Now that we have a blank project, we need to create 2 new models- the first is Recipe
and the second is UserLike
. Let's set up the Recipe
model first.
- Click on the Schema button on the side and then the "Create Model" button. Give it a name of
Recipe
. - Drag the
single line text
field to the schema and drop it. Name ittitle
and leave the Api Id alone. - Create a
single line text
field with the namedescription
. - Create a
markdown
field with the namecontent
. - Create a
asset picker
field with the nameimage
. - Create a
json
field with the nameingredients
. - Create a
single line text
field with the nameowner
.
Now create a UserLike
model and create the following fields:
- Create a
single line text
field with the nameuser
. - Click back on the
Recipe
page. Create a new reference field calleduserLikes
. - Select
UserLike
as the model that can be referenced. Check on the slider that saysallow multiple values
. Click theconfigure the reverse field
button. - On the new reference field dialog box, set the field name to be
recipe
rather thanRecipe
with a capital letter. - Press the
create
button.
Still with me? Okay great, so what we've done is create an account with GraphCMS and then established the two models that represent our schema. GraphCMS will do the rest and set up an entire backend for us to use.
Now we need to set the Public API permissions so we can actually access our endpoint from our app.
- Click on the settings button and then API Access.
- Click on the checkbox under queries
Content from stage Draft
. - Click on the
Draft
button under "Default content gets delivered from stage". - Check the
Mutations
checkbox and then click the save button.
Now that we've set up our GraphCMS, lets turn back to our project. First add .env
as a line to the .gitignore
file. Next add dotenv to our project:
bash
npm install --save dotenv
We need to make sure that we never commit our .env
file. Add .env to the .gitignore
file:
.gitignore
.nextnode_modules.DS_Store.env
Next we have to set up our next.config.js
file with the environmental variables that we will be using.
next.config.js
require('dotenv').config();const { BRANCH, GRAPHCMSURL, GRAPHCMSPROJECTID } = process.env;module.exports = {publicRuntimeConfig: {graphcms: {BRANCH,GRAPHCMSURL,GRAPHCMSPROJECTID,},},};
These environmental variables get set in .env
file:
.env
BRANCH=masterGRAPHCMSURL=YOUR_GRAPHCMS_URLGRAPHCMSPROJECTID=YOUR_PROJECT_ID
You can get the BRANCH
, GRAPHCMSURL
, GRAPHCMSPROJECTID
variables from the GraphCMS settings page that wwe were just on. You can copy the Endpoints
url. The format is GRAPHCMSURL/GRAPHCMSPROJECTID/BRANCH
. After setting your .env
variables and saving the next.config.js
, stop the server by pressing ctrl + c and then starting the server back up.