@@ -22,13 +22,14 @@ const MAX_INT = 2147483647;
22
22
const MIN_INT = - 2147483648 ;
23
23
24
24
function serializeInt ( value : mixed ) : number {
25
- if ( Array . isArray ( value ) ) {
26
- throw new TypeError (
27
- `Int cannot represent an array value: ${ inspect ( value ) } ` ,
28
- ) ;
25
+ let num = value ;
26
+ if ( typeof value === 'string' && value !== '' ) {
27
+ num = Number ( value ) ;
28
+ } else if ( typeof value === 'boolean' ) {
29
+ return value ? 1 : 0 ;
29
30
}
30
- const num = Number ( value ) ;
31
- if ( value === '' || ! isInteger ( num ) ) {
31
+
32
+ if ( ! isInteger ( num ) ) {
32
33
throw new TypeError (
33
34
`Int cannot represent non-integer value: ${ inspect ( value ) } ` ,
34
35
) ;
@@ -74,13 +75,14 @@ export const GraphQLInt = new GraphQLScalarType({
74
75
} ) ;
75
76
76
77
function serializeFloat ( value : mixed ) : number {
77
- if ( Array . isArray ( value ) ) {
78
- throw new TypeError (
79
- `Float cannot represent an array value: ${ inspect ( value ) } ` ,
80
- ) ;
78
+ let num = value ;
79
+ if ( typeof value === 'string' && value !== '' ) {
80
+ num = Number ( value ) ;
81
+ } else if ( typeof value === 'boolean' ) {
82
+ return value ? 1 : 0 ;
81
83
}
82
- const num = Number ( value ) ;
83
- if ( value === '' || ! isFinite ( num ) ) {
84
+
85
+ if ( ! isFinite ( num ) ) {
84
86
throw new TypeError (
85
87
`Float cannot represent non numeric value: ${ inspect ( value ) } ` ,
86
88
) ;
@@ -120,14 +122,16 @@ function serializeString(value: mixed): string {
120
122
value && typeof value . valueOf === 'function' ? value . valueOf ( ) : value ;
121
123
// Serialize string, boolean and number values to a string, but do not
122
124
// attempt to coerce object, function, symbol, or other types as strings.
123
- if (
124
- typeof result !== 'string' &&
125
- typeof result !== 'boolean' &&
126
- ! isFinite ( result )
127
- ) {
128
- throw new TypeError ( `String cannot represent value: ${ inspect ( result ) } ` ) ;
129
- }
130
- return String ( result ) ;
125
+ if ( typeof result === 'string' ) {
126
+ return result ;
127
+ }
128
+ if ( typeof result === 'boolean' ) {
129
+ return result ? 'true' : 'false' ;
130
+ }
131
+ if ( isFinite ( result ) ) {
132
+ return result . toString ( ) ;
133
+ }
134
+ throw new TypeError ( `String cannot represent value: ${ inspect ( value ) } ` ) ;
131
135
}
132
136
133
137
function coerceString ( value : mixed ) : string {
@@ -153,12 +157,15 @@ export const GraphQLString = new GraphQLScalarType({
153
157
} ) ;
154
158
155
159
function serializeBoolean ( value : mixed ) : boolean {
156
- if ( typeof value !== 'boolean' && ! isFinite ( value ) ) {
157
- throw new TypeError (
158
- `Boolean cannot represent a non boolean value: ${ inspect ( value ) } ` ,
159
- ) ;
160
+ if ( typeof value === 'boolean' ) {
161
+ return value ;
160
162
}
161
- return Boolean ( value ) ;
163
+ if ( isFinite ( value ) ) {
164
+ return value !== 0 ;
165
+ }
166
+ throw new TypeError (
167
+ `Boolean cannot represent a non boolean value: ${ inspect ( value ) } ` ,
168
+ ) ;
162
169
}
163
170
164
171
function coerceBoolean ( value : mixed ) : boolean {
@@ -185,17 +192,23 @@ function serializeID(value: mixed): string {
185
192
// to represent an object identifier (ex. MongoDB).
186
193
const result =
187
194
value && typeof value . valueOf === 'function' ? value . valueOf ( ) : value ;
188
- if ( typeof result !== 'string' && ! isInteger ( result ) ) {
189
- throw new TypeError ( `ID cannot represent value: ${ inspect ( value ) } ` ) ;
195
+ if ( typeof result === 'string' ) {
196
+ return result ;
190
197
}
191
- return String ( result ) ;
198
+ if ( isInteger ( result ) ) {
199
+ return String ( result ) ;
200
+ }
201
+ throw new TypeError ( `ID cannot represent value: ${ inspect ( value ) } ` ) ;
192
202
}
193
203
194
204
function coerceID ( value : mixed ) : string {
195
- if ( typeof value !== 'string' && ! isInteger ( value ) ) {
196
- throw new TypeError ( `ID cannot represent value: ${ inspect ( value ) } ` ) ;
205
+ if ( typeof value === 'string' ) {
206
+ return value ;
207
+ }
208
+ if ( isInteger ( value ) ) {
209
+ return value . toString ( ) ;
197
210
}
198
- return String ( value ) ;
211
+ throw new TypeError ( `ID cannot represent value: ${ inspect ( value ) } ` ) ;
199
212
}
200
213
201
214
export const GraphQLID = new GraphQLScalarType ( {
0 commit comments