Have an existing API within AWS AppSync and need to connect it from a React web application? If you are not completely new to GraphQL you should know that the most popular GraphQL client is Apollo.
There are many different way to connect to AWS AppSync with React, using different packages. Some required more packages/dependencies.
1. Use Apollo client
The latest version @apollo/client
can handle AWS AppSync’s query and mutation just fine with simple setup and also support React hook out of the box. Per their official doc, they will only support useQuery
for query and useMutation
for mutation hooks in their future release. Component based query and mutation will still work but will not be supported. However, for subscription, @apollo/client
and AWS AppSync API won’t work without complex setup because AppSync’s subscriptions use MQTT as the transport layer, but @apollo/client
support WebSockets.
If you are using React with version that support hook, and you don’t have the need for subscription, this option will work very well.
- Install required package
yarn add @apollo/client aws-appsync-auth-link graphql
- Create client.js with following setup.
aws-exports
is the config download from your AWS AppSync API
1 | import { |
- In your
index.js
add below code to Apollo client accessible.ApolloProvider
will make your AppSync client accessable any child of the entry point component whic is App in this example.
1 | import { ApolloProvider } from '@apollo/client'; |
Example of making a mutation call:
1 | import { |
2. Use AWS AppSync JavaScript SDK
This SDK can be used with apollo-client
and it is the offical SDK provided by AWS Labs. It support offline feature and also work with class components. Your index.js
will need to be setup with the following code. You will need to download aws-exports.js
from your AWS AppSync API and install react-apollo aws-appsync-react
for this setup. ``aws-exports.js` contains the GraphQL endpoint url and your AWS AppSync API region, auth type info.
new AWSAppSyncClient
will create a new AppSync client and store it in the client
variable.ApolloProvider
will make your AppSync client accessible any child of the entry point component whic is App in this example.
1 | import AWSAppSyncClient from 'aws-appsync' |
Rehydrate
will make sure application cache has been read and is ready to use before rendering the app. If app is not ready, it will show the default loading
text. If you want to confured the display text, you can call the render function as below.
1 | <Rehydrated |
This method didn’t work with out test application because it always show loading so it is not recommended to use unless you want to try it with a good reason. You can get more configuration from the project page here.
3. Use AWS Amplify
This is the easiest and with complete support of query, mutation, and subscription for AWS AppSync API. You will need your AWS AppSync API ID and the config export for this method. It can be found in AWS console.
Install AWS Amplify cli.
1
npm install -g @aws-amplify/cli
The cli is needed for importing API
Navigate to your React project root folder.
1
amplify init
Add the codegen category to your React project.
1
amplify add codegen --apiId YOUR_API_ID_HERE
Generate client code.
1
amplify codegen
Follow the steps in your terminal.
Add
aws-amplify
1
npm install aws-amplify
or use yarn
1
yarn add aws-amplify
Add below code to your React application root
index.js
1
2
3
4
5
6
7
8// some imports here
import { Amplify } from 'aws-amplify';
import amplifyConfig from './aws-exports';
// after all the imports
Amplify.configure(amplifyConfig);
// the rest of the code, ReactDOM etc.Example of how the function used to call mutation look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17import { API, graphqlOperation } from 'aws-amplify';
import { createTodo } from '/graphql/mutations';
const todo = {
item: 'visit qualityology.com'
}
const update = await API.graphql(
graphqlOperation(
createTodo,
{
input: todo,
},
),
)
.then((res) => res)
.catch((error) => console.log('error, creating user', error));
console.log('update', update);
Comments