@@ -117,24 +117,36 @@ 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 toJSON() or valueOf() 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 . toJSON === 'function' ) {
126
+ return value . toJSON ( ) ;
127
+ }
128
+ if ( typeof value . valueOf === 'function' ) {
129
+ return value . valueOf ( ) ;
130
+ }
131
+ }
132
+ return value ;
133
+ }
134
+
135
+ function serializeString ( rawValue : mixed ) : string {
136
+ const value = serializeObject ( rawValue ) ;
137
+
126
138
// Serialize string, boolean and number values to a string, but do not
127
139
// attempt to coerce object, function, symbol, or other types as strings.
128
- if ( typeof result === 'string' ) {
129
- return result ;
140
+ if ( typeof value === 'string' ) {
141
+ return value ;
130
142
}
131
- if ( typeof result === 'boolean' ) {
132
- return result ? 'true' : 'false' ;
143
+ if ( typeof value === 'boolean' ) {
144
+ return value ? 'true' : 'false' ;
133
145
}
134
- if ( isFinite ( result ) ) {
135
- return result . toString ( ) ;
146
+ if ( isFinite ( value ) ) {
147
+ return value . toString ( ) ;
136
148
}
137
- throw new TypeError ( `String cannot represent value: ${ inspect ( value ) } ` ) ;
149
+ throw new TypeError ( `String cannot represent value: ${ inspect ( rawValue ) } ` ) ;
138
150
}
139
151
140
152
function coerceString ( value : mixed ) : string {
@@ -190,18 +202,16 @@ export const GraphQLBoolean = new GraphQLScalarType({
190
202
} ,
191
203
} ) ;
192
204
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 ;
205
+ function serializeID ( rawValue : mixed ) : string {
206
+ const value = serializeObject ( rawValue ) ;
207
+
208
+ if ( typeof value === 'string' ) {
209
+ return value ;
200
210
}
201
- if ( isInteger ( result ) ) {
202
- return String ( result ) ;
211
+ if ( isInteger ( value ) ) {
212
+ return String ( value ) ;
203
213
}
204
- throw new TypeError ( `ID cannot represent value: ${ inspect ( value ) } ` ) ;
214
+ throw new TypeError ( `ID cannot represent value: ${ inspect ( rawValue ) } ` ) ;
205
215
}
206
216
207
217
function coerceID ( value : mixed ) : string {
0 commit comments