-
Notifications
You must be signed in to change notification settings - Fork 134
/
BigInt.ts
113 lines (100 loc) · 3.5 KB
/
BigInt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* eslint-disable @typescript-eslint/ban-types */
import { GraphQLScalarType, GraphQLScalarTypeConfig, print } from 'graphql';
import { createGraphQLError } from '../error.js';
import { serializeObject } from './utilities.js';
let warned = false;
function isSafeInteger(val: bigint | number): boolean {
return val <= Number.MAX_SAFE_INTEGER && val >= Number.MIN_SAFE_INTEGER;
}
function serializeSafeBigInt(val: bigint): bigint | number | string {
if (isSafeInteger(val)) {
return Number(val);
}
if (isBigIntSerializable()) {
return val;
}
return val.toString();
}
function isBigIntSerializable() {
if (!('toJSON' in BigInt.prototype)) {
if (!warned) {
warned = true;
console.warn(
'By default, BigInts are not serialized to JSON as numbers but instead as strings which may lead an unintegrity in your data. ' +
'To fix this, you can use "json-bigint-patch" to enable correct serialization for BigInts.',
);
}
return false;
}
return true;
}
export const GraphQLBigIntConfig: GraphQLScalarTypeConfig<
bigint | number,
bigint | string | number
> = /*#__PURE__*/ {
name: 'BigInt',
description: 'The `BigInt` scalar type represents non-fractional signed whole numeric values.',
serialize(outputValue) {
const coercedValue = serializeObject(outputValue);
let num = coercedValue;
if (typeof coercedValue === 'object' && coercedValue != null && 'toString' in coercedValue) {
num = BigInt(coercedValue.toString());
if (num.toString() !== coercedValue.toString()) {
throw createGraphQLError(`BigInt cannot represent non-integer value: ${coercedValue}`);
}
}
if (typeof coercedValue === 'boolean') {
num = BigInt(coercedValue);
}
if (typeof coercedValue === 'string' && coercedValue !== '') {
num = BigInt(coercedValue);
if (num.toString() !== coercedValue) {
throw createGraphQLError(`BigInt cannot represent non-integer value: ${coercedValue}`);
}
}
if (typeof coercedValue === 'number') {
if (!Number.isInteger(coercedValue)) {
throw createGraphQLError(`BigInt cannot represent non-integer value: ${coercedValue}`);
}
num = BigInt(coercedValue);
}
if (typeof num !== 'bigint') {
throw createGraphQLError(`BigInt cannot represent non-integer value: ${coercedValue}`);
}
return serializeSafeBigInt(num);
},
parseValue(inputValue) {
const bigint = BigInt(inputValue.toString());
if (inputValue.toString() !== bigint.toString()) {
throw createGraphQLError(`BigInt cannot represent value: ${inputValue}`);
}
if (isSafeInteger(bigint) && !isBigIntSerializable()) {
return Number(bigint.toString());
}
return bigint;
},
parseLiteral(valueNode) {
if (!('value' in valueNode)) {
throw createGraphQLError(`BigInt cannot represent non-integer value: ${print(valueNode)}`, {
nodes: valueNode,
});
}
const strOrBooleanValue = valueNode.value;
const bigint = BigInt(strOrBooleanValue);
if (strOrBooleanValue.toString() !== bigint.toString()) {
throw createGraphQLError(`BigInt cannot represent value: ${strOrBooleanValue}`);
}
if (isSafeInteger(bigint) && !isBigIntSerializable()) {
return Number(bigint.toString());
}
return bigint;
},
extensions: {
codegenScalarType: 'bigint',
jsonSchema: {
type: 'integer',
format: 'int64',
},
},
};
export const GraphQLBigInt = /*#__PURE__*/ new GraphQLScalarType(GraphQLBigIntConfig);