Graphql custom query error: Can't convert '9' to double

After some digging, this is expected behavior. Note that Fauna stores numbers as either an integer or a floating point number, and that those are distinct types, even though Javascript makes a mess of things.

I can replicate the issue when I store an integer value, which can happen in a couple of different ways. Again, I think that some of that is Javascript’s fault.

Use ToDouble to set the value with FQL

If I create a Document directly, with the JS driver or with the Dashboard, and type in a whole number (or a value with a zero decimal like 9.0) it gets stored in the driver then serialized into JSON without the decimal. When it gets to Fauna it is treated as an integer and stored that way. Fauna does not enforce any schema on your documents (yet, we are working on optional schema enforcement), so it is working correctly when it accepts an integer value and stores an integer value.

When GraphQL fetches the data it expects a Float but receives an Int – the GraphQL server is working correctly, and you receive an error.

Now, if I create a Document directly with JS or the Dashboard and wrap the value in ToDouble then we explicitly tell Fauna to store the value as a double-precision floating point number. When GraphQL fetches the data it expects a Float and receives a float.

Setting the values in Graphql should be fine

If I create a document through GraphQL and type in a value like 9 into the query, it gets serialized correctly as a double. The GraphQL server knows more about the intended type during serialization, because you specified it in the schema.

This means that if your application only works through the GraphQL API, you should be fine. If there are any custom resolvers that set these float values with FQL, then I suggest using ToDouble

Migrating existing integer values

You should be able to migrate your data by updating all documents and setting the Float values to themselves wrapped in ToDouble

Update(Ref(Collection("goals"), "101"), {
  data: {
    sortOrder: ToDouble(Var("previous_sortOrder"))
  }
})
1 Like