With Apollo Graphql, is it the same having a field as null and not sending it?

With Apollo Graphql, is it the same having a field as null and not sending it?


0

I am implementing a React Typescript app that communicates to a Kotlin backend using GraphQL (Apollo on the Frontend).

My mutation is :

updateProfile(input: UpdateProfileInput)

input UpdateProfileInput {
  firstName: String
  lastName: String
  email: String
}

I am supposed to send as an input only the fields that have been changed. So I should not send email if it’s the same to what it currently is in our database.

My question is, on the Frontend, will it work is I set the value to null, or would it still detects on the Backend that the field was sent?

For example, is it the same if I send input1 and input2 (as the fields are nullable):

const input1 = {
   firstName: null,
   lastName: null, 
   email: "[email protected]" //<- updated email
}

const input2 = {
   email: "[email protected]"
}

1 Answer
1


0

In a Graphql mutation, not sending a field is different to setting a field explicitly to null.

You should avoid sending any value for a field you do not wish to update. In your case, sending null would override the value for email that is already there.

New contributor

Joe Ryan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



Leave a Reply

Your email address will not be published. Required fields are marked *