Graphql - Float field when decimal number stored as integer

Hi There,

I have been tinkering with FaunaDB and I believe I found an issue. Lets say I have a collection called Order that has a data property price. Imagine I’ve got a Graphql type defined like so

type Order {

What I noticed is that if I create a document in Order and the price is a whole decimal number like 502.00, it is stored as 502 by FaunaDB (which is perfectly logical).

However, when I use the graphql interface with a graphql query that pulls out the particular order and that field, I’ll get an error like this:

"message": "Can't convert '502' to double",

Am I doing something wrong? Or perhaps this is a bug?

You are creating that decimal number via the dashboard right?
FQL and GraphQL (at least at this point) are separate, they can be used interchangeably but one thing to realize is that FQL will not validate data against the GraphQL schema.

What is happening here is that JavaScript is going to decide that your 502.00 is not a float and FQL will happily store it as an integer. I assume you added this document either via the dashboard or via FQL. The dashboard also executes FQL to store these documents but you do not have control over this FQL so there is no workaround. There are two other workarounds though:

  • Store your data via a GraphQL mutation, this won’t happen.
  • If you store data via FQL as well, call the ToDouble() FQL function on it before storing it.

Storing/editing data via the dashboard is fine, as long as you don’t store an integer kind of value in a double field, it’s the only known case that can go wrong when mixing FQL and GraphQL.

I was actually seeding my database with data using the faunadb-migrate tool you guys have which is a javascript tool and most likely using faunadb-js under the hood. Running plan vanilla q.Create commands.

Agreed. I wasn’t expecting the data to be validated as per graphql per-say. What I was hoping was that pulling data out via the graphql interface would attempt a “cast” when I pull it out? Then error out if the cast didn’t work?

It’s interesting when you have data in collections without explicitly columns and column type definitions but are working to pull it out through a strongly typed interface like Graphql, you hit these hurdles.

That makes sense. This seems to be the solution given how things are built now. Just thinking out loud, its unfortunate to have to, everywhere you are putting in a number that should be a double, explicitly casting to a double in case it is a whole number. It would be nice if the user just had to think in “one place” and let the system “automagically” help us. In my mind there are two possiblities:

  • One option, in SQL we have the table definitions. In FaunaDB, maybe have a weak (and optional) collection definition so that validation happens when you put data into fields it has a type for, but otherwise put in whatever you get?
  • Another option, have the system attempt a cast for data being pulled out. Some casts will work and others will not. So a string to a Double will error out, but an int to a double will not, etc…

Anyway these are just initial thoughts.

Gotcha. Will try the ToDouble() solution for now. Thanks!

1 Like