Skip to content

Commit d9f697b

Browse files
authored
refactor(postgres): use a custom type for big integers (#328)
* use custom string type for big integers * use breaking case for bigint
1 parent edd2e74 commit d9f697b

File tree

9 files changed

+65
-28
lines changed

9 files changed

+65
-28
lines changed

examples/kitchen-sink/data.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ insert into a.foreign_key (person_id, compound_key_1, compound_key_2) values
4040
insert into b.types values (
4141
12,
4242
50,
43-
200,
43+
467131188225,
4444
15.2,
4545
15.2,
4646
true,

src/postgraphql/__tests__/__snapshots__/postgraphqlIntegrationOperations-test.js.snap

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ Object {
633633
},
634634
"start": null,
635635
},
636-
"bigint": 50,
636+
"bigint": "467131188225",
637637
"boolean": false,
638638
"compoundType": Object {
639639
"a": 123,
@@ -666,8 +666,8 @@ Object {
666666
"seconds": 1,
667667
"years": 6,
668668
},
669-
"json": "{"x":1,"y":2,"z":3}",
670-
"jsonb": "{"a":1,"b":2,"c":3}",
669+
"json": "{\"x\":1,\"y\":2,\"z\":3}",
670+
"jsonb": "{\"a\":1,\"b\":2,\"c\":3}",
671671
"money": 5000,
672672
"nestedCompoundType": Object {
673673
"a": Object {
@@ -1677,7 +1677,7 @@ Object {
16771677
],
16781678
},
16791679
"jsonIdentityMutation": Object {
1680-
"json": "{"a":1,"b":2,"c":3}",
1680+
"json": "{\"a\":1,\"b\":2,\"c\":3}",
16811681
},
16821682
"mult1": Object {
16831683
"integer": 0,
@@ -2266,7 +2266,7 @@ Object {
22662266
"value": 20,
22672267
},
22682268
},
2269-
"bigint": 200,
2269+
"bigint": "467131188225",
22702270
"boolean": true,
22712271
"compoundType": Object {
22722272
"a": 1,
@@ -2278,8 +2278,8 @@ Object {
22782278
"domain": 5,
22792279
"domain2": 6,
22802280
"enum": "GREEN",
2281-
"json": "{"a":1,"b":2,"c":3,"d":{"e":4,"f":5,"g":[6,7,8,"x",false,null]}}",
2282-
"jsonb": "{"1":"a","2":"b","3":"c","4":{"5":"d","6":"e","7":["f","g","h",42,true,null]}}",
2281+
"json": "{\"a\":1,\"b\":2,\"c\":3,\"d\":{\"e\":4,\"f\":5,\"g\":[6,7,8,\"x\",false,null]}}",
2282+
"jsonb": "{\"1\":\"a\",\"2\":\"b\",\"3\":\"c\",\"4\":{\"5\":\"d\",\"6\":\"e\",\"7\":[\"f\",\"g\",\"h\",42,true,null]}}",
22832283
"money": 1000,
22842284
"nestedCompoundType": Object {
22852285
"a": Object {

src/postgraphql/__tests__/__snapshots__/postgraphqlIntegrationSchema-test.js.snap

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
exports[`test prints a schema with Relay 1 style ids 1`] = `
2-
"type CompoundKey implements Node {
2+
"# A signed eight-byte integer. The upper big integer values are greater then the
3+
# max value for a JavaScript number. Therefore all big integers will be output as
4+
# strings and not numbers.
5+
scalar BigInt
6+
7+
type CompoundKey implements Node {
38
# A globally unique identifier. Can be used in various places throughout the system to identify this single value.
49
id: ID!
510
personId2: Int!
@@ -809,7 +814,7 @@ type Query implements Node {
809814
# based pagination. May not be used with \`last\`.
810815
offset: Int
811816
): TableSetQueryConnection
812-
typesQuery(a: Int!, b: Boolean!, c: String!, d: [Int]!, e: Json!, f: FloatRangeInput!): Boolean
817+
typesQuery(a: BigInt!, b: Boolean!, c: String!, d: [Int]!, e: Json!, f: FloatRangeInput!): Boolean
813818

814819
# Reads and enables paginatation through a set of \`CompoundKey\`.
815820
allCompoundKeys(
@@ -980,7 +985,7 @@ input TypesMutationInput {
980985
# An arbitrary string value with no semantic meaning. Will be included in the
981986
# payload verbatim. May be used to track mutations by the client.
982987
clientMutationId: String
983-
a: Int!
988+
a: BigInt!
984989
b: Boolean!
985990
c: String!
986991
d: [Int]!
@@ -1170,6 +1175,11 @@ type AuthenticatePayload {
11701175
query: Query
11711176
}
11721177

1178+
# A signed eight-byte integer. The upper big integer values are greater then the
1179+
# max value for a JavaScript number. Therefore all big integers will be output as
1180+
# strings and not numbers.
1181+
scalar BigInt
1182+
11731183
enum Color {
11741184
RED
11751185
GREEN
@@ -1699,7 +1709,7 @@ type Type implements Node {
16991709
nodeId: ID!
17001710
id: Int!
17011711
smallint: Int!
1702-
bigint: Int!
1712+
bigint: BigInt!
17031713
numeric: Float!
17041714
decimal: Float!
17051715
boolean: Boolean!
@@ -1734,7 +1744,7 @@ input TypeCondition {
17341744
smallint: Int
17351745

17361746
# Checks for equality with the object’s \`bigint\` field.
1737-
bigint: Int
1747+
bigint: BigInt
17381748

17391749
# Checks for equality with the object’s \`numeric\` field.
17401750
numeric: Float
@@ -1809,7 +1819,7 @@ input TypeCondition {
18091819
input TypeInput {
18101820
id: Int
18111821
smallint: Int!
1812-
bigint: Int!
1822+
bigint: BigInt!
18131823
numeric: Float!
18141824
decimal: Float!
18151825
boolean: Boolean!
@@ -1839,7 +1849,7 @@ input TypeInput {
18391849
input TypePatch {
18401850
id: Int
18411851
smallint: Int
1842-
bigint: Int
1852+
bigint: BigInt
18431853
numeric: Float
18441854
decimal: Float
18451855
boolean: Boolean
@@ -2222,6 +2232,11 @@ type AuthenticatePayload {
22222232
query: Query
22232233
}
22242234

2235+
# A signed eight-byte integer. The upper big integer values are greater then the
2236+
# max value for a JavaScript number. Therefore all big integers will be output as
2237+
# strings and not numbers.
2238+
scalar BigInt
2239+
22252240
enum Color {
22262241
RED
22272242
GREEN
@@ -4039,7 +4054,7 @@ type Query implements Node {
40394054
# based pagination. May not be used with \`last\`.
40404055
offset: Int
40414056
): TableSetQueryConnection
4042-
typesQuery(a: Int!, b: Boolean!, c: String!, d: [Int]!, e: Json!, f: FloatRangeInput!): Boolean
4057+
typesQuery(a: BigInt!, b: Boolean!, c: String!, d: [Int]!, e: Json!, f: FloatRangeInput!): Boolean
40434058

40444059
# Reads and enables paginatation through a set of \`ForeignKey\`.
40454060
allForeignKeys(
@@ -4583,7 +4598,7 @@ type Type implements Node {
45834598
nodeId: ID!
45844599
id: Int!
45854600
smallint: Int!
4586-
bigint: Int!
4601+
bigint: BigInt!
45874602
numeric: Float!
45884603
decimal: Float!
45894604
boolean: Boolean!
@@ -4618,7 +4633,7 @@ input TypeCondition {
46184633
smallint: Int
46194634

46204635
# Checks for equality with the object’s \`bigint\` field.
4621-
bigint: Int
4636+
bigint: BigInt
46224637

46234638
# Checks for equality with the object’s \`numeric\` field.
46244639
numeric: Float
@@ -4693,7 +4708,7 @@ input TypeCondition {
46934708
input TypeInput {
46944709
id: Int
46954710
smallint: Int!
4696-
bigint: Int!
4711+
bigint: BigInt!
46974712
numeric: Float!
46984713
decimal: Float!
46994714
boolean: Boolean!
@@ -4723,7 +4738,7 @@ input TypeInput {
47234738
input TypePatch {
47244739
id: Int
47254740
smallint: Int
4726-
bigint: Int
4741+
bigint: BigInt
47274742
numeric: Float
47284743
decimal: Float
47294744
boolean: Boolean
@@ -4778,7 +4793,7 @@ input TypesMutationInput {
47784793
# An arbitrary string value with no semantic meaning. Will be included in the
47794794
# payload verbatim. May be used to track mutations by the client.
47804795
clientMutationId: String
4781-
a: Int!
4796+
a: BigInt!
47824797
b: Boolean!
47834798
c: String!
47844799
d: [Int]!
@@ -5166,7 +5181,12 @@ scalar Uuid
51665181
`;
51675182

51685183
exports[`test prints a schema without default mutations 1`] = `
5169-
"type CompoundKey implements Node {
5184+
"# A signed eight-byte integer. The upper big integer values are greater then the
5185+
# max value for a JavaScript number. Therefore all big integers will be output as
5186+
# strings and not numbers.
5187+
scalar BigInt
5188+
5189+
type CompoundKey implements Node {
51705190
# A globally unique identifier. Can be used in various places throughout the system to identify this single value.
51715191
nodeId: ID!
51725192
personId2: Int!
@@ -5689,7 +5709,7 @@ type Query implements Node {
56895709
# based pagination. May not be used with \`last\`.
56905710
offset: Int
56915711
): TableSetQueryConnection
5692-
typesQuery(a: Int!, b: Boolean!, c: String!, d: [Int]!, e: Json!, f: FloatRangeInput!): Boolean
5712+
typesQuery(a: BigInt!, b: Boolean!, c: String!, d: [Int]!, e: Json!, f: FloatRangeInput!): Boolean
56935713

56945714
# Reads and enables paginatation through a set of \`CompoundKey\`.
56955715
allCompoundKeys(
@@ -5860,7 +5880,7 @@ input TypesMutationInput {
58605880
# An arbitrary string value with no semantic meaning. Will be included in the
58615881
# payload verbatim. May be used to track mutations by the client.
58625882
clientMutationId: String
5863-
a: Int!
5883+
a: BigInt!
58645884
b: Boolean!
58655885
c: String!
58665886
d: [Int]!

src/postgraphql/__tests__/fixtures/queries/mutation-create.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mutation {
33
type: {
44
id: 201
55
smallint: 30
6-
bigint: 50
6+
bigint: "467131188225"
77
numeric: 15.2
88
decimal: 15.2
99
boolean: false

src/postgraphql/__tests__/fixtures/queries/procedure-mutation.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mutation {
88
mult2(input: { arg0: 1, arg1: 1 }) { integer }
99
mult3(input: { arg0: 1, arg1: 2 }) { integer }
1010
mult4(input: { arg0: 5, arg1: 2 }) { integer }
11-
typesMutation(input: { a: 50, b: false, c: "xyz", d: [1, 2, 3], e: "{\"a\":1,\"b\":2,\"c\":3}", f: { start: { value: 1, inclusive: false }, end: { value: 5, inclusive: false } } }) { boolean }
11+
typesMutation(input: { a: "50", b: false, c: "xyz", d: [1, 2, 3], e: "{\"a\":1,\"b\":2,\"c\":3}", f: { start: { value: 1, inclusive: false }, end: { value: 5, inclusive: false } } }) { boolean }
1212
compoundTypeMutation(input: { object: { a: 419, b: "easy cheesy baked potatoes", c: RED, fooBar: 8 } }) { compoundType { a b c d fooBar } }
1313
tableMutation(input: { id: 5 }) { post { nodeId id headline authorId } personByAuthorId { id name } postEdge { cursor node { id headline } } }
1414
tableSetMutation(input: {}) { people { name } }

src/postgraphql/__tests__/fixtures/queries/procedure-query.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ query {
33
add2Query(a: 2, b: 2)
44
add3Query(arg1: 5)
55
add4Query(arg0: 1, b: 3)
6-
typesQuery(a: 50, b: false, c: "xyz", d: [1, 2, 3], e: "{\"a\":1,\"b\":2,\"c\":3}", f: { start: { value: 1, inclusive: false }, end: { value: 5, inclusive: false } })
6+
typesQuery(a: "50", b: false, c: "xyz", d: [1, 2, 3], e: "{\"a\":1,\"b\":2,\"c\":3}", f: { start: { value: 1, inclusive: false }, end: { value: 5, inclusive: false } })
77
compoundTypeQuery(object: { a: 419, b: "easy cheesy baked potatoes", c: RED, fooBar: 8 }) { a b c d fooBar }
88
tableQuery(id: 5) { nodeId id headline authorId }
99
tableSetQuery { edges { cursor node { name } } }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pgStringType from '../scalar/pgStringType'
2+
import PgAliasType from '../PgAliasType'
3+
4+
const pgBigIntType = new PgAliasType({
5+
name: 'big_int',
6+
description:
7+
'A signed eight-byte integer. The upper big integer values are greater then ' +
8+
'the max value for a JavaScript number. Therefore all big integers will be ' +
9+
'output as strings and not numbers.',
10+
baseType: pgStringType,
11+
})
12+
13+
export default pgBigIntType

src/postgres/inventory/type/getTypeFromPgType.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import pgIntegerType from './scalar/pgIntegerType'
1010
import pgFloatType from './scalar/pgFloatType'
1111
import pgStringType from './scalar/pgStringType'
1212
import pgJsonType from './scalar/pgJsonType'
13+
import pgBigIntType from './custom/pgBigIntType'
1314
import pgUuidType from './custom/pgUuidType'
1415
import pgDatetimeType from './custom/pgDatetimeType'
1516
import pgDateType from './custom/pgDateType'
@@ -36,7 +37,7 @@ import PgListType from './PgListType'
3637
* @private
3738
*/
3839
const pgTypeIdToType = new Map<string, PgType<mixed>>([
39-
['20', pgIntegerType], // int8, bigint
40+
['20', pgBigIntType], // int8, bigint
4041
['21', pgIntegerType], // int2, smallint
4142
['23', pgIntegerType], // int4, integer
4243
['114', pgJsonType], // json

src/postgres/inventory/type/scalar/pgStringType.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const pgStringType: PgType<string> & AdapterType<string> = {
77
baseType: stringType,
88
isTypeOf: stringType.isTypeOf,
99
transformPgValueIntoValue: value => {
10+
if (typeof value === 'number')
11+
return value.toString(10)
12+
1013
if (!stringType.isTypeOf(value))
1114
throw new Error(`Expected string. Not '${typeof value}'.`)
1215

0 commit comments

Comments
 (0)