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.
This is part 2 of Building a To-Do List App.
Create React app
Let’s start by creating a React app using thecreate-react-app
command.
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.src/App.js
with the below content to include the Apollo
client setup.
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 filesrc/GraphQLData.js
and add the following.
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”.
src/App.js
file let’s update our src/index.js
file with the following code.
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.
src/GraphQLData.js
file
with the remaining queries.