Skip to main content
Version: v2.x

Wide Numeric Types

Databases often support numerical types which accommodate a wider range of numbers than the Number type in JavaScript (and in programming languages broadly). Parsing JSON payloads containing such numbers naively suffers from numerical overflow issues.

For example, The JSON object { "value" : 9223372036854775807}, is still a valid RFC4627 JSON string, but in most JS runtimes, the result of JSON.parse is the object { value: 9223372036854776000 }, where the last 4 digits have been rounded.

In order to deal with this you will need to ensure that clients issuing queries containing large numbers are adequately equipped to handle those. For Javascript, this could mean using a package such as json-bigint.

The Hasura GraphQL Engine also supports optionally representing large number types as strings. You can do this using Hasura Community Edition, Hasura Cloud, and Hasura Enterprise Edition by setting the stringify-numeric-types environment variable.

When this option is set, all values of large numerical type appearing in query results will be represented as strings. It's then up to the caller to interpret these strings numerically as needed.

Note

This only affects the JSON format of query results. Query input fields still use JSON and GraphQL number types, though the BigQuery backend also supports an extra flag to affect input fields.

Using the json-bigint package

The json-bigint package can be used to parse JSON strings containing large numbers:

var JSONbig = require('json-bigint');
var json = '{ "value" : 9223372036854775807, "v2": 123 }';
// Built-in JSON parsing:
var r = JSON.parse(json);

console.log(r.value.toString());
// Prints: 9223372036854776000

console.log(JSON.stringify(r));
// Prints: {"value":9223372036854776000,"v2":123}
// Big number JSON parsing:
var r1 = JSONbig.parse(json);

console.log(r1.value.toString());
// Prints: 9223372036854775807

console.log(JSONbig.stringify(r1));
// Prints: {"value":9223372036854775807,"v2":123}

Source: json-bigint homepage.