jsConverter: Handling undefined in JS Object


#1

I’m using reason-apollo to fetch data from my GraphQL server. Sometimes I don’t want to fetch everything, so I only query a few fields, for instance:

projects {
  id
  name
}

The issue comes when I try to convert this query to a record. Say I’m using the following record:

[@bs.deriving jsConverter]
type project = {
  id: int,
  name: string,
  color: option(string)
}

This gives an error, because the jsConverter expects: array({.. "color": option(string), "id": int, "name": string}). Is there a way to have jsConverter ignore fields that are wrapped in option?

Thanks,
Nicholas


#2

Sadly there are no way to get around this issues. I can second that I bump into this situation frequently when using GraphQL. Even though GraphQL responses is typed, it’s built with dynamic languages in mind. You can’t make a uniform type (let’s say project) and reuse it across your codebase while only querying the needed information


#3

I’ve run into this issue as well and opted to leave GraphQL data access as Js.t and access using ##.

Instead of converting to data to records pre-emptively when the data enters the system, I’d pass the Js.t around and convert to a record with right shape right before my UI needs to use the data (generally happens at a component’s prop contract).


#4

Yeah, I might just keep a bunch of redundant records around. Maybe I’ll look into opening a PR about this. Missing serde from Rust lol