@@ -117,24 +117,39 @@ export const GraphQLFloat = new GraphQLScalarType({
117
117
} ,
118
118
} ) ;
119
119
120
- function serializeString ( value : mixed ) : string {
121
- // Support serializing objects with custom valueOf() functions - a common way
122
- // to represent an complex value which can be represented as a string
123
- // (ex: MongoDB id objects).
124
- const result =
125
- value && typeof value . valueOf === 'function' ? value . valueOf ( ) : value ;
120
+ // Support serializing objects with custom valueOf() or toJSON() functions -
121
+ // a common way to represent a complex value which can be represented as
122
+ // a string (ex: MongoDB id objects).
123
+ function serializeObject ( value : mixed ) : mixed {
124
+ if ( typeof value === 'object' && value !== null ) {
125
+ if ( typeof value . valueOf === 'function' ) {
126
+ const valueOfResult = value . valueOf ( ) ;
127
+ if ( typeof valueOfResult !== 'object' ) {
128
+ return valueOfResult ;
129
+ }
130
+ }
131
+ if ( typeof value . toJSON === 'function' ) {
132
+ return value . toJSON ( ) ;
133
+ }
134
+ }
135
+ return value ;
136
+ }
137
+
138
+ function serializeString ( rawValue : mixed ) : string {
139
+ const value = serializeObject ( rawValue ) ;
140
+
126
141
// Serialize string, boolean and number values to a string, but do not
127
142
// attempt to coerce object, function, symbol, or other types as strings.
128
- if ( typeof result === 'string' ) {
129
- return result ;
143
+ if ( typeof value === 'string' ) {
144
+ return value ;
130
145
}
131
- if ( typeof result === 'boolean' ) {
132
- return result ? 'true' : 'false' ;
146
+ if ( typeof value === 'boolean' ) {
147
+ return value ? 'true' : 'false' ;
133
148
}
134
- if ( isFinite ( result ) ) {
135
- return result . toString ( ) ;
149
+ if ( isFinite ( value ) ) {
150
+ return value . toString ( ) ;
136
151
}
137
- throw new TypeError ( `String cannot represent value: ${ inspect ( value ) } ` ) ;
152
+ throw new TypeError ( `String cannot represent value: ${ inspect ( rawValue ) } ` ) ;
138
153
}
139
154
140
155
function coerceString ( value : mixed ) : string {
@@ -190,18 +205,16 @@ export const GraphQLBoolean = new GraphQLScalarType({
190
205
} ,
191
206
} ) ;
192
207
193
- function serializeID ( value : mixed ) : string {
194
- // Support serializing objects with custom valueOf() functions - a common way
195
- // to represent an object identifier (ex. MongoDB).
196
- const result =
197
- value && typeof value . valueOf === 'function' ? value . valueOf ( ) : value ;
198
- if ( typeof result === 'string' ) {
199
- return result ;
208
+ function serializeID ( rawValue : mixed ) : string {
209
+ const value = serializeObject ( rawValue ) ;
210
+
211
+ if ( typeof value === 'string' ) {
212
+ return value ;
200
213
}
201
- if ( isInteger ( result ) ) {
202
- return String ( result ) ;
214
+ if ( isInteger ( value ) ) {
215
+ return String ( value ) ;
203
216
}
204
- throw new TypeError ( `ID cannot represent value: ${ inspect ( value ) } ` ) ;
217
+ throw new TypeError ( `ID cannot represent value: ${ inspect ( rawValue ) } ` ) ;
205
218
}
206
219
207
220
function coerceID ( value : mixed ) : string {
0 commit comments