forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscalars.js
123 lines (114 loc) · 3.69 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
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
/* @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 * as Kind from '../language/kinds';
// As per the GraphQL Spec, Integers are only treated as valid when a valid
// 32-bit signed integer, providing the broadest support across platforms.
//
// n.b. JavaScript's integers are safe between -(2^53 - 1) and 2^53 - 1 because
// they are internally represented as IEEE 754 doubles.
const MAX_INT = 2147483647;
const MIN_INT = -2147483648;
function coerceInt(value: mixed): ?number {
if (value === '') {
throw new TypeError(
'Int cannot represent non 32-bit signed integer value: (empty string)'
);
}
const num = Number(value);
if (num === num && num <= MAX_INT && num >= MIN_INT) {
return (num < 0 ? Math.ceil : Math.floor)(num);
}
throw new TypeError(
'Int cannot represent non 32-bit signed integer value: ' + String(value)
);
}
export const GraphQLInt = new GraphQLScalarType({
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. ',
serialize: coerceInt,
parseValue: coerceInt,
parseLiteral(ast) {
if (ast.kind === Kind.INT) {
const num = parseInt(ast.value, 10);
if (num <= MAX_INT && num >= MIN_INT) {
return num;
}
}
return null;
}
});
function coerceFloat(value: mixed): ?number {
if (value === '') {
throw new TypeError(
'Float cannot represent non numeric value: (empty string)'
);
}
const num = Number(value);
if (num === num) {
return num;
}
throw new TypeError(
'Float cannot represent non numeric value: ' + String(value)
);
}
export const GraphQLFloat = new GraphQLScalarType({
name: 'Float',
description:
'The `Float` scalar type represents signed double-precision fractional ' +
'values as specified by ' +
'[IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point). ',
serialize: coerceFloat,
parseValue: coerceFloat,
parseLiteral(ast) {
return ast.kind === Kind.FLOAT || ast.kind === Kind.INT ?
parseFloat(ast.value) :
null;
}
});
export const GraphQLString = new GraphQLScalarType({
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.',
serialize: String,
parseValue: String,
parseLiteral(ast) {
return ast.kind === Kind.STRING ? ast.value : null;
}
});
export const GraphQLBoolean = new GraphQLScalarType({
name: 'Boolean',
description: 'The `Boolean` scalar type represents `true` or `false`.',
serialize: Boolean,
parseValue: Boolean,
parseLiteral(ast) {
return ast.kind === Kind.BOOLEAN ? ast.value : null;
}
});
export const GraphQLID = new GraphQLScalarType({
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.',
serialize: String,
parseValue: String,
parseLiteral(ast) {
return ast.kind === Kind.STRING || ast.kind === Kind.INT ?
ast.value :
null;
}
});