@ttoss/aws-appsync-backend v0.2.2
AWS AppSync Backend
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.
Installation
npm
npm install --save @ttoss/aws-appsync-backendyarn
yarn add @ttoss/aws-appsync-backendGetting Started
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.
Deploy AWS AppSync API
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.ymlAlso, you may clone this project and run the command:
$ npm run example:deployGet the URL and API key
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/graphqlGraphQL operations
Create the client
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 });Query
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);
});Mutation
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);
});Subscription
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.'),
});Using the provided example
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:subscriptionDestroy AWS AppSync API
Finally, you may want to destroy the stack created running the command:
$ aws cloudformation destroy --stack-name AWSAppSyncBackendCoolChatIf you're using our example:
$ npm run destroy