Creating a Basic UI
Create a simple to-do app and integrate it with Auth0. This step in the GraphQL tutorial walks you through creating a basic UI with React.
We’re overhauling Dgraph’s docs to make them clearer and more approachable. If you notice any issues during this transition or have suggestions, please let us know.
In this step, we’re creating a simple to do app (in React) and integrating it with Auth0.
Create React app
Let’s start by creating a React app using the create-react-app
command.
To verify navigate to the folder, start the dev server, and visit http://localhost:3000.
Install dependencies
Now, let’s install the various dependencies that we will need in the app.
Setup Apollo Client
Let’s start with installing the Apollo dependencies and then create a setup.
Now, let’s update our src/App.js
with the below content to include the Apollo
client setup.
Here we have created a simple instance of the Apollo client and passed the URL
of our GraphQL API. Then we have passed the client to ApolloProvider
and
wrapped our App
so that its accessible throughout the app.
Queries and Mutations
Now, let’s add some queries and mutations.
First, let’s see how we can add a todo and get todos. Create a file
src/GraphQLData.js
and add the following.
Now, let’s see how to use this to add a todo. Let’s import the dependencies
first in src/App.js
replacing all the code. Let’s now create the functions to
add a todo and get todos.
Auth0 Integration
Now, let’s integrate Auth0 in our app and use that to add the logged-in user. Let’s first create an app in Auth0.
- Head over to Auth0 and create an account. Click ‘sign up’ here
- Once the signup is done, click “Create Application” in “Integrate Auth0 into your app”.
- Give your app a name and select “Single Page Web App” app type
- Select React as the technology
- No need to do the sample app, scroll down to “Configure Auth0” and select “Application Settings”.
- Select your app and add the values of
domain
andclientid
in the filesrc/auth_template.json
. Check this link for more information. - Add
http://localhost:3000
to “Allowed Callback URLs”, “Allowed Web Origins” and “Allowed Logout URLs”.
Now that we have prepared our src/App.js
file let’s update our src/index.js
file with the following code.
Note that for the app to work from this point on, the src/auth_template.json
file must be configured with your auth0 credentials.
Here is a reference Here
Let’s also add definitions for updating, deleting and clearing all tasks to
src/GraphQLData.js
. Let’s also add the constants user
, isAuthenticated
,
loginWithRedirect
and logout
which they receive from the variable
useAuth0
. We also create a constant called logInOut
that contains the logic
to know if the user is logged in or not. This variable will show a button to
login or logout depending on the status of logged in or logged out. Note that
before calling the component Todos we call our variable {logInOut}
so that our
login button appears above the app.
For our app to work correctly we need to update the src/GraphQLData.js
file
with the remaining queries.
Here is the complete code Here
Let’s now start the app.
Now you should have an app running!
Was this page helpful?