lighthouse icon indicating copy to clipboard operation
lighthouse copied to clipboard

Remove all and set new children of hasMany relation using nested mutation `@update`

Open daanhegger opened this issue 4 years ago • 0 comments

What problem does this feature proposal attempt to solve?

I read about nested mutations and Ligthouse's 'shortcuts' to quickly manipulate objects in a hasMany relationship using these features:

type Mutation {
  updateUser(input: UpdateUserInput! @spread): User @update
}

input UpdateUserInput {
  id: ID!
  name: String
  posts: UpdatePostsHasMany
}

input UpdatePostsHasMany {
  create: [CreatePostInput!]
  update: [UpdatePostInput!]
  upsert: [UpsertPostInput!]
  delete: [ID!]
  connect: [ID!]
  disconnect: [ID!]
}

However, I often find myself having to clear all 'children' objects and create & connect new ones from the client side. Currently, I solve this by first querying the IDs of all children and deleting them using delete: [ID!]. Followed by a create: [CreatePostInput!] where I can submit objects that are not yet in the database.

My feature proposal combines the deletion of old hasMany-items, creation of the new ones, and connecting the new ones to the parent object. This makes client-side code easier and, when using transactions, makes code more stable: If the client-side code requests the delete, and the creation of the new records fails, the parent is left without its children.

Which possible solutions should be considered?

To make this pattern easier I propose a set: [CreatePostInput!] which first deletes all children (in this example: Posts), after which it creates new posts from the input and immediately connects them to the parent (in this example: User).

daanhegger avatar Aug 09 '21 20:08 daanhegger