@@ -5,27 +5,34 @@ import { serializeObject } from './utilities.js';
5
5
6
6
let warned = false ;
7
7
8
- function isSafeInteger ( val : bigint ) : boolean {
8
+ function isSafeInteger ( val : bigint | number ) : boolean {
9
9
return val <= Number . MAX_SAFE_INTEGER && val >= Number . MIN_SAFE_INTEGER ;
10
10
}
11
11
12
12
function serializeSafeBigInt ( val : bigint ) : bigint | number | string {
13
13
if ( isSafeInteger ( val ) ) {
14
14
return Number ( val ) ;
15
15
}
16
- if ( 'toJSON' in BigInt . prototype ) {
16
+ if ( isBigIntSerializable ( ) ) {
17
17
return val ;
18
18
}
19
- if ( ! warned ) {
20
- warned = true ;
21
- console . warn (
22
- 'By default, BigInts are not serialized to JSON as numbers but instead as strings which may lead an unintegrity in your data. ' +
23
- 'To fix this, you can use "json-bigint-patch" to enable correct serialization for BigInts.' ,
24
- ) ;
25
- }
26
19
return val . toString ( ) ;
27
20
}
28
21
22
+ function isBigIntSerializable ( ) {
23
+ if ( ! ( 'toJSON' in BigInt . prototype ) ) {
24
+ if ( ! warned ) {
25
+ warned = true ;
26
+ console . warn (
27
+ 'By default, BigInts are not serialized to JSON as numbers but instead as strings which may lead an unintegrity in your data. ' +
28
+ 'To fix this, you can use "json-bigint-patch" to enable correct serialization for BigInts.' ,
29
+ ) ;
30
+ }
31
+ return false ;
32
+ }
33
+ return true ;
34
+ }
35
+
29
36
export const GraphQLBigIntConfig : GraphQLScalarTypeConfig <
30
37
bigint | number ,
31
38
bigint | string | number
@@ -73,6 +80,9 @@ export const GraphQLBigIntConfig: GraphQLScalarTypeConfig<
73
80
if ( inputValue . toString ( ) !== bigint . toString ( ) ) {
74
81
throw createGraphQLError ( `BigInt cannot represent value: ${ inputValue } ` ) ;
75
82
}
83
+ if ( ! isSafeInteger ( bigint ) && ! isBigIntSerializable ( ) ) {
84
+ return Number ( bigint . toString ( ) ) ;
85
+ }
76
86
return bigint ;
77
87
} ,
78
88
parseLiteral ( valueNode ) {
@@ -86,6 +96,9 @@ export const GraphQLBigIntConfig: GraphQLScalarTypeConfig<
86
96
if ( strOrBooleanValue . toString ( ) !== bigint . toString ( ) ) {
87
97
throw createGraphQLError ( `BigInt cannot represent value: ${ strOrBooleanValue } ` ) ;
88
98
}
99
+ if ( ! isSafeInteger ( bigint ) && ! isBigIntSerializable ( ) ) {
100
+ return Number ( bigint . toString ( ) ) ;
101
+ }
89
102
return bigint ;
90
103
} ,
91
104
extensions : {
0 commit comments