Oops! Something went wrong while submitting the form.
We use cookies to improve your browsing experience on our website, to show you personalised content and to analize our website traffic. By browsing our website, you consent to our use of cookies. Read privacy policy.
GraphQL is becoming a popular way to use APIs in modern web and mobile apps.
However, learning new things is always time-consuming and without getting your hands dirty, it’s very difficult to understand the nuances of a new technology.
So, we have put together a powerful and concise tutorial that will guide you through setting up a GraphQL backend and integration into your React app in the shortest time possible. This tutorial is light on opinions, so that once you get a hang of the fundamentals, you can go on and tailor your workflow.
Key topics and takeaways:
Authentication
GraphQL API with AWS AppSync
Hosting
Working with multiple environments
Removing services
What will we be building?
We will build a basic real-time Restaurant CRUD app using authenticated GraphQL APIs. Click here to try the deployed version of the app to see what we’ll be building.
Will this tutorial teach React or GraphQL concepts as well?
No. The focus is to learn how to use AWS Amplify to build cloud-enabled, real-time web applications. If you are new to React or GraphQL, we recommend going through the official documentation and then coming back here.
If you’d like to see a video walkthrough of this process, click here
Here we'll walk you through the amplify configure setup. After you sign in to the AWS console, follow these steps:
Specify the AWS region: ap-south-1 (Mumbai) <Select the region based on your location. Click here for reference>
Specify the username of the new IAM user: amplify-app <name of="" your="" app=""></name>
In the AWS Console, click Next: Permissions, Next: Tags, Next: Review, and Create User to create the new IAM user. Then, return to the command line and press Enter.
Enter the credentials of the newly created user: accessKeyId: <your_access_key_id> </your_access_key_id> secretAccessKey: <your_secret_access_key></your_secret_access_key>
Profile Name: default
To view the newly created IAM user, go to the dashboard. Also, make sure that your region matches your selection.
Please choose the profile you want to use: default
Now, the AWS Amplify CLI has initialized a new project and you will see a new folder: amplify. This folder has files that hold your project configuration.
To access the AWS Cognito Console at any time, go to the dashboard. Also, ensure that your region is set correctly.
Now, our resources are created and we can start using them.
The first thing is to connect our React application to our new AWS Amplify project. To do this, reference the auto-generated aws-exports.js file that is now in our src folder.
To configure the app, open App.tsx and add the following code below the last import:
Now, we can start using our AWS services. To add the Authentication flow to the UI, export the app component by wrapping it with the authenticator HOC:
Now, let’s run the app to check if an Authentication flow has been added before our App component is rendered.
This flow gives users the ability to sign up and sign in. To view any users that were created, go back to the Cognito dashboard. Alternatively, you can also use:
The withAuthenticator HOC is a really easy way to get up and running with authentication, but in a real-world application, we probably want more control over how our form looks and functions. We can use the aws-amplify/Auth class to do this. This class has more than 30 methods including signUp, signIn, confirmSignUp, confirmSignIn, and forgotPassword. These functions return a promise, so they need to be handled asynchronously.
Do you want to generate code for your newly created GraphQL API: Yes
Choose the code generation language target: typescript
Enter the file name pattern of graphql queries, mutations and subscriptions: src/graphql/**/*.ts
Do you want to generate/update all possible GraphQL operations - queries, mutations and subscriptions: Yes
Enter maximum statement depth [increase from default if your schema is deeply nested]: 2
Enter the file name for the generated code: src/API.ts
Notice your GraphQL endpoint and API KEY. This step has created a new AWS AppSync API and generated the GraphQL queries, mutations, and subscriptions on your local. To check, see src/graphql or visit the AppSync dashboard. Alternatively, you can use:
Now that the GraphQL API is created, we can begin interacting with it from our client application. Here is how we’ll add queries, mutations, and subscriptions:
You can create multiple environments for your application to create & test out new features without affecting the main environment which you are working on.
When you use an existing environment to create a new environment, you get a copy of the entire backend application stack (CloudFormation) for the current environment. When you make changes in the new environment, you are then able to test these new changes in the new environment & merge only the changes that have been made since the new environment was created.
Let's take a look at how to create a new environment. In this new environment, we'll add another field for the restaurant owner to the GraphQL Schema.
First, we'll initialize a new environment using amplify init:
If you are unsure of what services you have enabled at any time, amplify status will give you the list of resources that are currently enabled in your app.
Sample code
The sample code for this blog post with an end to end working app is available here.
Summary
Once you've worked through all the sections above, your app should now have all the capabilities of a modern app, and building GraphQL + React apps should now be easier and faster with Amplify.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Build and Deploy a Real-Time React App Using AWS Amplify and GraphQL
GraphQL is becoming a popular way to use APIs in modern web and mobile apps.
However, learning new things is always time-consuming and without getting your hands dirty, it’s very difficult to understand the nuances of a new technology.
So, we have put together a powerful and concise tutorial that will guide you through setting up a GraphQL backend and integration into your React app in the shortest time possible. This tutorial is light on opinions, so that once you get a hang of the fundamentals, you can go on and tailor your workflow.
Key topics and takeaways:
Authentication
GraphQL API with AWS AppSync
Hosting
Working with multiple environments
Removing services
What will we be building?
We will build a basic real-time Restaurant CRUD app using authenticated GraphQL APIs. Click here to try the deployed version of the app to see what we’ll be building.
Will this tutorial teach React or GraphQL concepts as well?
No. The focus is to learn how to use AWS Amplify to build cloud-enabled, real-time web applications. If you are new to React or GraphQL, we recommend going through the official documentation and then coming back here.
If you’d like to see a video walkthrough of this process, click here
Here we'll walk you through the amplify configure setup. After you sign in to the AWS console, follow these steps:
Specify the AWS region: ap-south-1 (Mumbai) <Select the region based on your location. Click here for reference>
Specify the username of the new IAM user: amplify-app <name of="" your="" app=""></name>
In the AWS Console, click Next: Permissions, Next: Tags, Next: Review, and Create User to create the new IAM user. Then, return to the command line and press Enter.
Enter the credentials of the newly created user: accessKeyId: <your_access_key_id> </your_access_key_id> secretAccessKey: <your_secret_access_key></your_secret_access_key>
Profile Name: default
To view the newly created IAM user, go to the dashboard. Also, make sure that your region matches your selection.
Please choose the profile you want to use: default
Now, the AWS Amplify CLI has initialized a new project and you will see a new folder: amplify. This folder has files that hold your project configuration.
To access the AWS Cognito Console at any time, go to the dashboard. Also, ensure that your region is set correctly.
Now, our resources are created and we can start using them.
The first thing is to connect our React application to our new AWS Amplify project. To do this, reference the auto-generated aws-exports.js file that is now in our src folder.
To configure the app, open App.tsx and add the following code below the last import:
Now, we can start using our AWS services. To add the Authentication flow to the UI, export the app component by wrapping it with the authenticator HOC:
Now, let’s run the app to check if an Authentication flow has been added before our App component is rendered.
This flow gives users the ability to sign up and sign in. To view any users that were created, go back to the Cognito dashboard. Alternatively, you can also use:
The withAuthenticator HOC is a really easy way to get up and running with authentication, but in a real-world application, we probably want more control over how our form looks and functions. We can use the aws-amplify/Auth class to do this. This class has more than 30 methods including signUp, signIn, confirmSignUp, confirmSignIn, and forgotPassword. These functions return a promise, so they need to be handled asynchronously.
Do you want to generate code for your newly created GraphQL API: Yes
Choose the code generation language target: typescript
Enter the file name pattern of graphql queries, mutations and subscriptions: src/graphql/**/*.ts
Do you want to generate/update all possible GraphQL operations - queries, mutations and subscriptions: Yes
Enter maximum statement depth [increase from default if your schema is deeply nested]: 2
Enter the file name for the generated code: src/API.ts
Notice your GraphQL endpoint and API KEY. This step has created a new AWS AppSync API and generated the GraphQL queries, mutations, and subscriptions on your local. To check, see src/graphql or visit the AppSync dashboard. Alternatively, you can use:
Now that the GraphQL API is created, we can begin interacting with it from our client application. Here is how we’ll add queries, mutations, and subscriptions:
You can create multiple environments for your application to create & test out new features without affecting the main environment which you are working on.
When you use an existing environment to create a new environment, you get a copy of the entire backend application stack (CloudFormation) for the current environment. When you make changes in the new environment, you are then able to test these new changes in the new environment & merge only the changes that have been made since the new environment was created.
Let's take a look at how to create a new environment. In this new environment, we'll add another field for the restaurant owner to the GraphQL Schema.
First, we'll initialize a new environment using amplify init:
If you are unsure of what services you have enabled at any time, amplify status will give you the list of resources that are currently enabled in your app.
Sample code
The sample code for this blog post with an end to end working app is available here.
Summary
Once you've worked through all the sections above, your app should now have all the capabilities of a modern app, and building GraphQL + React apps should now be easier and faster with Amplify.
Velotio Technologies is an outsourced software product development partner for top technology startups and enterprises. We partner with companies to design, develop, and scale their products. Our work has been featured on TechCrunch, Product Hunt and more.
We have partnered with our customers to built 90+ transformational products in areas of edge computing, customer data platforms, exascale storage, cloud-native platforms, chatbots, clinical trials, healthcare and investment banking.
Since our founding in 2016, our team has completed more than 90 projects with 220+ employees across the following areas:
Building web/mobile applications
Architecting Cloud infrastructure and Data analytics platforms