Skip to content

TS: Use both input and output types in scalars parse/serialize #3397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/type/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,8 @@ export class GraphQLScalarType<TInternal = unknown, TExternal = TInternal> {
name: string;
description: Maybe<string>;
specifiedByURL: Maybe<string>;
serialize: GraphQLScalarSerializer<TExternal>;
parseValue: GraphQLScalarValueParser<TInternal>;
serialize: GraphQLScalarSerializer<TExternal, TInternal>;
parseValue: GraphQLScalarValueParser<TInternal, TExternal>;
parseLiteral: GraphQLScalarLiteralParser<TInternal>;
extensions: Readonly<GraphQLScalarTypeExtensions>;
astNode: Maybe<ScalarTypeDefinitionNode>;
Expand All @@ -594,17 +594,19 @@ export class GraphQLScalarType<TInternal = unknown, TExternal = TInternal> {
constructor(config: Readonly<GraphQLScalarTypeConfig<TInternal, TExternal>>) {
const parseValue =
config.parseValue ??
(identityFunc as GraphQLScalarValueParser<TInternal>);
(identityFunc as GraphQLScalarValueParser<TInternal, TExternal>);

this.name = assertName(config.name);
this.description = config.description;
this.specifiedByURL = config.specifiedByURL;
this.serialize =
config.serialize ?? (identityFunc as GraphQLScalarSerializer<TExternal>);
config.serialize ??
(identityFunc as GraphQLScalarSerializer<TExternal, TInternal>);
this.parseValue = parseValue;
this.parseLiteral =
config.parseLiteral ??
((node, variables) => parseValue(valueFromASTUntyped(node, variables)));
((node, variables) =>
parseValue(valueFromASTUntyped(node, variables) as TExternal));
this.extensions = toObjMap(config.extensions);
this.astNode = config.astNode;
this.extensionASTNodes = config.extensionASTNodes ?? [];
Expand Down Expand Up @@ -657,12 +659,12 @@ export class GraphQLScalarType<TInternal = unknown, TExternal = TInternal> {
}
}

export type GraphQLScalarSerializer<TExternal> = (
outputValue: unknown,
export type GraphQLScalarSerializer<TExternal, TInternal> = (
outputValue: TInternal,
) => TExternal;

export type GraphQLScalarValueParser<TInternal> = (
inputValue: unknown,
export type GraphQLScalarValueParser<TInternal, TExternal> = (
inputValue: TExternal,
) => TInternal;

export type GraphQLScalarLiteralParser<TInternal> = (
Expand All @@ -675,9 +677,9 @@ export interface GraphQLScalarTypeConfig<TInternal, TExternal> {
description?: Maybe<string>;
specifiedByURL?: Maybe<string>;
/** Serializes an internal value to include in a response. */
serialize?: GraphQLScalarSerializer<TExternal>;
serialize?: GraphQLScalarSerializer<TExternal, TInternal>;
/** Parses an externally provided value to use as an input. */
parseValue?: GraphQLScalarValueParser<TInternal>;
parseValue?: GraphQLScalarValueParser<TInternal, TExternal>;
/** Parses an externally provided literal value to use as an input. */
parseLiteral?: GraphQLScalarLiteralParser<TInternal>;
extensions?: Maybe<Readonly<GraphQLScalarTypeExtensions>>;
Expand All @@ -687,8 +689,8 @@ export interface GraphQLScalarTypeConfig<TInternal, TExternal> {

interface GraphQLScalarTypeNormalizedConfig<TInternal, TExternal>
extends GraphQLScalarTypeConfig<TInternal, TExternal> {
serialize: GraphQLScalarSerializer<TExternal>;
parseValue: GraphQLScalarValueParser<TInternal>;
serialize: GraphQLScalarSerializer<TExternal, TInternal>;
parseValue: GraphQLScalarValueParser<TInternal, TExternal>;
parseLiteral: GraphQLScalarLiteralParser<TInternal>;
extensions: Readonly<GraphQLScalarTypeExtensions>;
extensionASTNodes: ReadonlyArray<ScalarTypeExtensionNode>;
Expand Down