-
Notifications
You must be signed in to change notification settings - Fork 2k
/
scalars.js
73 lines (66 loc) · 1.87 KB
/
scalars.js
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
/* @flow */
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
import { GraphQLScalarType } from './definition';
import { Kind } from '../language';
// Integers are only safe when between -(2^53 - 1) and 2^53 - 1 due to being
// encoded in JavaScript and represented in JSON as double-precision floating
// point numbers, as specified by IEEE 754.
var MAX_INT = 9007199254740991;
var MIN_INT = -9007199254740991;
export var GraphQLInt = new GraphQLScalarType({
name: 'Int',
coerce(value) {
var num = +value;
return num === num && num <= MAX_INT && num >= MIN_INT ? num | 0 : null;
},
coerceLiteral(ast) {
if (ast.kind === Kind.INT) {
var num = parseInt(ast.value, 10);
if (num <= MAX_INT && num >= MIN_INT) {
return num;
}
}
}
});
export var GraphQLFloat = new GraphQLScalarType({
name: 'Float',
coerce(value) {
var num = +value;
return num === num ? num : null;
},
coerceLiteral(ast) {
return ast.kind === Kind.FLOAT || ast.kind === Kind.INT ?
parseFloat(ast.value) :
null;
}
});
export var GraphQLString = new GraphQLScalarType({
name: 'String',
coerce: value => '' + value,
coerceLiteral(ast) {
return ast.kind === Kind.STRING ? ast.value : null;
}
});
export var GraphQLBoolean = new GraphQLScalarType({
name: 'Boolean',
coerce: value => !!value,
coerceLiteral(ast) {
return ast.kind === Kind.BOOLEAN ? ast.value : null;
}
});
export var GraphQLID = new GraphQLScalarType({
name: 'ID',
coerce: value => '' + value,
coerceLiteral(ast) {
return ast.kind === Kind.STRING || ast.kind === Kind.INT ?
ast.value :
null;
}
});