AWS AppSync client for a NodeJS App. This package is a wrap of this tutorial.
This client is a Apollo JavaScript client plus the authentication method that we need to access AWS AppSync URL (more details about authentication method can be found on the AWS AppSync JavaScript SDK docs). Also, this backend client adds fetch polyfills and WebSocket to perform subscriptions.
npm install --save @ttoss/aws-appsync-backend
yarn add @ttoss/aws-appsync-backend
You must have AWS CLI installed and have the permissions to create AWS AppSync resources.
*note: in this section, we'll use AWSAppSyncBackendCoolChat
as the stack name, but you may choose any name you want.
We've provided a simple chat example using a CloudFormation template which creates an AWS AppSync API with few GraphQL operations.
To deploy the API, run this command:
$ aws cloudformation deploy --stack-name AWSAppSyncBackendCoolChat --template-file ./path_to_template/cloudformation.yml
Also, you may clone this project and run the command:
$ npm run example:deploy
After deployed, you can get the URL and API key accessing the AWS AppSync or the CloudFormation console and check the stack outputs.
Also, you can get them running the command:
$ aws cloudformation describe-stacks --stack-name AWSAppSyncBackendCoolChat --query 'Stacks[0].Outputs'
Which will return something like this:
- OutputKey: ApiKey
OutputValue: da2-rvbbc6xzkXXXXXXX
- OutputKey: Url
OutputValue: https://vnkglexXXXXX.appsync-api.us-east-1.amazonaws.com/graphql
import { AwsAppSyncBackend, AuthOptions, AUTH_TYPE, gql } from '@ttoss/aws-appsync-backend';
const url = ... // your URL here
const auth: AuthOptions = {
type: AUTH_TYPE.API_KEY,
apiKey: ... // your API key here
};
const awsAppSyncBackend = AwsAppSyncBackend({ url, auth });
awsAppSyncBackend
.query({
query: gql`
query {
oldMessages {
author
dateTime
content
}
}
`,
})
.then(({ data }) => {
console.log(data.oldMessages);
})
.catch((err) => {
console.error('Occurred an error.');
console.log(err);
});
const author = 'Pedro';
const content = 'Hello again!';
awsAppSyncBackend
.mutate({
mutation: gql`
mutation sendMessageMutation($author: String!, $content: String!) {
sendMessage(author: $author, content: $content) {
author
dateTime
content
}
}
`,
variables: { author, content },
})
.then(({ data }) => {
console.log(data.sendMessage);
})
.catch((err) => {
console.error('Occurred an error.');
console.log(err);
});
awsAppSyncBackend
.subscribe({
query: gql`
subscription {
sentMessage {
author
dateTime
content
}
}
`,
})
.subscribe({
next: ({ data }) => console.log(data.sentMessage),
error: (err) => {
console.error('Occurred an error.');
console.log(err);
},
complete: () => console.log('Completed.'),
});
If you want to use the example we've created, you just need to run these commands:
$ npm run example:query
$ npm run example:mutation -- --author Pedro --content "Hi"
$ npm run example:subscription
Finally, you may want to destroy the stack created running the command:
$ aws cloudformation destroy --stack-name AWSAppSyncBackendCoolChat
If you're using our example:
$ npm run destroy