DEV Community

Cover image for Build Serverless GraphQL API using AWS AppSync
Sougata Maity
Sougata Maity

Posted on

Build Serverless GraphQL API using AWS AppSync

_

Building a Serverless GraphQL API using AWS AppSync

_

GraphQL is a popular API design paradigm that allows clients to request specific data from a server in a flexible and efficient manner. AWS AppSync is a fully managed service that makes it easy to build and deploy GraphQL APIs on the AWS cloud. In this tutorial, we will see how to use AWS AppSync to build a serverless GraphQL API for a simple to-do list application.

Prerequisites:

An AWS account
The AWS CLI and the AppSync CLI installed on your local machine
Step 1: Create an AWS AppSync API

First, we need to create an AWS AppSync API that will serve as the back end for our GraphQL API. We can do this using the AppSync CLI by running the following command:

amplify api add
Enter fullscreen mode Exit fullscreen mode

Building a Serverless GraphQL API using AWS AppSync

GraphQL is a popular API design paradigm that allows clients to request specific data from a server in a flexible and efficient manner. AWS AppSync is a fully managed service that makes it easy to build and deploy GraphQL APIs on the AWS cloud. In this tutorial, we will see how to use AWS AppSync to build a serverless GraphQL API for a simple to-do list application.

Prerequisites:

An AWS account
The AWS CLI and the AppSync CLI installed on your local machine
Step 1: Create an AWS AppSync API

First, we need to create an AWS AppSync API that will serve as the back end for our GraphQL API. We can do this using the AppSync CLI by running the following command:

Copy code
amplify api add
This will start a guided process that will ask you to enter the following information:

API name: A name for your API (e.g. "ToDoAppAPI")
Authentication type: Choose "API Key" as the authentication type
Deployment stage: Choose "Development" as the deployment stage
Once you have entered this information, the CLI will create an AWS AppSync API and a local GraphQL schema file for you.

Step 2: Define the GraphQL schema

The GraphQL schema defines the types, queries, and mutations that make up your API. In the local GraphQL schema file that was created in the previous step, you can define the types and operations for your API.

For our to-do list application, we will define a Task type with the following fields:

type Task {
  id: ID!
  title: String!
  description: String
  status: TaskStatus!
}

enum TaskStatus {
  IN_PROGRESS
  COMPLETED
}
Enter fullscreen mode Exit fullscreen mode

We will also define the following queries and mutations:

type Query {
  getTasks: [Task]
  getTask(id: ID!): Task
}

type Mutation {
  createTask(title: String!, description: String): Task
  updateTask(id: ID!, status: TaskStatus!): Task
  deleteTask(id: ID!): Task
}

Enter fullscreen mode Exit fullscreen mode

Step 3: Connect to a data source

Next, we need to connect our GraphQL API to a data source where we can store and retrieve our to-do list tasks. AWS AppSync supports a variety of data sources, including Amazon DynamoDB, Amazon RDS, and AWS Lambda.

For this tutorial, we will use Amazon DynamoDB as our data source. To set this up, run the following command:

amplify api add-dynamodb-datasource

Enter fullscreen mode Exit fullscreen mode

This will start a guided process that will ask you to enter the following information:

Data source name: A name for your data source (e.g. "ToDoAppDynamoDB")
Table name: A name for the DynamoDB table (e.g. "ToDoAppTasks")
Primary key type: Choose "ID" as the primary key type
Sort key name: Leave this blank
Read/write capacity mode: Choose "On-demand"
Once you have entered this information, the CLI will create a DynamoDB table and configure it as the data source for your GraphQL API.

Step 4: Implement resolvers

Resolvers are the functions that are responsible for actually fetching and mutating data in the data source. AWS AppSync provides a mapping template language that you can use to write resolvers that translate GraphQL operations into data source operations.

To implement the resolvers for our to-do list application, we will need to write mapping templates for each of the queries and mutations that we defined in the' GraphQL' schema. Here is an example of what the mapping template for the 'createTask' mutation might look like:

{
  "version": "2018-05-29",
  "operation": "PutItem",
  "key": {
    "id": {
      "S": "$util.autoId()"
    }
  },
  "attributeValues": {
    "title": {
      "S": "$ctx.args.title"
    },
    "description": {
      "S": "$ctx.args.description"
    },
    "status": {
      "S": "IN_PROGRESS"
    }
  }
}

Enter fullscreen mode Exit fullscreen mode

You can write the mapping templates for the other queries and mutations in a similar way.

Step 5: Deploy the GraphQL API

Once you have written all the resolvers, you are ready to deploy your GraphQL API. Run the following command to deploy the API:

amplify api push

Enter fullscreen mode Exit fullscreen mode

This will create the necessary resources in your AWS account and deploy your GraphQL API.

Step 6: Test the GraphQL API

To test your GraphQL API, you can use a tool like GraphiQL or Postman. Simply send a GraphQL query or mutation to the API endpoint, and you should see the corresponding data being fetched or modified in the DynamoDB table.

Here is an example of a GraphQL query that fetches all tasks:

query {
  getTasks {
    id
    title
    status
  }
}

Enter fullscreen mode Exit fullscreen mode

And here is an example of a GraphQL mutation that creates a new task:

mutation {
  createTask(title: "Buy groceries", description: "Milk, bread, eggs") {
    id
    title
    status
  }
}

Enter fullscreen mode Exit fullscreen mode

Conclusion

In this tutorial, we saw how to use AWS AppSync to build a serverless GraphQL API for a simple to-do list application. AWS AppSync makes it easy to build and deploy GraphQL APIs on the AWS cloud, and it supports a variety of data sources and authentication options. By following the steps outlined in this tutorial, you should be able to build your own GraphQL API using AWS AppSync.

THANK YOU

Top comments (0)