retro / graphql-builder

GraphQL client library for Clojure and ClojureScript
MIT License
184 stars 15 forks source link

Enforcing data types #26

Open dsbw opened 2 years ago

dsbw commented 2 years ago

Howdy,

I've been using this (very helpful, thank you!) library for a project where I take data from a database via graphql and then put it into a JavaFX table control, which has raised an interesting issue that I'm wondering how other people handle.

Let's say I have a database field which is some kind of floating point (double or money or whatever). It's specified as Float on the GraphQL side, but when it's a round number, like 1.0, the JSON comes in as "1" and ends up as a Long. Most of the time, this isn't a problem, but using a Long in the JavaFX table column set for Double causes a type exception.

I think my handling for this is going to be on the table side, so that it detects longs and coerces to doubles accordingly, which will work for the table but not for any other circumstance where the Long/Double discrepancy might arise.

The real issue is, I think, json/read-str, which doesn't take into account the GraphQL type (and why would it?), so I was wondering if anyone else had the problem and how they'd handled it.

retro commented 2 years ago

We usually wrap the request fn with additional processing if it’s needed, there’s nothing automatic unfortunately

dsbw commented 2 years ago

Oh, so it's like "I know that this field is double so I'll convert it?"

I was thinking there might be a way to query the library to say "Hey, what type is this supposed to be?"