ra-aws-amplify icon indicating copy to clipboard operation
ra-aws-amplify copied to clipboard

Example of many to many relationship

Open mayteio opened this issue 4 years ago • 1 comments

In the example, a post can have many editors.

type Post
  @model
  {
  id: ID!
  title: String!
  content: String
  editors: [PostEditor] @connection(name: "PostEditors")
}

type PostEditor
  @model {
  id: ID!
  post: Post! @connection(name: "PostEditors")
  editor: User! @connection(name: "UserEditors")
}

type User
  @model {
  id: ID!
  username: String!
  posts: [PostEditor] @connection(name: "UserEditors")
}

mayteio avatar Mar 18 '20 06:03 mayteio

Create works nicely on the feature/many-to-many branch though I have yet to get update or delete working.

Basically, via redux-sagas, we listen for CRUD_CREATE_SUCCESS, CRUD_UPDATE_SUCCESS, CRUD_DELETE_SUCCESS and fire off new dataProvider calls depending on what we're trying to do. See here.

Delete would be easy - in the example app, there is a PostCategory connection (many to many) with a queryField postCategorysByPost. You'd simple query this with the postId, then use dataProvider.delete to delete each connection.

Update is a little trickier:

  • get all connections via postCategorysByPost queryField
  • separate them into two lists:
    • those that exist in the form values that don't in the DynamoDB record (create them)
    • those that don't exist in the form values but do in the DynamoDB record (delete them)

mayteio avatar Apr 03 '20 07:04 mayteio