DEV Community

Cover image for Effortless API Scaling: Unlock the Power of AWS AppSync
Lilupa Karu
Lilupa Karu

Posted on

Effortless API Scaling: Unlock the Power of AWS AppSync

AWS AppSync Journey

During the challenging times of the COVID-19 pandemic in 2020, I embarked on architecting my first AWS AppSync enterprise solution with GraphQL APIs. The project involved integrating several data sources, including a legacy system. After completing the architecture design and undergoing a thorough peer review, our engineering teams began building the solution, with AWS AppSync as the core component. The project was a success, delivering a robust solution that brought smiles to everyone involved. At the time, I had the intention of writing an article about the experience, but, much like the pandemic itself, that plan faded into the background.

Fast forward to a few months ago, I had a coffee catch up with Brett, an ex-colleague who had been part of that AppSync project. As we reminisced about the incredible time we spent on the project, I was inspired to revisit AWS AppSync and finally write this article. Reflecting on the challenges we overcame and the happy outcomes we achieved, I owe it to Brett's recollection of those moments to share this journey.

Since AWS AppSync is a serverless implementation of GraphQL, a brief overview of GraphQL is necessary before proceeding.

What is GraphQL

The fundamental concept of a GraphQL API is that all API functionality is accessed through a unified query language (GQL) via a single endpoint. Instead of making multiple requests to different endpoints to retrieve various data needed for building a software application, a single request can be issued to a GraphQL API, returning all the necessary data at once. This approach reduces the complexity of modern applications and enhances user experience by enabling faster load times.

Image description

The system providing data to a GraphQL API is rarely just a single application. In my experience, I’ve worked with nearly a dozen applications where GraphQL played a key role, but only one of those had a single data source. Typically, the backend consists of multiple microservices or data sources, with the GraphQL API layer responsible for aggregating data from various applications and delivering a unified response to the API requester. This GraphQL layer must efficiently parse the incoming requests, identify the appropriate data sources for each part, and seamlessly combine the results to generate the final response.

Components in GraphQL

GraphQL Types
GraphQL Types define the structure of entities by specifying their fields and attributes. Below is a list of the various types available.

  • Scalar Type
  • Object Type [In the Object type, there are three special root operation types: Query, Mutation, and Subscription]
  • Input Types
  • Enumeration Type
  • Union and Interface Type
  • Lists and Non-Null

Schema
The schema is treated as a contract between the server and the client. It consists of a collection of types and fields, along with a defined set of operations available for interacting with the data.

type Project {
    id: ID!
    name: String!
    client: String!
    tasks: [Task!]
  }

type Task {
    title: String!
    description: String
    duration: Int
  }
Enter fullscreen mode Exit fullscreen mode

Query
Queries serve as entry points on a GraphQL server, providing read access to data sources.

  query {
    project (id: 1) {
      name
      client
    }
  }
Enter fullscreen mode Exit fullscreen mode

Mutations
GraphQL Mutations function as entry points on a GraphQL server, allowing write access to data sources. Whenever data needs to be modified—whether creating, updating, or deleting—it is done through GraphQL mutations.

type Mutation {
  addProject(name: String, client: String): Project
}
Enter fullscreen mode Exit fullscreen mode

Resolvers
Resolvers are built-in functions in GraphQL that connect types or fields defined in the schema to the corresponding data in the data sources.

AppSync Deep Dive

With a basic understanding of GraphQL, attention can now be turned to AWS’s serverless offering of GraphQL. AWS AppSync, a fully managed serverless service, facilitates real-time data queries, synchronisation, and communication. Through AppSync, AWS provides a GraphQL-as-a-Service solution, enabling the seamless creation of scalable and resilient GraphQL APIs in the cloud. Request parsing and resolution, as well as integration with other AWS services such as AWS Lambda, NoSQL and SQL data stores, and HTTP APIs, are all handled to retrieve backend data for the API.

Overview and Architecture

Developers are granted access to their data via this managed GraphQL service, which offers several advantages over traditional gateways. GraphQL promotes declarative coding and integrates effectively with modern tools and frameworks such as React, React Native, iOS, and Android. Subscriptions can be utilised to implement live updates, push notifications, and other features.

When GraphQL subscription operations are invoked, AWS AppSync automatically establishes and maintains a secure WebSocket connection. Data can then be distributed in real-time to subscribers from a data source, while connection and scaling requirements are managed.

With AWS AppSync, serverless GraphQL and Pub/Sub APIs allow secure querying, modification, or publication of data using a single endpoint.

GraphQL APIs

AWS AppSync-built GraphQL APIs empower frontend developers to seamlessly query multiple databases, microservices, and APIs through a single GraphQL endpoint. This simplifies data interaction and accelerates application development.

Image description

Pub/ Sub APIs

Image description

AppSync Use Cases

Gateway for microservices
AppSync serves as a powerful gateway for microservices, allowing you to access and aggregate data from various sources with ease.

Real Time Collaboration
AppSync enables scalable, real-time collaboration by broadcasting data from the backend to all connected clients (one-to-many) or facilitating communication between clients (many-to-many). This is ideal for chat applications, shared documents, or live dashboards.

Polyglot data access
With AppSync, you can interact with diverse data sources—including SQL databases in Amazon Aurora Serverless, NoSQL tables in Amazon DynamoDB, search indexes in Amazon OpenSearch Service, REST endpoints via Amazon API Gateway, and serverless backends using AWS Lambda—using a single GraphQL call.

Offline Delta Sync
AppSync integrates with Amplify DataStore, an on-device persistent storage engine, to enable offline-first capabilities. Data is automatically synchronised between mobile/web apps and the cloud using GraphQL, ensuring a seamless experience even in limited connectivity scenarios.

Let’s Build Something Together

Why just read when you can build? Let’s dive into a practical example to explore how AWS AppSync works in action.

For this demo, we’ll build a simple coffee ordering app—a favorite theme of mine because, let’s face it, life without coffee isn’t life at all.

Building the Coffee Order App

Here’s what the app does:

  1. GraphQL Schema for Orders Customers can place coffee orders by providing their name, mobile number, and preferred coffee type. Orders are submitted through the createOrder mutation and stored in a DynamoDB table via a Lambda function.

Image description

Image description

  1. Querying Orders
    Coffee shop owners can retrieve new orders from the database using the listOrders GraphQL query. This query also utilises the same Lambda function for data retrieval.

  2. Infrastructure and Frontend
    The backend is built entirely with AWS CDK, ensuring it adheres to Infrastructure as Code (IaC) best practices. On the frontend, I’ve created a simple React application that fetches new coffee orders via AppSync’s GraphQL API.

Image description

The complete application is available on GitHub for you to explore and adapt.

AWS CDK-based AppSync APIs: Vitinya CDK
React Front-end web app: Vitinya Web

As for the app’s name, Vitinya, it’s inspired by the Vitinya Pass in Bulgaria’s Balkan Mountains—a place I’ve yet to visit but remains high on my bucket list. By naming the app after this scenic destination, I’ve given myself a constant reminder to plan my Eastern European holiday soon!

Until Next Time

That’s all for now. Stay tuned for more articles and hands-on examples in the beautiful AWS ecosystem. Until then, happy AWSing!

Top comments (2)

Collapse
 
rayan_amarasekara_5d09269 profile image
Rayan Amarasekara

Interesting content. Thanks for the step by step explanation 🤩

Collapse
 
isuri profile image
Isuri

Thanks for sharing this informative piece on AWS app sync.The example (building coffee order app) you provided really helped me to understand how AWS app sync works. Learned a lot!