-
Notifications
You must be signed in to change notification settings - Fork 2k
/
scalars.ts
324 lines (286 loc) · 9.88 KB
/
scalars.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { inspect } from '../jsutils/inspect.js';
import { isObjectLike } from '../jsutils/isObjectLike.js';
import { GraphQLError } from '../error/GraphQLError.js';
import { Kind } from '../language/kinds.js';
import { print } from '../language/printer.js';
import { defaultScalarValueToLiteral } from '../utilities/valueToLiteral.js';
import type { GraphQLNamedType } from './definition.js';
import { GraphQLScalarType } from './definition.js';
/**
* Maximum possible Int value as per GraphQL Spec (32-bit signed integer).
* n.b. This differs from JavaScript's numbers that are IEEE 754 doubles safe up-to 2^53 - 1
* */
export const GRAPHQL_MAX_INT = 2147483647;
/**
* Minimum possible Int value as per GraphQL Spec (32-bit signed integer).
* n.b. This differs from JavaScript's numbers that are IEEE 754 doubles safe starting at -(2^53 - 1)
* */
export const GRAPHQL_MIN_INT = -2147483648;
export const GraphQLInt = new GraphQLScalarType<number>({
name: 'Int',
description:
'The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.',
coerceOutputValue(outputValue) {
const coercedValue = coerceOutputValueObject(outputValue);
if (typeof coercedValue === 'boolean') {
return coercedValue ? 1 : 0;
}
let num = coercedValue;
if (typeof coercedValue === 'string' && coercedValue !== '') {
num = Number(coercedValue);
}
if (typeof num !== 'number' || !Number.isInteger(num)) {
throw new GraphQLError(
`Int cannot represent non-integer value: ${inspect(coercedValue)}`,
);
}
if (num > GRAPHQL_MAX_INT || num < GRAPHQL_MIN_INT) {
throw new GraphQLError(
'Int cannot represent non 32-bit signed integer value: ' +
inspect(coercedValue),
);
}
return num;
},
coerceInputValue(inputValue) {
if (typeof inputValue !== 'number' || !Number.isInteger(inputValue)) {
throw new GraphQLError(
`Int cannot represent non-integer value: ${inspect(inputValue)}`,
);
}
if (inputValue > GRAPHQL_MAX_INT || inputValue < GRAPHQL_MIN_INT) {
throw new GraphQLError(
`Int cannot represent non 32-bit signed integer value: ${inputValue}`,
);
}
return inputValue;
},
coerceInputLiteral(valueNode) {
if (valueNode.kind !== Kind.INT) {
throw new GraphQLError(
`Int cannot represent non-integer value: ${print(valueNode)}`,
{ nodes: valueNode },
);
}
const num = parseInt(valueNode.value, 10);
if (num > GRAPHQL_MAX_INT || num < GRAPHQL_MIN_INT) {
throw new GraphQLError(
`Int cannot represent non 32-bit signed integer value: ${valueNode.value}`,
{ nodes: valueNode },
);
}
return num;
},
valueToLiteral(value) {
if (
typeof value === 'number' &&
Number.isInteger(value) &&
value <= GRAPHQL_MAX_INT &&
value >= GRAPHQL_MIN_INT
) {
return { kind: Kind.INT, value: String(value) };
}
},
});
export const GraphQLFloat = new GraphQLScalarType<number>({
name: 'Float',
description:
'The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).',
coerceOutputValue(outputValue) {
const coercedValue = coerceOutputValueObject(outputValue);
if (typeof coercedValue === 'boolean') {
return coercedValue ? 1 : 0;
}
let num = coercedValue;
if (typeof coercedValue === 'string' && coercedValue !== '') {
num = Number(coercedValue);
}
if (typeof num !== 'number' || !Number.isFinite(num)) {
throw new GraphQLError(
`Float cannot represent non numeric value: ${inspect(coercedValue)}`,
);
}
return num;
},
coerceInputValue(inputValue) {
if (typeof inputValue !== 'number' || !Number.isFinite(inputValue)) {
throw new GraphQLError(
`Float cannot represent non numeric value: ${inspect(inputValue)}`,
);
}
return inputValue;
},
coerceInputLiteral(valueNode) {
if (valueNode.kind !== Kind.FLOAT && valueNode.kind !== Kind.INT) {
throw new GraphQLError(
`Float cannot represent non numeric value: ${print(valueNode)}`,
{ nodes: valueNode },
);
}
return parseFloat(valueNode.value);
},
valueToLiteral(value) {
const literal = defaultScalarValueToLiteral(value);
if (literal.kind === Kind.FLOAT || literal.kind === Kind.INT) {
return literal;
}
},
});
export const GraphQLString = new GraphQLScalarType<string>({
name: 'String',
description:
'The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.',
coerceOutputValue(outputValue) {
const coercedValue = coerceOutputValueObject(outputValue);
// Coerces string, boolean and number values to a string, but do not
// attempt to coerce object, function, symbol, or other types as strings.
if (typeof coercedValue === 'string') {
return coercedValue;
}
if (typeof coercedValue === 'boolean') {
return coercedValue ? 'true' : 'false';
}
if (typeof coercedValue === 'number' && Number.isFinite(coercedValue)) {
return coercedValue.toString();
}
throw new GraphQLError(
`String cannot represent value: ${inspect(outputValue)}`,
);
},
coerceInputValue(inputValue) {
if (typeof inputValue !== 'string') {
throw new GraphQLError(
`String cannot represent a non string value: ${inspect(inputValue)}`,
);
}
return inputValue;
},
coerceInputLiteral(valueNode) {
if (valueNode.kind !== Kind.STRING) {
throw new GraphQLError(
`String cannot represent a non string value: ${print(valueNode)}`,
{ nodes: valueNode },
);
}
return valueNode.value;
},
valueToLiteral(value) {
const literal = defaultScalarValueToLiteral(value);
if (literal.kind === Kind.STRING) {
return literal;
}
},
});
export const GraphQLBoolean = new GraphQLScalarType<boolean>({
name: 'Boolean',
description: 'The `Boolean` scalar type represents `true` or `false`.',
coerceOutputValue(outputValue) {
const coercedValue = coerceOutputValueObject(outputValue);
if (typeof coercedValue === 'boolean') {
return coercedValue;
}
if (Number.isFinite(coercedValue)) {
return coercedValue !== 0;
}
throw new GraphQLError(
`Boolean cannot represent a non boolean value: ${inspect(coercedValue)}`,
);
},
coerceInputValue(inputValue) {
if (typeof inputValue !== 'boolean') {
throw new GraphQLError(
`Boolean cannot represent a non boolean value: ${inspect(inputValue)}`,
);
}
return inputValue;
},
coerceInputLiteral(valueNode) {
if (valueNode.kind !== Kind.BOOLEAN) {
throw new GraphQLError(
`Boolean cannot represent a non boolean value: ${print(valueNode)}`,
{ nodes: valueNode },
);
}
return valueNode.value;
},
valueToLiteral(value) {
const literal = defaultScalarValueToLiteral(value);
if (literal.kind === Kind.BOOLEAN) {
return literal;
}
},
});
export const GraphQLID = new GraphQLScalarType<string>({
name: 'ID',
description:
'The `ID` scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as `"4"`) or integer (such as `4`) input value will be accepted as an ID.',
coerceOutputValue(outputValue) {
const coercedValue = coerceOutputValueObject(outputValue);
if (typeof coercedValue === 'string') {
return coercedValue;
}
if (Number.isInteger(coercedValue)) {
return String(coercedValue);
}
throw new GraphQLError(
`ID cannot represent value: ${inspect(outputValue)}`,
);
},
coerceInputValue(inputValue) {
if (typeof inputValue === 'string') {
return inputValue;
}
if (typeof inputValue === 'number' && Number.isInteger(inputValue)) {
return inputValue.toString();
}
throw new GraphQLError(`ID cannot represent value: ${inspect(inputValue)}`);
},
coerceInputLiteral(valueNode) {
if (valueNode.kind !== Kind.STRING && valueNode.kind !== Kind.INT) {
throw new GraphQLError(
'ID cannot represent a non-string and non-integer value: ' +
print(valueNode),
{ nodes: valueNode },
);
}
return valueNode.value;
},
valueToLiteral(value) {
// ID types can use number values and Int literals.
const stringValue = Number.isInteger(value) ? String(value) : value;
if (typeof stringValue === 'string') {
// Will parse as an IntValue.
return /^-?(?:0|[1-9][0-9]*)$/.test(stringValue)
? { kind: Kind.INT, value: stringValue }
: { kind: Kind.STRING, value: stringValue, block: false };
}
},
});
export const specifiedScalarTypes: ReadonlyArray<GraphQLScalarType> =
Object.freeze([
GraphQLString,
GraphQLInt,
GraphQLFloat,
GraphQLBoolean,
GraphQLID,
]);
export function isSpecifiedScalarType(type: GraphQLNamedType): boolean {
return specifiedScalarTypes.some(({ name }) => type.name === name);
}
// Support coercing objects with custom valueOf() or toJSON() functions -
// a common way to represent a complex value which can be represented as
// a string (ex: MongoDB id objects).
function coerceOutputValueObject(outputValue: unknown): unknown {
if (isObjectLike(outputValue)) {
if (typeof outputValue.valueOf === 'function') {
const valueOfResult = outputValue.valueOf();
if (!isObjectLike(valueOfResult)) {
return valueOfResult;
}
}
if (typeof outputValue.toJSON === 'function') {
return outputValue.toJSON();
}
}
return outputValue;
}