Skip to content

Commit

Permalink
fix(types): Prevent numeric overflow for arbitrary-precision types (#123
Browse files Browse the repository at this point in the history
)

* Prevent numeric types from overflowing

* Update tests
  • Loading branch information
benjie committed Dec 3, 2017
1 parent cda42a5 commit d2c1e95
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 99 deletions.
14 changes: 13 additions & 1 deletion packages/graphile-build-pg/src/plugins/PgTypesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,11 @@ export default (function PgTypesPlugin(
}, {});
const categoryLookup = {
B: () => GraphQLBoolean,
N: () => GraphQLFloat,

// Numbers may be too large for GraphQL/JS to handle, so stringify by
// default.
N: () => GraphQLString,

A: type =>
new GraphQLList(
enforceGqlTypeByPgType(pgTypeById[type.arrayItemTypeId])
Expand Down Expand Up @@ -315,16 +319,24 @@ export default (function PgTypesPlugin(
), // bitint - even though this is int8, it's too big for JS int, so cast to string.
"21": GraphQLInt, // int2
"23": GraphQLInt, // int4
"700": GraphQLFloat, // float4
"701": GraphQLFloat, // float8
"1700": GraphQLString, // numeric
"790": GraphQLFloat, // money

"1186": GQLInterval, // interval
"1082": SimpleDate, // date
"1114": SimpleDatetime, // timestamp
"1184": SimpleDatetime, // timestamptz
"1083": SimpleTime, // time
"1266": SimpleTime, // timetz

"114": SimpleJSON, // json
"3802": SimpleJSON, // jsonb
"2950": SimpleUUID, // uuid

"1560": GraphQLString, // bit
"1562": GraphQLString, // varbit
},
pgExtendedTypes && {
"114": GraphQLJSON,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ mutation {
id: 201
smallint: 30
bigint: "467131188225"
numeric: 15.2
decimal: 15.2
numeric: "15.2"
decimal: "15.2"
boolean: false
varchar: "abc"
enum: RED
Expand All @@ -16,7 +16,7 @@ mutation {
json: "{\"x\":1,\"y\":2,\"z\":3}"
jsonb: "{\"a\":1,\"b\":2,\"c\":3}"
numrange: {
start: { value: 50, inclusive: true }
start: { value: "50", inclusive: true }
}
daterange: {
start: { value: "1927-11-05", inclusive: false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Object {
"value": "1927-11-06",
},
},
"decimal": 15.2,
"decimal": "15.2",
"domain": 6,
"domain2": 5,
"enum": "RED",
Expand Down Expand Up @@ -80,12 +80,12 @@ Object {
"bazBuz": 0,
},
"nodeId": "WyJ0eXBlcyIsMjAxXQ==",
"numeric": 15.2,
"numeric": "15.2",
"numrange": Object {
"end": null,
"start": Object {
"inclusive": true,
"value": 50,
"value": "50",
},
},
"smallint": 30,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2475,11 +2475,11 @@ Object {
"numrange": Object {
"end": Object {
"inclusive": false,
"value": 52,
"value": "52",
},
"start": Object {
"inclusive": true,
"value": -10,
"value": "-10",
},
},
"smallint": 50,
Expand Down Expand Up @@ -2550,11 +2550,11 @@ Object {
"numrange": Object {
"end": Object {
"inclusive": false,
"value": 52,
"value": "52",
},
"start": Object {
"inclusive": true,
"value": -10,
"value": "-10",
},
},
"smallint": 50,
Expand Down

0 comments on commit d2c1e95

Please sign in to comment.