Amplify-js: How to add business keys/unique constraint in GraphQL - AppSync AWS

Created on 29 Dec 2018  路  5Comments  路  Source: aws-amplify/amplify-js

Here is my definition of an entity/model in GraphQL SDL that is used by AWS Amplify to create a App Sync API and its table in DynamoDB.

I want to make the name and author as the business keys. Is there a way to achieve this ? I believe Prism has @Unique directive for the same. Or if it is not possible, can a constraint be added in the table in DynamoDB ?

type Post 
  @model 
  @versioned 
  @auth(rules: [{allow: owner}]) 
{
  id: ID!
  name: String!
  author: String!
  description: String
}

GraphQL investigating pending-close-response-required question

All 5 comments

@bishonbopanna, you can add a unique Primary Key (Partition Key + Sort Key) in DynamoDB, but that's about it.

If what you're trying to achieve is a unique combination of name and author, for example:
Mutation 1: createPost(name: A, author: B) \\ Succeed
Mutation 2: createPost(name: A, author: C) \\ Succeed
Mutation 3: createPost(name: A, author: B) \\ Error Unique Constraint Violation
You could create a table with Name as the Partition Key and Author as the Sort Key

However, if what you're trying to achieve is unique name + unique author, i.e:
Mutation 1: createPost(name: A, author: B) \\ Succeed
Mutation 2: createPost(name: A, author: C) \\ Error Unique Constraint Violation
Your best bet is to create a Pipeline resolver: First query the DB for name / author. If found, throw a custom error. Otherwise, proceed with mutation.

Thanks @temideoye for the response. Could you please confirm this statement - "You could create a table with Name as the Partition Key and Author as the Sort Key" as I tried it and it didnt seem to work.

Also, can a feature request be made to add "@Unique" constrain to AWS supported Graphql SDL ?

@bishonbopanna, when you say "it didn't seem to work", did you mean the table creation failed? Otherwise, what did you try and what was the error message? As for the feature request, sure, feel free to create one + notify the Amplify team.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been automatically closed because of inactivity. Please open a new issue if are still encountering problems.

Was this page helpful?
0 / 5 - 0 ratings