Deploying a serverless backend using AWS AppSync

Introduction

AWS AppSync is a managed service that enables developers to build and deploy scalable GraphQL APIs quickly. With AppSync, you can create APIs that run on AWS Lambda, AWS DynamoDB, and other AWS services. AppSync also comes with built-in features like offline capabilities, real-time data synchronization, and data transformation.

In this tutorial, we’ll cover how to deploy a serverless backend using AWS AppSync. We’ll start by discussing the benefits of using AppSync, then go on to explain the concepts of GraphQL, and finally, move on to creating an API with AppSync.

Benefits of using AWS AppSync

AWS AppSync offers several benefits that make it an attractive option for developers who want to build and deploy scalable GraphQL APIs. Some of these benefits include:

1. Easy to set up and use

With AWS AppSync, you can set up a GraphQL API in just a few clicks. AppSync takes care of the infrastructure, so you can focus on building your GraphQL API.

2. Integration with other AWS services

AppSync integrates seamlessly with other AWS services like Lambda, DynamoDB, and Elasticsearch. This gives you the flexibility to choose the best combination of services for your API.

3. Real-time data synchronization

With AppSync, you can enable real-time data synchronization between your API and your client applications. This means that your client applications can receive updates in real-time as data changes.

4. Offline capabilities

AppSync enables offline capabilities for your API, so your users can continue to use your application even when they’re offline.

Understanding GraphQL

GraphQL is a query language for APIs that was developed by Facebook. It enables you to define the shape of the data that your API returns and allows you to retrieve that data in a single request.

GraphQL follows a single endpoint architecture, which means that all data is fetched using a single API endpoint. This is in contrast to RESTful APIs, which require multiple endpoints to retrieve different types of data.

GraphQL consists of three main components: queries, mutations, and subscriptions.

1. Queries

Queries are used to retrieve data from the server. You can think of a query as a GET request in a RESTful API.

Here’s an example of a GraphQL query:

query {
  getPosts {
    id,
    title,
    body
  }
}

In this example, we’re retrieving an array of post objects that contain an ID, title, and body.

2. Mutations

Mutations are used to modify data on the server. You can think of a mutation as a POST, PUT, or DELETE request in a RESTful API.

Here’s an example of a GraphQL mutation:

mutation {
  createPost(
    title: "My new post",
    body: "This is the body of my new post"
  ) {
    id,
    title,
    body
  }
}

In this example, we’re creating a new post object with a title and body.

3. Subscriptions

Subscriptions are used to enable real-time data synchronization between the server and the client.

Here’s an example of a GraphQL subscription:

subscription {
  onCreatePost {
    id,
    title,
    body
  }
}

In this example, we’re subscribing to new post objects as they’re created.

Creating an API with AWS AppSync

Now that we understand the concepts of GraphQL, we can move on to creating an API with AWS AppSync. In this tutorial, we’ll create a simple API that allows users to create, read, update, and delete posts.

Step 1: Create an AWS AppSync API

The first step in creating an AWS AppSync API is to create a new API. To do this, follow these steps:

  1. Go to the AWS Management Console and select “AppSync” under “Services.”
  2. Click on “Create API.”

  3. Choose the type of API that you want to create. For this tutorial, select “HTTP.”

  4. Give your API a name and description.

  5. Click on “Create.”

Step 2: Define your schema

The next step is to define your schema. Your schema defines the shape of the data that your API will return.

To define your schema, follow these steps:

  1. On the left-hand side of the AppSync console, click on “Schema.”
  2. Click on “Edit schema.”

  3. Add the following code to your schema:

type Post {
  id: ID!
  title: String!
  body: String!
}

type Query {
  getPost(id: ID!): Post
  getPosts: [Post]
}

type Mutation {
  createPost(title: String!, body: String!): Post!
  updatePost(id: ID!, title: String, body: String): Post!
  deletePost(id: ID!): ID!
}

In this schema, we have defined a post object that contains an ID, title, and body. We have also defined three operations: getPost, getPosts, and createPost. The getPost operation retrieves a post object by ID, the getPosts operation retrieves an array of post objects, and the createPost operation creates a new post object.

Step 3: Set up your resolvers

Now that our schema is defined, we need to set up our resolvers. Resolvers are responsible for fetching data from our backend data sources.

To set up your resolvers, follow these steps:

  1. On the left-hand side of the AppSync console, click on “Data sources.”
  2. Click on “Create data source.”

  3. Choose the type of data source that you want to create. For this tutorial, select “None.”

  4. Give your data source a name and description.

  5. Click on “Create.”

  6. On the left-hand side of the AppSync console, click on “Resolvers.”

  7. Click on “Create resolver.”

  8. Choose the operation that you want to create a resolver for. For this tutorial, select “getPost.”

  9. Specify the data source that you want to use. For this tutorial, select the data source that you just created.

  10. Add the following code to your resolver:

{
  "version": "2017-02-28",
  "operation": "GetItem",
  "key": {
      "id": $util.dynamodb.toDynamoDBJson($ctx.args.id)
  }
}

In this resolver, we’re telling AppSync to use the “GetItem” operation of our data source to retrieve a post object by ID.

  1. Click on “Create resolver.”
  2. Repeat steps 7-11 for the remaining operations: getPosts, createPost, updatePost, and deletePost. Here are the resolvers that you’ll need:

getPosts

{
  "version": "2017-02-28",
  "operation": "Scan"
}

createPost

{
  "version": "2017-02-28",
  "operation": "PutItem",
  "key": {
    "id": { "S": "$util.autoId()" }
  },
  "attributeValues": {
    "title": { "S": "$ctx.args.title" },
    "body": { "S": "$ctx.args.body" }
  }
}

updatePost

{
  "version": "2017-02-28",
  "operation": "UpdateItem",
  "key": {
    "id": $util.dynamodb.toDynamoDBJson($ctx.args.id)
  },
  "update": {
    "expression": "SET #title = :title, #body = :body",
    "expressionNames": {
      "#title": "title",
      "#body": "body"
    },
    "expressionValues": {
      ":title": $util.dynamodb.toDynamoDBJson($ctx.args.title),
      ":body": $util.dynamodb.toDynamoDBJson($ctx.args.body)
    }
  },
  "condition": {
    "expression": "attribute_exists(id)"
  }
}

deletePost

{
  "version": "2017-02-28",
  "operation": "DeleteItem",
  "key": {
    "id": $util.dynamodb.toDynamoDBJson($ctx.args.id)
  }
}

Step 4: Test your API

Now that our API is set up, we can test it using the AppSync console.

To test your API, follow these steps:

  1. On the left-hand side of the AppSync console, click on “Queries.”
  2. Run the following query to retrieve all post objects:

query {
  getPosts {
    id,
    title,
    body
  }
}
  1. Run the following query to retrieve a single post object by ID:
query {
  getPost(id: "1") {
    id,
    title,
    body
  }
}
  1. Run the following mutation to create a new post object:
mutation {
  createPost(title: "My new post", body: "This is the body of my new post") {
    id,
    title,
    body
  }
}
  1. Run the following mutation to update a post object:
mutation {
  updatePost(id: "1", title: "My updated post", body: "This is the updated body of my post") {
    id,
    title,
    body
  }
}
  1. Run the following mutation to delete a post object:
mutation {
  deletePost(id: "1")
}

Conclusion

In this tutorial, we covered how to deploy a serverless backend using AWS AppSync. We started by discussing the benefits of using AppSync, then went on to explain the concepts of GraphQL, and finally, moved on to creating an API with AppSync.

By following the steps outlined in this tutorial, you should now have a good understanding of how to create a GraphQL API using AWS AppSync.

Related Post