diff --git a/src/type/definition.js b/src/type/definition.js index 17d9bc7d066..f013a6ab79b 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -37,7 +37,7 @@ import type { GraphQLSchema } from './schema'; * These are all of the possible kinds of types. */ export type GraphQLType = - | GraphQLScalarType + | GraphQLScalarType | GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType @@ -68,12 +68,12 @@ export function assertType(type: mixed): GraphQLType { * These types may be used as input types for arguments and directives. */ type GraphQLInputType_ = - | GraphQLScalarType + | GraphQLScalarType | GraphQLEnumType | GraphQLInputObjectType | GraphQLList | GraphQLNonNull< - | GraphQLScalarType + | GraphQLScalarType | GraphQLEnumType | GraphQLInputObjectType | GraphQLList, @@ -102,14 +102,14 @@ export function assertInputType(type: ?GraphQLType): GraphQLInputType { * These types may be used as output types as the result of fields. */ export type GraphQLOutputType = - | GraphQLScalarType + | GraphQLScalarType | GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType | GraphQLEnumType | GraphQLList | GraphQLNonNull< - | GraphQLScalarType + | GraphQLScalarType | GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType @@ -140,7 +140,7 @@ export function assertOutputType(type: ?GraphQLType): GraphQLOutputType { /** * These types may describe types which may be leaf values. */ -export type GraphQLLeafType = GraphQLScalarType | GraphQLEnumType; +export type GraphQLLeafType = GraphQLScalarType | GraphQLEnumType; export function isLeafType(type: ?GraphQLType): boolean %checks { return type instanceof GraphQLScalarType || type instanceof GraphQLEnumType; @@ -201,7 +201,7 @@ export function assertAbstractType(type: ?GraphQLType): GraphQLAbstractType { * These types can all accept null as a value. */ type GraphQLNullableType_ = - | GraphQLScalarType + | GraphQLScalarType | GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType @@ -220,7 +220,7 @@ export function getNullableType( * These named types do not include modifiers like List or NonNull. */ export type GraphQLNamedType = - | GraphQLScalarType + | GraphQLScalarType | GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType @@ -297,18 +297,18 @@ function resolveThunk(thunk: Thunk): T { * }); * */ -export class GraphQLScalarType { +export class GraphQLScalarType { name: string; description: ?string; astNode: ?ScalarTypeDefinitionNode; + _scalarConfig: GraphQLScalarTypeConfig; - _scalarConfig: GraphQLScalarTypeConfig<*, *>; - - constructor(config: GraphQLScalarTypeConfig<*, *>): void { - assertValidName(config.name); + constructor(config: GraphQLScalarTypeConfig): void { this.name = config.name; this.description = config.description; this.astNode = config.astNode; + this._scalarConfig = config; + assertValidName(config.name); invariant( typeof config.serialize === 'function', `${this.name} must provide "serialize" function. If this custom Scalar ` + @@ -323,11 +323,10 @@ export class GraphQLScalarType { 'functions.', ); } - this._scalarConfig = config; } // Serializes an internal value to include in a response. - serialize(value: mixed): mixed { + serialize(value: mixed): TExternal | void { const serializer = this._scalarConfig.serialize; return serializer(value); } @@ -338,12 +337,11 @@ export class GraphQLScalarType { } // Parses an externally provided value to use as an input. - parseValue(value: mixed): mixed { + parseValue(value: mixed): TInternal | void { const parser = this._scalarConfig.parseValue; - if (isInvalid(value)) { - return undefined; + if (!isInvalid(value)) { + return parser ? parser(value) : (value: any); } - return parser ? parser(value) : value; } // Determines if an internal value is valid for this type. @@ -352,11 +350,14 @@ export class GraphQLScalarType { } // Parses an externally provided literal value to use as an input. - parseLiteral(valueNode: ValueNode, variables: ?ObjMap): mixed { + parseLiteral( + valueNode: ValueNode, + variables: ?ObjMap, + ): TInternal | void { const parser = this._scalarConfig.parseLiteral; return parser ? parser(valueNode, variables) - : valueFromASTUntyped(valueNode, variables); + : (valueFromASTUntyped(valueNode, variables): any); } toString(): string { @@ -371,16 +372,16 @@ export class GraphQLScalarType { GraphQLScalarType.prototype.toJSON = GraphQLScalarType.prototype.inspect = GraphQLScalarType.prototype.toString; -export type GraphQLScalarTypeConfig = { +export type GraphQLScalarTypeConfig = { name: string, description?: ?string, astNode?: ?ScalarTypeDefinitionNode, - serialize: (value: mixed) => ?TExternal, - parseValue?: (value: mixed) => ?TInternal, + serialize: (value: mixed) => TExternal | void, + parseValue?: (value: mixed) => TInternal | void, parseLiteral?: ( valueNode: ValueNode, variables: ?ObjMap, - ) => ?TInternal, + ) => TInternal | void, }; /** diff --git a/src/type/scalars.js b/src/type/scalars.js index ca3a5447389..bb1a9bdef47 100644 --- a/src/type/scalars.js +++ b/src/type/scalars.js @@ -19,7 +19,7 @@ import type { GraphQLType } from './definition'; const MAX_INT = 2147483647; const MIN_INT = -2147483648; -function coerceInt(value: mixed): ?number { +function coerceInt(value: mixed): number { if (value === '') { throw new TypeError( 'Int cannot represent non 32-bit signed integer value: (empty string)', @@ -58,7 +58,7 @@ export const GraphQLInt = new GraphQLScalarType({ }, }); -function coerceFloat(value: mixed): ?number { +function coerceFloat(value: mixed): number { if (value === '') { throw new TypeError( 'Float cannot represent non numeric value: (empty string)', @@ -88,7 +88,7 @@ export const GraphQLFloat = new GraphQLScalarType({ }, }); -function coerceString(value: mixed): ?string { +function coerceString(value: mixed): string { if (Array.isArray(value)) { throw new TypeError( `String cannot represent an array value: [${String(value)}]`, @@ -137,7 +137,7 @@ export const GraphQLID = new GraphQLScalarType({ }, }); -export const specifiedScalarTypes: Array = [ +export const specifiedScalarTypes = [ GraphQLString, GraphQLInt, GraphQLFloat, diff --git a/src/utilities/buildClientSchema.js b/src/utilities/buildClientSchema.js index 1945557fbaf..cb57afd5de4 100644 --- a/src/utilities/buildClientSchema.js +++ b/src/utilities/buildClientSchema.js @@ -204,7 +204,7 @@ export function buildClientSchema( function buildScalarDef( scalarIntrospection: IntrospectionScalarType, - ): GraphQLScalarType { + ): GraphQLScalarType { return new GraphQLScalarType({ name: scalarIntrospection.name, description: scalarIntrospection.description, diff --git a/src/utilities/schemaPrinter.js b/src/utilities/schemaPrinter.js index bac60d576a5..bb78799194c 100644 --- a/src/utilities/schemaPrinter.js +++ b/src/utilities/schemaPrinter.js @@ -161,7 +161,7 @@ export function printType(type: GraphQLType, options?: Options): string { return printInputObject(type, options); } -function printScalar(type: GraphQLScalarType, options): string { +function printScalar(type: GraphQLScalarType, options): string { return printDescription(options, type) + `scalar ${type.name}`; }