jeddeloh / rescript-apollo-client

ReScript bindings for the Apollo Client ecosystem
MIT License
127 stars 18 forks source link

Nullable field being discarded from mutation #130

Closed dfalling closed 2 years ago

dfalling commented 2 years ago

I have a nullable field that I'm trying to change from a value to null. The query simply drops the field, rather than passing the new null value. How can I make Apollo send a null value rather than just omitting the field? I've hunted in the Apollo docs and don't see anything, so I'm hoping there are some rescript tricks I'm missing.

Thanks!

dfalling commented 2 years ago

This was happening for nullable date fields, which I already have to transform for the input object. I'm making the None value map to a Some(null) which ensures it's sent. Feels like a hack but I'm not sure if there's another way:

let optionalDateToJson = (date: option<Js.Date.t>): option<Js.Json.t> => {
  switch date {
  | Some(date) => Js.Date.toJSONUnsafe(date)->Js.Json.string->Some
  | None => Js.Json.null->Some
  }
}
jeddeloh commented 2 years ago

For anyone finding this issue later (and if I'm understanding correctly), each query/mutation takes a mapJsVariables arg for this specific case of when you want to explicitly pass a null.

dfalling commented 2 years ago

The tricky part for me is I'm explicitly setting a None on an input_object, so I'd expect that would populate the field and send it as a null.

Expected behavior

set field to None -> send null don't set field to anything -> field is treated as an optional and omitted

jeddeloh commented 2 years ago

Yeah, I think that's an understandable intuition to have. However, when you think about it, there's no way to differentiate between something explicitly set to None and something that is implicitly None by its omission which is how we ended up here. We could create a new type of optional Some | None | Missing, but then the incompatibility with normal options would probably cause its own issues.