Search code examples
reactjskotlingraphqlapollo-clientapollo-server

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


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: "test@test.com" //<- updated email
}

const input2 = {
   email: "test@test.com"
}

Solution

  • 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.