Hi, how can I update an optional property in types...
# prisma-whats-new
m
Hi, how can I update an optional property in typescript only if it is passed? I have a mutation with three optional connect ids, would like to include only those non empty in my update mutation. What is the recommended approach here?
Copy code
return ctx.db.mutation.updateBook(
      {
        where: { id },
        data: {
        author: {
          connect: {
            id: authorId
          }
        }
        cover: {
          connect: {
            id: coverId
          }
        }
      }
      }, info)
Wherein
authorId
and
coverId
could optionally be empty. If I simple pass the empty parameters i get the
You provided an invalid argument for the where selector
error message.
m
@Moritz what happens if you pass in null for either
authorId
or
coverId
?
I have a feeling prisma might ignore null arguments
m
If I simple pass the empty parameters i get the
You provided an invalid argument for the where selector
error message.
j
Yeah null should do or for brevity, just check of they are null and use different update calls
m
sounds like the error is on
where
. Do you have a valid ID?
m
It references the
where
from the connect...
Error: You provided an invalid argument for the where selector on Author.
My query has the author input:
Copy code
"author": {
            "connect": {}
          }
@juicycleff Thx, however, I feel that doing this will just yield very convoluted ifs, I think prisma should just ignore null values, shouldnt it?
j
@Moritz yeah it does
You just pass in null. Should do
But also please confirm
m
Well if I pass undefined, I get the mutation
Copy code
"author": {
           "connect": {}
         }
and the above error. This is when I have an optional mutation param
author
that is not passed in the mutation. This is not the behavior I was expecting...
@juicycleff have you ever successfully worked with null params?
j
Yeah
It works
Awesomely
And hey Moritz I'm looking to collaborate
m
Mh interesting for me it gives me an error... Ill play around a little bit to see where things are going wrong... Do you have a typescript code sample where your taking in an optional param?
What kind of collaboration? Im currently swamped with work but always open to interesting ideas 🙂
j
Yeah I do have typescript advance boilerplate
I will message you privately
m
Got it to work. The problem was that I was passing
Copy code
author: {
         connect: {
           id: authorId
         }
       }
       cover: {
         connect: {
           id: coverId
         }
       }
instead of
Copy code
author: {
connect:  authorId
}
cover: {
connect: coverId
}
Thanks guys!
Yes just drop me a message!
j
Ok great you go it fixed