Example application showing how to use single-table design with GraphQL and DynamoDB

Overview

GraphQL + DynamoDB -- Single-table example

This repository includes an example of building a GraphQL API with DynamoDB using a single DynamoDB table. It is intended to pair with this guide on using DynamoDB in a GraphQL API.

Table of Contents

Usage

This application uses the AWS CDK to deploy a GraphQL API to AWS AppSync. Be sure to install and bootstrap the CDK before use.

To deploy, clone this repository and run the following commands:

npm i
cdk deploy

This will deploy an AppSync API and return the GraphQL root URL and an API key to access the API.

You can query the API in the AWS console, or you can open the index.html file to use a local GraphQL explorer. Before opening the file, be sure to replace GRAPHQL_ROOT and API_KEY with the received values from your deploy.

Application background

This sample application builds a portion of a SaaS blog hosting platform. Users create a Site on the platform and are able to create Posts on the site. Other users can view the Posts and attach a Comment to a Post.

The simplified ERD is below. This application includes the general CDK code and AppSync resolvers to demonstrate the key differences between single-table and multi-table design with GraphQL + DynamoDB.

AppSync - ERD

Takeaways

  • The DynamoDB data modeling uses common single-table design principles. The DynamoDB table has a composite primary key using the generic PK and SK for the attribute names. Further, each item written to the table includes a _TYPE attribute to help distinguish the particular type.

  • Single-table design optimizes for latency over simplicity. In using a single-table design, you can fetch multiple types of entities in a single request, as shown in the getSite Query request and response templates.

    Notice the request template does a lookahead to see if the incoming query is asking for the posts property on the site entity and adjusts its DynamoDB Query accordingly. Further, the response template needs to prepare a response from the result set containing both Site and Post items.

  • The single-table model adds some constraints. In this single-table model, both Site and Post items use the Site's domain as the partition key. This enables us to fetch both in a single request.

    In our example repository showing the same application with a multi-table design, the Post items use a siteId as the partition key. Because Post items are retrieved after the Site is retrieved, it can use a more normalized model. This makes it easier to change the domain of Site and Post items in that example, at the cost of increased latency for common requests.

You might also like...

Get-A-Room example application using Domain Driven Design and Clean Architecture. Written in TypeScript and deployed to AWS with a serverless stack.

Get-A-Room example application using Domain Driven Design and Clean Architecture. Written in TypeScript and deployed to AWS with a serverless stack.

Domain Driven Microservices on AWS in Practice This project provides a Domain Driven Design & Clean Architecture-informed, multi-service event-driven

Dec 31, 2022

GraphQL-first boilerplate that scales with TypeScript + Node Express + Apollo GraphQL APIs.

graphql-typescript-boilerplate A boilerplate project for quickly building Graphql APIs and with typescript 🚀 Installation Install the dependencies: y

May 15, 2022

next-graphql-server is a library for building production-grade GraphQL servers using Next.js with API Routes

next-graphql-server next-graphql-server is an easy to use Next.js library for creating performant GraphQL endpoints on top of Next.js API Routes. Star

Nov 21, 2022

GraphQL Hive provides all the tools the get visibility of your GraphQL architecture at all stages, from standalone APIs to composed schemas (Federation, Stitching)

GraphQL Hive GraphQL Hive provides all the tools the get visibility of your GraphQL architecture at all stages, from standalone APIs to composed schem

Dec 21, 2022

A basic example of a GraphQL API built with NestJS, Prisma and TypeScript

A progressive Node.js framework for building efficient and scalable server-side applications. Description Nest framework TypeScript starter repository

Dec 18, 2022

Learn GraphQL by building a blogging engine. Create resolvers, write schemas, write queries, design the database, test and also deploy.

GraphQL Blog graphqlblog.com Learn GraphQL by building a blogging engine. Create resolvers, write schemas, write queries, design the database, test an

Aug 17, 2022

Sample apps showing how to build music and video apps for Xbox using a WebView.

Sample apps showing how to build music and video apps for Xbox using a WebView.

description languages name page_type products urlFragment Sample showing how to build music and video apps using primarily web technologies for Xbox.

Dec 14, 2022

NX monorepo showing the TMDB Watchlist mobile app with Expo, tRPC, Next, and Prisma

NX monorepo showing the TMDB Watchlist mobile app with Expo, tRPC, Next, and Prisma

tmdb-watchlist-prisma This app uses TMDB to retrieve a list of Now Playing movies. You can add/remove movies to track which ones you've watched. There

Dec 28, 2022

It is a tours website for showing the information about all the tours of this company and making the clients able to book them.

It is a tours website for showing the information about all the tours of this company and making the clients able to book them.

NATOURS APP Table of Contents Deployed Website Built With Getting Started Description Documentation Screenshots Deployed Website : NOTE: Heroku is pla

Sep 24, 2022
Comments
  • Incorrect sort key in example

    Incorrect sort key in example

    I haven't tested your solution bus was looking at the implementation and I think there is an error in your template:

    See: https://github.com/alexdebrie/graphql-dynamodb-single-table/blob/23c5d95511f6207ac6b02d961c6071cac320b30a/lib/mapping-templates/Query.getPostsForSite.request.vtl#L8

    Shouldn't the SK be POST#? The post is created with POST#${id} as sort key!

    opened by Nr18 3
Owner
Alex DeBrie
Alex DeBrie
NextJS with GraphQL using type-graphl and graphql-codegen adoptable dogs example code

This is a Next.js project bootstrapped with create-next-app. Getting Started First, run the development server: npm run dev # or yarn dev Open http://

Jack Herrington 2 Mar 31, 2022
Small example showing how you can make game sprite animations using CSS with Javascript movement.

Hi there, I'm Björn Hjorth ?? I like combining the web and game development, if you like what you see please do not be a stranger and say "Hi" on Twit

Björn Hjorth 29 Nov 9, 2022
A serverless AWS expense tracker API. AWS Lambda functions, API gateway, and Dynamodb are among the ingredients.

AWS-Serverless-API A serverless AWS expense tracker API. AWS Lambda functions API gateway Dynamodb Endpoints Create a new expense: Method: POST Body f

Ondiek Elijah Ochieng 1 Jul 16, 2022
awsrun 189 Jan 3, 2023
The Remix Stack for deploying to AWS with DynamoDB, authentication, testing, linting, formatting, etc.

The Remix Stack for deploying to AWS with DynamoDB, authentication, testing, linting, formatting, etc.

Remix 311 Jan 1, 2023
Functionless-based mini-framework for DynamoDB migrations in AWS CDK.

dynamodb-migrations This repo is heavily in progress! Readme describes desired contract and functionality. Please do not try using it yet!. I'm not ev

Rafal Wilinski 23 Dec 20, 2022
✏️ A small jQuery extension to turn a static HTML table into an editable one. For quickly populating a small table with JSON data, letting the user modify it with validation, and then getting JSON data back out.

jquery-editable-table A small jQuery extension to turn an HTML table editable for fast data entry and validation Demo ?? https://jsfiddle.net/torrobin

Tor 7 Jul 31, 2022
Another table select prompt plugin of inquirer.js, with powerful table render and filters.

inquirer-table-select-prompt Table row selection prompt for Inquirer.js 动机 现有的 inquirer.js 没有支持表格行选中的命令行交互的插件. 社区内能查找到的,只有一个二维数组的 checkbox,eduardobouc

锂电 3 Jan 7, 2023
This web application is a type of a scoreboard showing the names and current scores of the leading competitors in any field which is using API's to get and set data

Leaderboard-project This web application is a type of a scoreboard showing the names and current scores of the leading competitors in any field which

Ravi Teja 5 Feb 11, 2022
Kyrillos Hany 14 Aug 10, 2022