-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathastFromValue-test.ts
419 lines (348 loc) · 10.6 KB
/
astFromValue-test.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import { expect } from 'chai';
import { describe, it } from 'mocha';
import {
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLList,
GraphQLNonNull,
GraphQLScalarType,
} from '../../type/definition.js';
import {
GraphQLBoolean,
GraphQLFloat,
GraphQLID,
GraphQLInt,
GraphQLString,
} from '../../type/scalars.js';
import { astFromValue } from '../astFromValue.js';
describe('astFromValue', () => {
it('converts boolean values to ASTs', () => {
expect(astFromValue(true, GraphQLBoolean)).to.deep.equal({
kind: 'BooleanValue',
value: true,
});
expect(astFromValue(false, GraphQLBoolean)).to.deep.equal({
kind: 'BooleanValue',
value: false,
});
expect(astFromValue(undefined, GraphQLBoolean)).to.deep.equal(null);
expect(astFromValue(null, GraphQLBoolean)).to.deep.equal({
kind: 'NullValue',
});
expect(astFromValue(0, GraphQLBoolean)).to.deep.equal({
kind: 'BooleanValue',
value: false,
});
expect(astFromValue(1, GraphQLBoolean)).to.deep.equal({
kind: 'BooleanValue',
value: true,
});
expect(astFromValue(0n, GraphQLBoolean)).to.deep.equal({
kind: 'BooleanValue',
value: false,
});
expect(astFromValue(1n, GraphQLBoolean)).to.deep.equal({
kind: 'BooleanValue',
value: true,
});
const NonNullBoolean = new GraphQLNonNull(GraphQLBoolean);
expect(astFromValue(0, NonNullBoolean)).to.deep.equal({
kind: 'BooleanValue',
value: false,
});
});
it('converts Int values to Int ASTs', () => {
expect(astFromValue(-1, GraphQLInt)).to.deep.equal({
kind: 'IntValue',
value: '-1',
});
expect(astFromValue(123.0, GraphQLInt)).to.deep.equal({
kind: 'IntValue',
value: '123',
});
expect(astFromValue(1e4, GraphQLInt)).to.deep.equal({
kind: 'IntValue',
value: '10000',
});
expect(astFromValue(1n, GraphQLInt)).to.deep.equal({
kind: 'IntValue',
value: '1',
});
// GraphQL spec does not allow coercing non-integer values to Int to avoid
// accidental data loss.
expect(() => astFromValue(123.5, GraphQLInt)).to.throw(
'Int cannot represent non-integer value: 123.5',
);
// Note: outside the bounds of 32bit signed int.
expect(() => astFromValue(1e40, GraphQLInt)).to.throw(
'Int cannot represent non 32-bit signed integer value: 1e+40',
);
// Note: outside the bounds of 32bit signed int.
expect(() => astFromValue(9007199254740991, GraphQLInt)).to.throw(
'Int cannot represent non 32-bit signed integer value: 9007199254740991',
);
// Note: outside the bounds of 32bit signed int as BigInt.
expect(() => astFromValue(9007199254740991n, GraphQLInt)).to.throw(
'Int cannot represent non 32-bit signed integer value: 9007199254740991',
);
expect(() => astFromValue(NaN, GraphQLInt)).to.throw(
'Int cannot represent non-integer value: NaN',
);
});
it('converts Float values to Int/Float ASTs', () => {
expect(astFromValue(-1, GraphQLFloat)).to.deep.equal({
kind: 'IntValue',
value: '-1',
});
expect(astFromValue(123.0, GraphQLFloat)).to.deep.equal({
kind: 'IntValue',
value: '123',
});
expect(astFromValue(9007199254740993n, GraphQLFloat)).to.deep.equal({
kind: 'IntValue',
value: '9007199254740993',
});
expect(astFromValue(123.5, GraphQLFloat)).to.deep.equal({
kind: 'FloatValue',
value: '123.5',
});
expect(astFromValue(1e4, GraphQLFloat)).to.deep.equal({
kind: 'IntValue',
value: '10000',
});
expect(astFromValue(1e40, GraphQLFloat)).to.deep.equal({
kind: 'FloatValue',
value: '1e+40',
});
});
it('converts String values to String ASTs', () => {
expect(astFromValue('hello', GraphQLString)).to.deep.equal({
kind: 'StringValue',
value: 'hello',
});
expect(astFromValue('VALUE', GraphQLString)).to.deep.equal({
kind: 'StringValue',
value: 'VALUE',
});
expect(astFromValue('VA\nLUE', GraphQLString)).to.deep.equal({
kind: 'StringValue',
value: 'VA\nLUE',
});
expect(astFromValue(123, GraphQLString)).to.deep.equal({
kind: 'StringValue',
value: '123',
});
expect(astFromValue(9007199254740993n, GraphQLString)).to.deep.equal({
kind: 'StringValue',
value: '9007199254740993',
});
expect(astFromValue(false, GraphQLString)).to.deep.equal({
kind: 'StringValue',
value: 'false',
});
expect(astFromValue(null, GraphQLString)).to.deep.equal({
kind: 'NullValue',
});
expect(astFromValue(undefined, GraphQLString)).to.deep.equal(null);
});
it('converts ID values to Int/String ASTs', () => {
expect(astFromValue('hello', GraphQLID)).to.deep.equal({
kind: 'StringValue',
value: 'hello',
});
expect(astFromValue('VALUE', GraphQLID)).to.deep.equal({
kind: 'StringValue',
value: 'VALUE',
});
// Note: EnumValues cannot contain non-identifier characters
expect(astFromValue('VA\nLUE', GraphQLID)).to.deep.equal({
kind: 'StringValue',
value: 'VA\nLUE',
});
// Note: IntValues are used when possible.
expect(astFromValue(-1, GraphQLID)).to.deep.equal({
kind: 'IntValue',
value: '-1',
});
expect(astFromValue(123, GraphQLID)).to.deep.equal({
kind: 'IntValue',
value: '123',
});
expect(astFromValue('123', GraphQLID)).to.deep.equal({
kind: 'IntValue',
value: '123',
});
expect(astFromValue('01', GraphQLID)).to.deep.equal({
kind: 'StringValue',
value: '01',
});
expect(astFromValue(9007199254740993n, GraphQLID)).to.deep.equal({
kind: 'IntValue',
value: '9007199254740993',
});
expect(() => astFromValue(false, GraphQLID)).to.throw(
'ID cannot represent value: false',
);
expect(astFromValue(null, GraphQLID)).to.deep.equal({ kind: 'NullValue' });
expect(astFromValue(undefined, GraphQLID)).to.deep.equal(null);
});
it('converts using serialize from a custom scalar type', () => {
const passthroughScalar = new GraphQLScalarType({
name: 'PassthroughScalar',
serialize(value) {
return value;
},
});
expect(astFromValue('value', passthroughScalar)).to.deep.equal({
kind: 'StringValue',
value: 'value',
});
expect(() => astFromValue(NaN, passthroughScalar)).to.throw(
'Cannot convert value to AST: NaN.',
);
expect(() => astFromValue(Infinity, passthroughScalar)).to.throw(
'Cannot convert value to AST: Infinity.',
);
const returnNullScalar = new GraphQLScalarType({
name: 'ReturnNullScalar',
serialize() {
return null;
},
});
expect(astFromValue('value', returnNullScalar)).to.equal(null);
class SomeClass {}
const returnCustomClassScalar = new GraphQLScalarType({
name: 'ReturnCustomClassScalar',
serialize() {
return new SomeClass();
},
});
expect(() => astFromValue('value', returnCustomClassScalar)).to.throw(
'Cannot convert value to AST: {}.',
);
});
it('does not converts NonNull values to NullValue', () => {
const NonNullBoolean = new GraphQLNonNull(GraphQLBoolean);
expect(astFromValue(null, NonNullBoolean)).to.deep.equal(null);
});
const complexValue = { someArbitrary: 'complexValue' };
const myEnum = new GraphQLEnumType({
name: 'MyEnum',
values: {
HELLO: {},
GOODBYE: {},
COMPLEX: { value: complexValue },
},
});
it('converts string values to Enum ASTs if possible', () => {
expect(astFromValue('HELLO', myEnum)).to.deep.equal({
kind: 'EnumValue',
value: 'HELLO',
});
expect(astFromValue(complexValue, myEnum)).to.deep.equal({
kind: 'EnumValue',
value: 'COMPLEX',
});
// Note: case sensitive
expect(() => astFromValue('hello', myEnum)).to.throw(
'Enum "MyEnum" cannot represent value: "hello"',
);
// Note: Not a valid enum value
expect(() => astFromValue('UNKNOWN_VALUE', myEnum)).to.throw(
'Enum "MyEnum" cannot represent value: "UNKNOWN_VALUE"',
);
});
it('converts array values to List ASTs', () => {
expect(
astFromValue(['FOO', 'BAR'], new GraphQLList(GraphQLString)),
).to.deep.equal({
kind: 'ListValue',
values: [
{ kind: 'StringValue', value: 'FOO' },
{ kind: 'StringValue', value: 'BAR' },
],
});
expect(
astFromValue(['HELLO', 'GOODBYE'], new GraphQLList(myEnum)),
).to.deep.equal({
kind: 'ListValue',
values: [
{ kind: 'EnumValue', value: 'HELLO' },
{ kind: 'EnumValue', value: 'GOODBYE' },
],
});
function* listGenerator() {
yield 1;
yield 2;
yield 3;
}
expect(
astFromValue(listGenerator(), new GraphQLList(GraphQLInt)),
).to.deep.equal({
kind: 'ListValue',
values: [
{ kind: 'IntValue', value: '1' },
{ kind: 'IntValue', value: '2' },
{ kind: 'IntValue', value: '3' },
],
});
});
it('converts list singletons', () => {
expect(astFromValue('FOO', new GraphQLList(GraphQLString))).to.deep.equal({
kind: 'StringValue',
value: 'FOO',
});
});
it('skip invalid list items', () => {
const ast = astFromValue(
['FOO', null, 'BAR'],
new GraphQLList(new GraphQLNonNull(GraphQLString)),
);
expect(ast).to.deep.equal({
kind: 'ListValue',
values: [
{ kind: 'StringValue', value: 'FOO' },
{ kind: 'StringValue', value: 'BAR' },
],
});
});
const inputObj = new GraphQLInputObjectType({
name: 'MyInputObj',
fields: {
foo: { type: GraphQLFloat },
bar: { type: myEnum },
},
});
it('converts input objects', () => {
expect(astFromValue({ foo: 3, bar: 'HELLO' }, inputObj)).to.deep.equal({
kind: 'ObjectValue',
fields: [
{
kind: 'ObjectField',
name: { kind: 'Name', value: 'foo' },
value: { kind: 'IntValue', value: '3' },
},
{
kind: 'ObjectField',
name: { kind: 'Name', value: 'bar' },
value: { kind: 'EnumValue', value: 'HELLO' },
},
],
});
});
it('converts input objects with explicit nulls', () => {
expect(astFromValue({ foo: null }, inputObj)).to.deep.equal({
kind: 'ObjectValue',
fields: [
{
kind: 'ObjectField',
name: { kind: 'Name', value: 'foo' },
value: { kind: 'NullValue' },
},
],
});
});
it('does not converts non-object values as input objects', () => {
expect(astFromValue(5, inputObj)).to.equal(null);
});
});