Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@ttoss/aws-appsync-backend

Package Overview
Dependencies
Maintainers
2
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ttoss/aws-appsync-backend

AWS AppSync client for backend.

latest
Source
npmnpm
Version
0.2.2
Version published
Maintainers
2
Created
Source

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-backend

yarn

yarn add @ttoss/aws-appsync-backend

Getting 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.yml

Also, you may clone this project and run the command:

$ npm run example:deploy

Get 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/graphql

GraphQL 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:subscription

Destroy AWS AppSync API

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

Author

  • Pedro Arantes

License

MIT

Keywords

aws-appsync

FAQs

Package last updated on 30 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts