@@ -21,41 +21,42 @@ import { type GraphQLNamedType, GraphQLScalarType } from './definition';
21
21
const MAX_INT = 2147483647 ;
22
22
const MIN_INT = - 2147483648 ;
23
23
24
- function serializeInt ( value : mixed ) : number {
25
- if ( typeof value === 'boolean' ) {
26
- return value ? 1 : 0 ;
24
+ function serializeInt ( outputValue : mixed ) : number {
25
+ if ( typeof outputValue === 'boolean' ) {
26
+ return outputValue ? 1 : 0 ;
27
27
}
28
28
29
- let num = value ;
30
- if ( typeof value === 'string' && value !== '' ) {
31
- num = Number ( value ) ;
29
+ let num = outputValue ;
30
+ if ( typeof outputValue === 'string' && outputValue !== '' ) {
31
+ num = Number ( outputValue ) ;
32
32
}
33
33
34
34
if ( ! isInteger ( num ) ) {
35
35
throw new GraphQLError (
36
- `Int cannot represent non-integer value: ${ inspect ( value ) } ` ,
36
+ `Int cannot represent non-integer value: ${ inspect ( outputValue ) } ` ,
37
37
) ;
38
38
}
39
39
if ( num > MAX_INT || num < MIN_INT ) {
40
40
throw new GraphQLError (
41
- `Int cannot represent non 32-bit signed integer value: ${ inspect ( value ) } ` ,
41
+ 'Int cannot represent non 32-bit signed integer value: ' +
42
+ inspect ( outputValue ) ,
42
43
) ;
43
44
}
44
45
return num ;
45
46
}
46
47
47
- function coerceInt ( value : mixed ) : number {
48
- if ( ! isInteger ( value ) ) {
48
+ function coerceInt ( inputValue : mixed ) : number {
49
+ if ( ! isInteger ( inputValue ) ) {
49
50
throw new GraphQLError (
50
- `Int cannot represent non-integer value: ${ inspect ( value ) } ` ,
51
+ `Int cannot represent non-integer value: ${ inspect ( inputValue ) } ` ,
51
52
) ;
52
53
}
53
- if ( value > MAX_INT || value < MIN_INT ) {
54
+ if ( inputValue > MAX_INT || inputValue < MIN_INT ) {
54
55
throw new GraphQLError (
55
- `Int cannot represent non 32-bit signed integer value: ${ inspect ( value ) } ` ,
56
+ `Int cannot represent non 32-bit signed integer value: ${ inputValue } ` ,
56
57
) ;
57
58
}
58
- return value ;
59
+ return inputValue ;
59
60
}
60
61
61
62
export const GraphQLInt = new GraphQLScalarType ( {
@@ -64,48 +65,48 @@ export const GraphQLInt = new GraphQLScalarType({
64
65
'The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.' ,
65
66
serialize : serializeInt ,
66
67
parseValue : coerceInt ,
67
- parseLiteral ( ast ) {
68
- if ( ast . kind !== Kind . INT ) {
68
+ parseLiteral ( valueNode ) {
69
+ if ( valueNode . kind !== Kind . INT ) {
69
70
throw new GraphQLError (
70
- ' Int cannot represent non-integer value: ' + print ( ast ) ,
71
- ast ,
71
+ ` Int cannot represent non-integer value: ${ print ( valueNode ) } ` ,
72
+ valueNode ,
72
73
) ;
73
74
}
74
- const num = parseInt ( ast . value , 10 ) ;
75
+ const num = parseInt ( valueNode . value , 10 ) ;
75
76
if ( num > MAX_INT || num < MIN_INT ) {
76
77
throw new GraphQLError (
77
- ' Int cannot represent non 32-bit signed integer value: ' + ast . value ,
78
- ast ,
78
+ ` Int cannot represent non 32-bit signed integer value: ${ valueNode . value } ` ,
79
+ valueNode ,
79
80
) ;
80
81
}
81
82
return num ;
82
83
} ,
83
84
} ) ;
84
85
85
- function serializeFloat ( value : mixed ) : number {
86
- if ( typeof value === 'boolean' ) {
87
- return value ? 1 : 0 ;
86
+ function serializeFloat ( outputValue : mixed ) : number {
87
+ if ( typeof outputValue === 'boolean' ) {
88
+ return outputValue ? 1 : 0 ;
88
89
}
89
90
90
- let num = value ;
91
- if ( typeof value === 'string' && value !== '' ) {
92
- num = Number ( value ) ;
91
+ let num = outputValue ;
92
+ if ( typeof outputValue === 'string' && outputValue !== '' ) {
93
+ num = Number ( outputValue ) ;
93
94
}
94
95
if ( ! isFinite ( num ) ) {
95
96
throw new GraphQLError (
96
- `Float cannot represent non numeric value: ${ inspect ( value ) } ` ,
97
+ `Float cannot represent non numeric value: ${ inspect ( outputValue ) } ` ,
97
98
) ;
98
99
}
99
100
return num ;
100
101
}
101
102
102
- function coerceFloat ( value : mixed ) : number {
103
- if ( ! isFinite ( value ) ) {
103
+ function coerceFloat ( inputValue : mixed ) : number {
104
+ if ( ! isFinite ( inputValue ) ) {
104
105
throw new GraphQLError (
105
- `Float cannot represent non numeric value: ${ inspect ( value ) } ` ,
106
+ `Float cannot represent non numeric value: ${ inspect ( inputValue ) } ` ,
106
107
) ;
107
108
}
108
- return value ;
109
+ return inputValue ;
109
110
}
110
111
111
112
export const GraphQLFloat = new GraphQLScalarType ( {
@@ -114,60 +115,62 @@ export const GraphQLFloat = new GraphQLScalarType({
114
115
'The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).' ,
115
116
serialize : serializeFloat ,
116
117
parseValue : coerceFloat ,
117
- parseLiteral ( ast ) {
118
- if ( ast . kind !== Kind . FLOAT && ast . kind !== Kind . INT ) {
118
+ parseLiteral ( valueNode ) {
119
+ if ( valueNode . kind !== Kind . FLOAT && valueNode . kind !== Kind . INT ) {
119
120
throw new GraphQLError (
120
- ' Float cannot represent non numeric value: ' + print ( ast ) ,
121
- ast ,
121
+ ` Float cannot represent non numeric value: ${ print ( valueNode ) } ` ,
122
+ valueNode ,
122
123
) ;
123
124
}
124
- return parseFloat ( ast . value ) ;
125
+ return parseFloat ( valueNode . value ) ;
125
126
} ,
126
127
} ) ;
127
128
128
129
// Support serializing objects with custom valueOf() or toJSON() functions -
129
130
// a common way to represent a complex value which can be represented as
130
131
// a string (ex: MongoDB id objects).
131
- function serializeObject ( value : mixed ) : mixed {
132
- if ( isObjectLike ( value ) ) {
133
- if ( typeof value . valueOf === 'function' ) {
134
- const valueOfResult = value . valueOf ( ) ;
132
+ function serializeObject ( outputValue : mixed ) : mixed {
133
+ if ( isObjectLike ( outputValue ) ) {
134
+ if ( typeof outputValue . valueOf === 'function' ) {
135
+ const valueOfResult = outputValue . valueOf ( ) ;
135
136
if ( ! isObjectLike ( valueOfResult ) ) {
136
137
return valueOfResult ;
137
138
}
138
139
}
139
- if ( typeof value . toJSON === 'function' ) {
140
+ if ( typeof outputValue . toJSON === 'function' ) {
140
141
// $FlowFixMe(>=0.90.0)
141
- return value . toJSON ( ) ;
142
+ return outputValue . toJSON ( ) ;
142
143
}
143
144
}
144
- return value ;
145
+ return outputValue ;
145
146
}
146
147
147
- function serializeString ( rawValue : mixed ) : string {
148
- const value = serializeObject ( rawValue ) ;
148
+ function serializeString ( outputValue : mixed ) : string {
149
+ const coercedValue = serializeObject ( outputValue ) ;
149
150
150
151
// Serialize string, boolean and number values to a string, but do not
151
152
// attempt to coerce object, function, symbol, or other types as strings.
152
- if ( typeof value === 'string' ) {
153
- return value ;
153
+ if ( typeof coercedValue === 'string' ) {
154
+ return coercedValue ;
154
155
}
155
- if ( typeof value === 'boolean' ) {
156
- return value ? 'true' : 'false' ;
156
+ if ( typeof coercedValue === 'boolean' ) {
157
+ return coercedValue ? 'true' : 'false' ;
157
158
}
158
- if ( isFinite ( value ) ) {
159
- return value . toString ( ) ;
159
+ if ( isFinite ( coercedValue ) ) {
160
+ return coercedValue . toString ( ) ;
160
161
}
161
- throw new GraphQLError ( `String cannot represent value: ${ inspect ( rawValue ) } ` ) ;
162
+ throw new GraphQLError (
163
+ `String cannot represent value: ${ inspect ( outputValue ) } ` ,
164
+ ) ;
162
165
}
163
166
164
- function coerceString ( value : mixed ) : string {
165
- if ( typeof value !== 'string' ) {
167
+ function coerceString ( inputValue : mixed ) : string {
168
+ if ( typeof inputValue !== 'string' ) {
166
169
throw new GraphQLError (
167
- `String cannot represent a non string value: ${ inspect ( value ) } ` ,
170
+ `String cannot represent a non string value: ${ inspect ( inputValue ) } ` ,
168
171
) ;
169
172
}
170
- return value ;
173
+ return inputValue ;
171
174
}
172
175
173
176
export const GraphQLString = new GraphQLScalarType ( {
@@ -176,74 +179,74 @@ export const GraphQLString = new GraphQLScalarType({
176
179
'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.' ,
177
180
serialize : serializeString ,
178
181
parseValue : coerceString ,
179
- parseLiteral ( ast ) {
180
- if ( ast . kind !== Kind . STRING ) {
182
+ parseLiteral ( valueNode ) {
183
+ if ( valueNode . kind !== Kind . STRING ) {
181
184
throw new GraphQLError (
182
- ' String cannot represent a non string value: ' + print ( ast ) ,
183
- ast ,
185
+ ` String cannot represent a non string value: ${ print ( valueNode ) } ` ,
186
+ valueNode ,
184
187
) ;
185
188
}
186
- return ast . value ;
189
+ return valueNode . value ;
187
190
} ,
188
191
} ) ;
189
192
190
- function serializeBoolean ( value : mixed ) : boolean {
191
- if ( typeof value === 'boolean' ) {
192
- return value ;
193
+ function serializeBoolean ( outputValue : mixed ) : boolean {
194
+ if ( typeof outputValue === 'boolean' ) {
195
+ return outputValue ;
193
196
}
194
- if ( isFinite ( value ) ) {
195
- return value !== 0 ;
197
+ if ( isFinite ( outputValue ) ) {
198
+ return outputValue !== 0 ;
196
199
}
197
200
throw new GraphQLError (
198
- `Boolean cannot represent a non boolean value: ${ inspect ( value ) } ` ,
201
+ `Boolean cannot represent a non boolean value: ${ inspect ( outputValue ) } ` ,
199
202
) ;
200
203
}
201
204
202
- function coerceBoolean ( value : mixed ) : boolean {
203
- if ( typeof value !== 'boolean' ) {
205
+ function coerceBoolean ( inputValue : mixed ) : boolean {
206
+ if ( typeof inputValue !== 'boolean' ) {
204
207
throw new GraphQLError (
205
- `Boolean cannot represent a non boolean value: ${ inspect ( value ) } ` ,
208
+ `Boolean cannot represent a non boolean value: ${ inspect ( inputValue ) } ` ,
206
209
) ;
207
210
}
208
- return value ;
211
+ return inputValue ;
209
212
}
210
213
211
214
export const GraphQLBoolean = new GraphQLScalarType ( {
212
215
name : 'Boolean' ,
213
216
description : 'The `Boolean` scalar type represents `true` or `false`.' ,
214
217
serialize : serializeBoolean ,
215
218
parseValue : coerceBoolean ,
216
- parseLiteral ( ast ) {
217
- if ( ast . kind !== Kind . BOOLEAN ) {
219
+ parseLiteral ( valueNode ) {
220
+ if ( valueNode . kind !== Kind . BOOLEAN ) {
218
221
throw new GraphQLError (
219
- ' Boolean cannot represent a non boolean value: ' + print ( ast ) ,
220
- ast ,
222
+ ` Boolean cannot represent a non boolean value: ${ print ( valueNode ) } ` ,
223
+ valueNode ,
221
224
) ;
222
225
}
223
- return ast . value ;
226
+ return valueNode . value ;
224
227
} ,
225
228
} ) ;
226
229
227
- function serializeID ( rawValue : mixed ) : string {
228
- const value = serializeObject ( rawValue ) ;
230
+ function serializeID ( outputValue : mixed ) : string {
231
+ const coercedValue = serializeObject ( outputValue ) ;
229
232
230
- if ( typeof value === 'string' ) {
231
- return value ;
233
+ if ( typeof coercedValue === 'string' ) {
234
+ return coercedValue ;
232
235
}
233
- if ( isInteger ( value ) ) {
234
- return String ( value ) ;
236
+ if ( isInteger ( coercedValue ) ) {
237
+ return String ( coercedValue ) ;
235
238
}
236
- throw new GraphQLError ( `ID cannot represent value: ${ inspect ( rawValue ) } ` ) ;
239
+ throw new GraphQLError ( `ID cannot represent value: ${ inspect ( outputValue ) } ` ) ;
237
240
}
238
241
239
- function coerceID ( value : mixed ) : string {
240
- if ( typeof value === 'string' ) {
241
- return value ;
242
+ function coerceID ( inputValue : mixed ) : string {
243
+ if ( typeof inputValue === 'string' ) {
244
+ return inputValue ;
242
245
}
243
- if ( isInteger ( value ) ) {
244
- return value . toString ( ) ;
246
+ if ( isInteger ( inputValue ) ) {
247
+ return inputValue . toString ( ) ;
245
248
}
246
- throw new GraphQLError ( `ID cannot represent value: ${ inspect ( value ) } ` ) ;
249
+ throw new GraphQLError ( `ID cannot represent value: ${ inspect ( inputValue ) } ` ) ;
247
250
}
248
251
249
252
export const GraphQLID = new GraphQLScalarType ( {
@@ -252,14 +255,15 @@ export const GraphQLID = new GraphQLScalarType({
252
255
'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.' ,
253
256
serialize : serializeID ,
254
257
parseValue : coerceID ,
255
- parseLiteral ( ast ) {
256
- if ( ast . kind !== Kind . STRING && ast . kind !== Kind . INT ) {
258
+ parseLiteral ( valueNode ) {
259
+ if ( valueNode . kind !== Kind . STRING && valueNode . kind !== Kind . INT ) {
257
260
throw new GraphQLError (
258
- 'ID cannot represent a non-string and non-integer value: ' + print ( ast ) ,
259
- ast ,
261
+ 'ID cannot represent a non-string and non-integer value: ' +
262
+ print ( valueNode ) ,
263
+ valueNode ,
260
264
) ;
261
265
}
262
- return ast . value ;
266
+ return valueNode . value ;
263
267
} ,
264
268
} ) ;
265
269
0 commit comments