This repository was archived by the owner on Feb 27, 2023. It is now read-only.
forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScalars.java
More file actions
712 lines (637 loc) · 25.5 KB
/
Scalars.java
File metadata and controls
712 lines (637 loc) · 25.5 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
package graphql;
import graphql.language.BooleanValue;
import graphql.language.FloatValue;
import graphql.language.IntValue;
import graphql.language.StringValue;
import graphql.schema.Coercing;
import graphql.schema.CoercingParseLiteralException;
import graphql.schema.CoercingParseValueException;
import graphql.schema.CoercingSerializeException;
import graphql.schema.GraphQLScalarType;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.UUID;
import static graphql.Assert.assertShouldNeverHappen;
/**
* This contains the implementations of the Scalar types that ship with graphql-java. Some are proscribed
* by the graphql specification (Int, Float, String, Boolean and ID) while others are offer because they are common on
* Java platforms.
*
* For more info see http://graphql.org/learn/schema/#scalar-types and more specifically http://facebook.github.io/graphql/#sec-Scalars
*/
public class Scalars {
private static final BigInteger LONG_MAX = BigInteger.valueOf(Long.MAX_VALUE);
private static final BigInteger LONG_MIN = BigInteger.valueOf(Long.MIN_VALUE);
private static final BigInteger INT_MAX = BigInteger.valueOf(Integer.MAX_VALUE);
private static final BigInteger INT_MIN = BigInteger.valueOf(Integer.MIN_VALUE);
private static final BigInteger BYTE_MAX = BigInteger.valueOf(Byte.MAX_VALUE);
private static final BigInteger BYTE_MIN = BigInteger.valueOf(Byte.MIN_VALUE);
private static final BigInteger SHORT_MAX = BigInteger.valueOf(Short.MAX_VALUE);
private static final BigInteger SHORT_MIN = BigInteger.valueOf(Short.MIN_VALUE);
private static boolean isNumberIsh(Object input) {
return input instanceof Number || input instanceof String;
}
private static String typeName(Object input) {
if (input == null) {
return "null";
}
return input.getClass().getSimpleName();
}
/**
* This represents the "Int" type as defined in the graphql specification : http://facebook.github.io/graphql/#sec-Int
*
* The Int scalar type represents a signed 32‐bit numeric non‐fractional value.
*/
public static final GraphQLScalarType GraphQLInt = new GraphQLScalarType("Int", "Built-in Int", new Coercing<Integer, Integer>() {
private Integer convertImpl(Object input) {
if (input instanceof Integer) {
return (Integer) input;
} else if (isNumberIsh(input)) {
BigDecimal value;
try {
value = new BigDecimal(input.toString());
} catch (NumberFormatException e) {
return null;
}
try {
return value.intValueExact();
} catch (ArithmeticException e) {
return null;
}
} else {
return null;
}
}
@Override
public Integer serialize(Object input) {
Integer result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException(
"Expected type 'Int' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public Integer parseValue(Object input) {
Integer result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException(
"Expected type 'Int' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public Integer parseLiteral(Object input) {
if (!(input instanceof IntValue)) {
throw new CoercingParseLiteralException(
"Expected AST type 'IntValue' but was '" + typeName(input) + "'."
);
}
BigInteger value = ((IntValue) input).getValue();
if (value.compareTo(INT_MIN) < 0 || value.compareTo(INT_MAX) > 0) {
throw new CoercingParseLiteralException(
"Expected value to be in the Integer range but it was '" + value.toString() + "'"
);
}
return value.intValue();
}
});
/**
* This represents the "Float" type as defined in the graphql specification : http://facebook.github.io/graphql/#sec-Float
*
* Note: The Float type in GraphQL is equivalent to Double in Java. (double precision IEEE 754)
*/
public static final GraphQLScalarType GraphQLFloat = new GraphQLScalarType("Float", "Built-in Float", new Coercing<Double, Double>() {
private Double convertImpl(Object input) {
if (isNumberIsh(input)) {
BigDecimal value;
try {
value = new BigDecimal(input.toString());
} catch (NumberFormatException e) {
return null;
}
return value.doubleValue();
} else {
return null;
}
}
@Override
public Double serialize(Object input) {
Double result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException(
"Expected type 'Float' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public Double parseValue(Object input) {
Double result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException(
"Expected type 'Float' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public Double parseLiteral(Object input) {
if (input instanceof IntValue) {
return ((IntValue) input).getValue().doubleValue();
} else if (input instanceof FloatValue) {
return ((FloatValue) input).getValue().doubleValue();
} else {
throw new CoercingParseLiteralException(
"Expected AST type 'IntValue' or 'FloatValue' but was '" + typeName(input) + "'."
);
}
}
});
/**
* This represents the "String" type as defined in the graphql specification : http://facebook.github.io/graphql/#sec-String
*/
public static final GraphQLScalarType GraphQLString = new GraphQLScalarType("String", "Built-in String", new Coercing<String, String>() {
@Override
public String serialize(Object input) {
return input.toString();
}
@Override
public String parseValue(Object input) {
return serialize(input);
}
@Override
public String parseLiteral(Object input) {
if (!(input instanceof StringValue)) {
throw new CoercingParseLiteralException(
"Expected AST type 'IntValue' but was '" + typeName(input) + "'."
);
}
return ((StringValue) input).getValue();
}
});
/**
* This represents the "Boolean" type as defined in the graphql specification : http://facebook.github.io/graphql/#sec-Boolean
*/
public static final GraphQLScalarType GraphQLBoolean = new GraphQLScalarType("Boolean", "Built-in Boolean", new Coercing<Boolean, Boolean>() {
private Boolean convertImpl(Object input) {
if (input instanceof Boolean) {
return (Boolean) input;
} else if (input instanceof String) {
return Boolean.parseBoolean((String) input);
} else if (isNumberIsh(input)) {
BigDecimal value;
try {
value = new BigDecimal(input.toString());
} catch (NumberFormatException e) {
// this should never happen because String is handled above
return assertShouldNeverHappen();
}
return value.compareTo(BigDecimal.ZERO) != 0;
} else {
return null;
}
}
@Override
public Boolean serialize(Object input) {
Boolean result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException(
"Expected type 'Boolean' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public Boolean parseValue(Object input) {
Boolean result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException(
"Expected type 'Boolean' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public Boolean parseLiteral(Object input) {
if (!(input instanceof BooleanValue)) {
throw new CoercingParseLiteralException(
"Expected AST type 'BooleanValue' but was '" + typeName(input) + "'."
);
}
return ((BooleanValue) input).isValue();
}
});
/**
* This represents the "ID" type as defined in the graphql specification : http://facebook.github.io/graphql/#sec-ID
*
* The ID scalar type represents a unique identifier, often used to re-fetch an object or as the key for a cache. The
* ID type is serialized in the same way as a String; however, it is not intended to be human‐readable. While it is
* often numeric, it should always serialize as a String.
*/
public static final GraphQLScalarType GraphQLID = new GraphQLScalarType("ID", "Built-in ID", new Coercing<Object, Object>() {
private String convertImpl(Object input) {
if (input instanceof String) {
return (String) input;
}
if (input instanceof Integer) {
return String.valueOf(input);
}
if (input instanceof Long) {
return String.valueOf(input);
}
if (input instanceof UUID) {
return String.valueOf(input);
}
return null;
}
@Override
public String serialize(Object input) {
String result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException(
"Expected type 'ID' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public String parseValue(Object input) {
String result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException(
"Expected type 'ID' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public String parseLiteral(Object input) {
if (input instanceof StringValue) {
return ((StringValue) input).getValue();
}
if (input instanceof IntValue) {
return ((IntValue) input).getValue().toString();
}
throw new CoercingParseLiteralException(
"Expected AST type 'IntValue' or 'StringValue' but was '" + typeName(input) + "'."
);
}
});
/**
* This represents the "Long" type which is a representation of java.lang.Long
*/
public static final GraphQLScalarType GraphQLLong = new GraphQLScalarType("Long", "Long type", new Coercing<Long, Long>() {
private Long convertImpl(Object input) {
if (input instanceof Long) {
return (Long) input;
} else if (isNumberIsh(input)) {
BigDecimal value;
try {
value = new BigDecimal(input.toString());
} catch (NumberFormatException e) {
return null;
}
try {
return value.longValueExact();
} catch (ArithmeticException e) {
return null;
}
} else {
return null;
}
}
@Override
public Long serialize(Object input) {
Long result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException(
"Expected type 'Long' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public Long parseValue(Object input) {
Long result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException(
"Expected type 'Long' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public Long parseLiteral(Object input) {
if (input instanceof StringValue) {
try {
return Long.parseLong(((StringValue) input).getValue());
} catch (NumberFormatException e) {
throw new CoercingParseLiteralException(
"Expected value to be a Long but it was '" + String.valueOf(input) + "'"
);
}
} else if (input instanceof IntValue) {
BigInteger value = ((IntValue) input).getValue();
if (value.compareTo(LONG_MIN) < 0 || value.compareTo(LONG_MAX) > 0) {
throw new CoercingParseLiteralException(
"Expected value to be in the Long range but it was '" + value.toString() + "'"
);
}
return value.longValue();
}
throw new CoercingParseLiteralException(
"Expected AST type 'IntValue' or 'StringValue' but was '" + typeName(input) + "'."
);
}
});
/**
* This represents the "Short" type which is a representation of java.lang.Short
*/
public static final GraphQLScalarType GraphQLShort = new GraphQLScalarType("Short", "Built-in Short as Int", new Coercing<Short, Short>() {
private Short convertImpl(Object input) {
if (input instanceof Short) {
return (Short) input;
} else if (isNumberIsh(input)) {
BigDecimal value;
try {
value = new BigDecimal(input.toString());
} catch (NumberFormatException e) {
return null;
}
try {
return value.shortValueExact();
} catch (ArithmeticException e) {
return null;
}
} else {
return null;
}
}
@Override
public Short serialize(Object input) {
Short result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException(
"Expected type 'Short' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public Short parseValue(Object input) {
Short result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException(
"Expected type 'Short' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public Short parseLiteral(Object input) {
if (!(input instanceof IntValue)) {
throw new CoercingParseLiteralException(
"Expected AST type 'IntValue' but was '" + typeName(input) + "'."
);
}
BigInteger value = ((IntValue) input).getValue();
if (value.compareTo(SHORT_MIN) < 0 || value.compareTo(SHORT_MAX) > 0) {
throw new CoercingParseLiteralException(
"Expected value to be in the Short range but it was '" + value.toString() + "'"
);
}
return value.shortValue();
}
});
/**
* This represents the "Byte" type which is a representation of java.lang.Byte
*/
public static final GraphQLScalarType GraphQLByte = new GraphQLScalarType("Byte", "Built-in Byte as Int", new Coercing<Byte, Byte>() {
private Byte convertImpl(Object input) {
if (input instanceof Byte) {
return (Byte) input;
} else if (isNumberIsh(input)) {
BigDecimal value;
try {
value = new BigDecimal(input.toString());
} catch (NumberFormatException e) {
return null;
}
try {
return value.byteValueExact();
} catch (ArithmeticException e) {
return null;
}
} else {
return null;
}
}
@Override
public Byte serialize(Object input) {
Byte result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException(
"Expected type 'Byte' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public Byte parseValue(Object input) {
Byte result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException(
"Expected type 'Byte' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public Byte parseLiteral(Object input) {
if (!(input instanceof IntValue)) {
throw new CoercingParseLiteralException(
"Expected AST type 'IntValue' but was '" + typeName(input) + "'."
);
}
BigInteger value = ((IntValue) input).getValue();
if (value.compareTo(BYTE_MIN) < 0 || value.compareTo(BYTE_MAX) > 0) {
throw new CoercingParseLiteralException(
"Expected value to be in the Byte range but it was '" + value.toString() + "'"
);
}
return value.byteValue();
}
});
/**
* This represents the "BigInteger" type which is a representation of java.math.BigInteger
*/
public static final GraphQLScalarType GraphQLBigInteger = new GraphQLScalarType("BigInteger", "Built-in java.math.BigInteger", new Coercing<BigInteger, BigInteger>() {
private BigInteger convertImpl(Object input) {
if (isNumberIsh(input)) {
BigDecimal value;
try {
value = new BigDecimal(input.toString());
} catch (NumberFormatException e) {
return null;
}
try {
return value.toBigIntegerExact();
} catch (ArithmeticException e) {
return null;
}
}
return null;
}
@Override
public BigInteger serialize(Object input) {
BigInteger result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException(
"Expected type 'BigInteger' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public BigInteger parseValue(Object input) {
BigInteger result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException(
"Expected type 'BigInteger' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public BigInteger parseLiteral(Object input) {
if (input instanceof StringValue) {
try {
return new BigDecimal(((StringValue) input).getValue()).toBigIntegerExact();
} catch (NumberFormatException | ArithmeticException e) {
throw new CoercingParseLiteralException(
"Unable to turn AST input into a 'BigInteger' : '" + String.valueOf(input) + "'"
);
}
} else if (input instanceof IntValue) {
return ((IntValue) input).getValue();
} else if (input instanceof FloatValue) {
try {
return ((FloatValue) input).getValue().toBigIntegerExact();
} catch (ArithmeticException e) {
throw new CoercingParseLiteralException(
"Unable to turn AST input into a 'BigInteger' : '" + String.valueOf(input) + "'"
);
}
}
throw new CoercingParseLiteralException(
"Expected AST type 'IntValue', 'StringValue' or 'FloatValue' but was '" + typeName(input) + "'."
);
}
});
/**
* This represents the "BigDecimal" type which is a representation of java.math.BigDecimal
*/
public static final GraphQLScalarType GraphQLBigDecimal = new GraphQLScalarType("BigDecimal", "Built-in java.math.BigDecimal", new Coercing<BigDecimal, BigDecimal>() {
private BigDecimal convertImpl(Object input) {
if (isNumberIsh(input)) {
try {
return new BigDecimal(input.toString());
} catch (NumberFormatException e) {
return null;
}
}
return null;
}
@Override
public BigDecimal serialize(Object input) {
BigDecimal result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException(
"Expected type 'BigDecimal' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public BigDecimal parseValue(Object input) {
BigDecimal result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException(
"Expected type 'BigDecimal' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public BigDecimal parseLiteral(Object input) {
if (input instanceof StringValue) {
try {
return new BigDecimal(((StringValue) input).getValue());
} catch (NumberFormatException e) {
throw new CoercingParseLiteralException(
"Unable to turn AST input into a 'BigDecimal' : '" + String.valueOf(input) + "'"
);
}
} else if (input instanceof IntValue) {
return new BigDecimal(((IntValue) input).getValue());
} else if (input instanceof FloatValue) {
return ((FloatValue) input).getValue();
}
throw new CoercingParseLiteralException(
"Expected AST type 'IntValue', 'StringValue' or 'FloatValue' but was '" + typeName(input) + "'."
);
}
});
/**
* This represents the "Char" type which is a representation of java.lang.Character
*/
public static final GraphQLScalarType GraphQLChar = new GraphQLScalarType("Char", "Built-in Char as Character", new Coercing<Character, Character>() {
private Character convertImpl(Object input) {
if (input instanceof String && ((String) input).length() == 1) {
return ((String) input).charAt(0);
} else if (input instanceof Character) {
return (Character) input;
} else {
return null;
}
}
@Override
public Character serialize(Object input) {
Character result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException(
"Expected type 'Char' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public Character parseValue(Object input) {
Character result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException(
"Expected type 'Char' but was '" + typeName(input) + "'."
);
}
return result;
}
@Override
public Character parseLiteral(Object input) {
if (!(input instanceof StringValue)) {
throw new CoercingParseLiteralException(
"Expected AST type 'StringValue' but was '" + typeName(input) + "'."
);
}
String value = ((StringValue) input).getValue();
if (value.length() != 1) {
throw new CoercingParseLiteralException(
"Empty 'StringValue' provided."
);
}
return value.charAt(0);
}
});
}