Is it possible to update associated resources at t...
# prisma-whats-new
l
Is it possible to update associated resources at the same time in one query? This one works for creating new ones if you leave the id out. If you add the id it raises an error.
Copy code
mutation {
  updateItemType(
    id:"cj1nldl3fqvz50122qxrnqd99"
    name:"test"
    itemTypeLine: [
      {
        id: "cj1wbh8ycv3zm0165nz5de0q3"   <--- when removed it creates a new one but I want the existing resource
        amount: 5
      }
    ]
  ) {
    id
    name
    itemTypeLine {
      id
      amount
    }
  }
}
n
@lucfranken you cannot currently update an existing connected node like that
are the two nodes already connected?
l
ok clear, so I should run multiple mutations on them correct?
yes
n
yes
l
ok, this would be a nice feature I guess. Now my code will be littered with loops checking if(id) -> run separate update mutation }else{ run insert }
n
what do you use the if here for?
l
Let's say you have a React component (with subs) which shows an invoice. You let the user change the invoice and the invoiceLines. I want to save all of that in one go.
That's the scenario in real world.
n
why do you need to check the id then?
l
How would I be able to save the invoice + invoice lines (some new, some existing) in one mutation?
n
Ah, wasn't aware that you are dealing with some new and some existing πŸ™‚
Well in your UI you know which ones are newly created
l
Yes, that is because the problem is that the state (for the user) is the total invoice. Only when he pressed save I want to update the invoice. He can also cancel.
Yes I do know which ones are new
n
I wouldn't say so
In your case it's about a nested update in combination with a nested "append"
hmm well it could be done with a nested upsert, right? I'l l add it to this feature request: https://github.com/graphcool/feature-requests/issues/127
l
sounds good!
n
thanks for the suggestion! πŸ™
l
so for now, that's the best approach? 3 separate mutations? One for updating the Invoice, one for adding a line and one for updating a line?
n
for now, I would use nested mutations from the other side for new ItemTypeLines:
l
thanks for your quick support!
very cool how you guys are active here on slack!
n
Copy code
mutation {
  createItemTypeLine(..., itemId: "item-id") {
    id
  }
}
πŸ™‚
l
mm, I don't get your example.
n
So yes, you will need 3 types of mutations
l
ok clear
n
1)
updateItemType
2)
createItemLineType
for each new
itemLineType
3)
updateItemLineType
for each existing
itemLineType
l
I read about beta, are there interesting new features in there?
yes thanks
l
thanks! Will take a look there
πŸ‘ 1
@nilan: Implemented it as you described. Works fine but also really shows why it would be usefull to allow nested updates. You have loops, you have to attach the id to every invoice line, check whether it’s generated on the client or is already on server etc. So really a useful feature imo!
πŸ‘ 1
Also I was thinking about immutability in react. Actually it would make quite a lot of sense to really overwrite a full record including associated records.