How to manipulate AWS AppSync and GraphQL to comply with DynamoDB best practices?

How to manipulate AWS AppSync and GraphQL to comply with DynamoDB best practices?

DynamoDB operates best with a single table per application (<a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-general-nosql-design.html" target="_blank">https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-general-nosql-design.html</a>), yet AppSync by default breaks that rule by the way it auto-generates code from the GraphQL schema (that AWS recommends users allow the API to do). Therefore, to use AppSync with GraphQL while upholding DynamoDB's best practices (assuming DynamoDB is the sole data source for the GraphQL API), would this approach work?

DynamoDB operates best with a single table per application (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-general-nosql-design.html), yet AppSync by default breaks that rule by the way it auto-generates code from the GraphQL schema (that AWS recommends users allow the API to do). Therefore, to use AppSync with GraphQL while upholding DynamoDB's best practices (assuming DynamoDB is the sole data source for the GraphQL API), would this approach work?

First, create a blank DynamoDB table (TheTable in this example) and give it a partition key (partitionKey) and a sort key (sortKey).

Second, manually enforce every GraphQL type to be backed by that table (TheTable). This is where AppSync automatic code generation will go the other direction.

GraphQL schema:

type Pineapple {
    partitionKey: String!
    sortKey: String!
    name: String!
}

create varying types as long as they all map to the same table

type MachineGun { partitionKey: String! sortKey: String! name: String! }

input CreatePineappleInput { partitionKey: String! sortKey: String! name: String! }

type Mutation { createPineapple(input: CreatePineappleInput!): Pineapple }

Third, configure your own resolvers to handle the schema (again avoid auto-generated code):

Resolver:

{
    "version" : "2017-02-28",
    "operation" : "PutItem",
    "key" : {
        "partitionKey": $util.dynamodb.toDynamoDBJson($ctx.args.input.partitionKey),
        "sortKey": $util.dynamodb.toDynamoDBJson($ctx.args.input.sortKey),
    },
    "attributeValues" : $util.dynamodb.toMapValuesJson($ctx.args.input),
}

And when we run the mutation in the AppSync console:

GraphQL operation:

mutation createPineapple($createPineappleInput: CreatePineappleInput!) {
  createPineapple(input: $createPineappleInput) {
    name
  }
}

{ "createPineappleInput": { "partitionKey": "attraction123", "sortKey": "meta", "name": "Looking OK" } }

We get the result we hoped for:

{
  "data": {
    "createPineapple": {
      "name": "Looking OK"
    }
  }
}

Is there a reason why this wouldn't achieve single-table efficiency using AppSync?

amazon-web-services graphql

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Amazon Web Services Partnership

We’re excited to announce that we’ve been selected as an inaugural Amazon Web Services’ (AWS) Technology Partner. The Amazon Partner Network (APN) Competency Program highlights companies who have demonstrated technical expertise and customer...

Infrastructure as Code on Amazon Web Services (AWS)

How to Build and Manage Infrastructure as Code with Amazon Web Services - AWS CloudFormation, AWS CloudWatch and Amazon EC2

Web Design Service in USA

Looking for designers for your business website design? We at Data EximIT offer our best **[web designing services in the USA](https://www.dataeximit.com/services/ "web designing services in the USA")** with the aim of craft your website more...

AWS Launches a No-Code Mobile and Web App Builder in Beta: Amazon Honeycode

Recently, AWS announced the beta release of Amazon Honeycode, a fully managed service allowing customers to build mobile and web applications without writing any code quickly.

For World Class Web Development Services in India visit RB Genie

Do you want excellent and world class web development services for your valuable projects? Contact **RB Genie **now, we have more than 8 years experienced team of web developers, which specializes in overall web design and website development...