@@ -37,7 +37,7 @@ import type { GraphQLSchema } from './schema';
37
37
* These are all of the possible kinds of types.
38
38
*/
39
39
export type GraphQLType =
40
- | GraphQLScalarType
40
+ | GraphQLScalarType < any >
41
41
| GraphQLObjectType
42
42
| GraphQLInterfaceType
43
43
| GraphQLUnionType
@@ -68,12 +68,12 @@ export function assertType(type: mixed): GraphQLType {
68
68
* These types may be used as input types for arguments and directives.
69
69
*/
70
70
type GraphQLInputType_ < T > =
71
- | GraphQLScalarType
71
+ | GraphQLScalarType < any >
72
72
| GraphQLEnumType
73
73
| GraphQLInputObjectType
74
74
| GraphQLList < T >
75
75
| GraphQLNonNull <
76
- | GraphQLScalarType
76
+ | GraphQLScalarType < any >
77
77
| GraphQLEnumType
78
78
| GraphQLInputObjectType
79
79
| GraphQLList < T > ,
@@ -102,14 +102,14 @@ export function assertInputType(type: ?GraphQLType): GraphQLInputType {
102
102
* These types may be used as output types as the result of fields.
103
103
*/
104
104
export type GraphQLOutputType =
105
- | GraphQLScalarType
105
+ | GraphQLScalarType < any >
106
106
| GraphQLObjectType
107
107
| GraphQLInterfaceType
108
108
| GraphQLUnionType
109
109
| GraphQLEnumType
110
110
| GraphQLList < GraphQLOutputType >
111
111
| GraphQLNonNull <
112
- | GraphQLScalarType
112
+ | GraphQLScalarType < any >
113
113
| GraphQLObjectType
114
114
| GraphQLInterfaceType
115
115
| GraphQLUnionType
@@ -140,7 +140,7 @@ export function assertOutputType(type: ?GraphQLType): GraphQLOutputType {
140
140
/**
141
141
* These types may describe types which may be leaf values.
142
142
*/
143
- export type GraphQLLeafType = GraphQLScalarType | GraphQLEnumType ;
143
+ export type GraphQLLeafType = GraphQLScalarType < any > | GraphQLEnumType ;
144
144
145
145
export function isLeafType ( type : ?GraphQLType ) : boolean % checks {
146
146
return type instanceof GraphQLScalarType || type instanceof GraphQLEnumType ;
@@ -201,7 +201,7 @@ export function assertAbstractType(type: ?GraphQLType): GraphQLAbstractType {
201
201
* These types can all accept null as a value.
202
202
*/
203
203
type GraphQLNullableType_ < T > =
204
- | GraphQLScalarType
204
+ | GraphQLScalarType < any >
205
205
| GraphQLObjectType
206
206
| GraphQLInterfaceType
207
207
| GraphQLUnionType
@@ -220,7 +220,7 @@ export function getNullableType<T: GraphQLType>(
220
220
* These named types do not include modifiers like List or NonNull.
221
221
*/
222
222
export type GraphQLNamedType =
223
- | GraphQLScalarType
223
+ | GraphQLScalarType < any >
224
224
| GraphQLObjectType
225
225
| GraphQLInterfaceType
226
226
| GraphQLUnionType
@@ -297,18 +297,18 @@ function resolveThunk<T>(thunk: Thunk<T>): T {
297
297
* } ) ;
298
298
*
299
299
* /
300
- export class GraphQLScalarType {
300
+ export class GraphQLScalarType < TInternal , TExternal = TInternal > {
301
301
name : string ;
302
302
description : ?string ;
303
303
astNode : ?ScalarTypeDefinitionNode ;
304
+ _scalarConfig : GraphQLScalarTypeConfig < TInternal , TExternal > ;
304
305
305
- _scalarConfig : GraphQLScalarTypeConfig < * , * > ;
306
-
307
- constructor ( config : GraphQLScalarTypeConfig < * , * > ) : void {
308
- assertValidName ( config . name ) ;
306
+ constructor ( config : GraphQLScalarTypeConfig < TInternal , TExternal > ) : void {
309
307
this. name = config . name ;
310
308
this . description = config . description ;
311
309
this . astNode = config . astNode ;
310
+ this . _scalarConfig = config ;
311
+ assertValidName ( config . name ) ;
312
312
invariant (
313
313
typeof config . serialize === 'function' ,
314
314
`${ this . name } must provide "serialize" function. If this custom Scalar ` +
@@ -323,11 +323,10 @@ export class GraphQLScalarType {
323
323
'functions.' ,
324
324
) ;
325
325
}
326
- this._scalarConfig = config;
327
326
}
328
327
329
328
// Serializes an internal value to include in a response.
330
- serialize ( value : mixed ) : mixed {
329
+ serialize ( value : mixed ) : TExternal | void {
331
330
const serializer = this . _scalarConfig . serialize ;
332
331
return serializer ( value ) ;
333
332
}
@@ -338,12 +337,11 @@ export class GraphQLScalarType {
338
337
}
339
338
340
339
// Parses an externally provided value to use as an input.
341
- parseValue(value: mixed): mixed {
340
+ parseValue ( value : mixed ) : TInternal | void {
342
341
const parser = this . _scalarConfig . parseValue ;
343
- if ( isInvalid ( value ) ) {
344
- return undefined ;
342
+ if ( ! isInvalid ( value ) ) {
343
+ return parser ? parser ( value ) : ( value : any ) ;
345
344
}
346
- return parser ? parser ( value ) : value ;
347
345
}
348
346
349
347
// Determines if an internal value is valid for this type.
@@ -352,11 +350,14 @@ export class GraphQLScalarType {
352
350
}
353
351
354
352
// Parses an externally provided literal value to use as an input.
355
- parseLiteral(valueNode: ValueNode, variables: ?ObjMap< mixed > ): mixed {
353
+ parseLiteral (
354
+ valueNode : ValueNode ,
355
+ variables : ?ObjMap < mixed > ,
356
+ ) : TInternal | void {
356
357
const parser = this . _scalarConfig . parseLiteral ;
357
358
return parser
358
359
? parser ( valueNode , variables )
359
- : valueFromASTUntyped ( valueNode , variables ) ;
360
+ : ( valueFromASTUntyped ( valueNode , variables ) : any ) ;
360
361
}
361
362
362
363
toString(): string {
@@ -371,16 +372,16 @@ export class GraphQLScalarType {
371
372
GraphQLScalarType . prototype . toJSON = GraphQLScalarType . prototype . inspect =
372
373
GraphQLScalarType . prototype . toString ;
373
374
374
- export type GraphQLScalarTypeConfig < TInternal , TExternal > = {
375
+ export type GraphQLScalarTypeConfig < TInternal , TExternal = TInternal > = {
375
376
name : string ,
376
377
description ?: ?string ,
377
378
astNode ?: ?ScalarTypeDefinitionNode ,
378
- serialize : ( value : mixed ) => ? TExternal ,
379
- parseValue ?: ( value : mixed ) => ? TInternal ,
379
+ serialize : ( value : mixed ) => TExternal | void ,
380
+ parseValue ?: ( value : mixed ) => TInternal | void ,
380
381
parseLiteral ?: (
381
382
valueNode : ValueNode ,
382
383
variables : ?ObjMap < mixed > ,
383
- ) => ? TInternal ,
384
+ ) => TInternal | void ,
384
385
} ;
385
386
386
387
/**
0 commit comments