Part 7 of the Serverless series.

AWS AppSync is a managed GraphQL service. Clients ask for exactly the data they need, in one request, and AppSync fetches it from your data sources: DynamoDB, Lambda, HTTP APIs, Aurora, OpenSearch, EventBridge or Bedrock. It also pushes real-time updates over WebSockets with subscriptions.

Why GraphQL?

With a REST API, a mobile screen showing a game, its players and the current question might need three requests, each returning more fields than the screen uses. With GraphQL, the client asks once:

query {
  game(id: "ABC") {
    topic
    players { nickname score }
    currentQuestion { text answers }
  }
}

That’s most valuable when many different clients (web, mobile, partners) need different shapes of the same data.

The building blocks

Schema: the types and operations your API offers:

type Game {
  id: ID!
  topic: String!
  status: String!
}

type Query {
  game(id: ID!): Game
}

type Mutation {
  startGame(id: ID!): Game
}

type Subscription {
  onGameUpdated(id: ID!): Game
    @aws_subscribe(mutations: ["startGame"])
}

Data sources: where data comes from.

Resolvers: connect a field to a data source. AppSync’s JavaScript resolvers run inside AppSync itself, so simple reads and writes need no Lambda at all:

import * as ddb from '@aws-appsync/utils/dynamodb';

export function request(ctx) {
  return ddb.get({ key: { PK: `GAME#${ctx.args.id}`, SK: 'META' } });
}

export function response(ctx) {
  return ctx.result;
}

Pipeline resolvers chain several functions, for example “check the caller is the host, then update the game”.

Subscriptions

Clients subscribe over a WebSocket, and AppSync pushes data whenever a linked mutation runs. In the schema above, every client subscribed to onGameUpdated(id: "ABC") receives the result of startGame(id: "ABC"). AppSync manages the connections and fan-out for you.

The limitation: GraphQL subscriptions are triggered by mutations through AppSync. If you just need to broadcast events from your backend to clients, AppSync Events is simpler.

Authorisation

AppSync supports several modes, and can combine them on one API:

  • API key: for public data and prototypes.
  • Amazon Cognito user pools: signed-in users, with group-based rules in the schema.
  • IAM: backend services and unauthenticated Cognito identities.
  • OIDC: any external identity provider.
  • Lambda: your own logic.

When to choose AppSync GraphQL

  • Yes for client-heavy apps with varied data needs, real-time features tied to data changes, and teams already using GraphQL. AWS Amplify’s data tooling builds on it.
  • Maybe not for simple CRUD APIs consumed by one client, where an HTTP API and Lambda are easier to reason about.

Hands-on

In the AppSync console, choose Create API → GraphQL APIs → Design from scratch and let it create a DynamoDB-backed type. Run queries and mutations in the console’s query editor, then open the same API in a second browser tab, start a subscription, and watch it receive updates when you run a mutation in the first.

Gotchas

  • Watch for N+1 queries. A list of 50 games each resolving players with its own request is 51 requests. Design keys so related data comes back together, or batch.
  • Errors return HTTP 200. GraphQL reports errors in the response body, so monitor AppSync’s error metrics rather than HTTP status codes.
  • Set query depth and complexity limits on public APIs.

Next: AppSync Events: serverless WebSockets.

← Series overview