Hi, is there a way to create multiple nodes in one...
# prisma-whats-new
l
Hi, is there a way to create multiple nodes in one mutation?
n
@leopradel you can create nodes across relations using nested mutations - what's your use case?
l
I want to insert 300 objects of the same type in one mutation just to avoid 300 call to the api
Nested mutations doesn’t solve my problem but are really usefull simple smile
n
there's currently no way to do it - are you doing an initial data import?
l
When a user signup I want to import some data from a service
n
hmm ok - so this should be related to the user, right?
l
yep
n
you can actually use nested mutations then
probably you will call
createUser
first, get its id
and afterwards you can call
updateUser(id: "some-id" items: [{name: "first item", age: 3}, {name: "hi", age 12}])
l
okay thank you!
will try to do that
n
cheers! do note that using
items
with
updateUser
is always overwriting current connections
it's not appending as you might think
l
Cool good to know!
And if I want to connect multiple nodes in one mutation is that possible?
n
so, you have multiple pairs of nodes that should be connected pairwise?
for example, two users U1, U2 and two posts P1, P2 and you want to connect (U1, P1) and (U2, P2)?
l
more U1 to P1 and P2
n
ah
updateUser(id: "some-id" postsIds: ["p1-id", "p2-id])
l
With that method it will remove the old connections right?
n
yes
l
So if I want to add some new items and create relations with existing elements the best way to do that is to: - create a nested mutation that insert new items
updateUser(id: "some-id" items: [{name: "first item", age: 3}, {name: "hi", age 12}])
- get the ids returned for that mutation - And concat the ids of the new ones with the ones I already have
updateUser(id: "some-id" postsIds: ["p1-id", "p2-id', 'concat-id'])
n
this depends, how many items do you create in step 1, and how many in step 3?
l
It depends but I would say max 1000 on first insert
And on step 3 I want to make a connection with the user to these 1000 new ids + 1000 already existing in database
n
Yes I would say the way you lined it up is the best way - even though I honestly can't say if it's possible to connect to 2000 items in step 3 🙂 need to try it out
l
Okay thank you very much for your time simple smile !