Skip to content

Commit

Permalink
Add type params to GraphQLScalar (#3228)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored Aug 10, 2021
1 parent af878f3 commit 4f21cdc
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 182 deletions.
36 changes: 20 additions & 16 deletions src/type/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,23 +579,27 @@ export interface GraphQLScalarTypeExtensions {
* });
* ```
*/
export class GraphQLScalarType {
export class GraphQLScalarType<TInternal = unknown, TExternal = TInternal> {
name: string;
description: Maybe<string>;
specifiedByURL: Maybe<string>;
serialize: GraphQLScalarSerializer<unknown>;
parseValue: GraphQLScalarValueParser<unknown>;
parseLiteral: GraphQLScalarLiteralParser<unknown>;
serialize: GraphQLScalarSerializer<TExternal>;
parseValue: GraphQLScalarValueParser<TInternal>;
parseLiteral: GraphQLScalarLiteralParser<TInternal>;
extensions: Maybe<Readonly<GraphQLScalarTypeExtensions>>;
astNode: Maybe<ScalarTypeDefinitionNode>;
extensionASTNodes: ReadonlyArray<ScalarTypeExtensionNode>;

constructor(config: Readonly<GraphQLScalarTypeConfig<unknown, unknown>>) {
const parseValue = config.parseValue ?? identityFunc;
constructor(config: Readonly<GraphQLScalarTypeConfig<TInternal, TExternal>>) {
const parseValue =
config.parseValue ??
(identityFunc as GraphQLScalarValueParser<TInternal>);

this.name = config.name;
this.description = config.description;
this.specifiedByURL = config.specifiedByURL;
this.serialize = config.serialize ?? identityFunc;
this.serialize =
config.serialize ?? (identityFunc as GraphQLScalarSerializer<TExternal>);
this.parseValue = parseValue;
this.parseLiteral =
config.parseLiteral ??
Expand Down Expand Up @@ -627,7 +631,7 @@ export class GraphQLScalarType {
}
}

toConfig(): GraphQLScalarTypeNormalizedConfig {
toConfig(): GraphQLScalarTypeNormalizedConfig<TInternal, TExternal> {
return {
name: this.name,
description: this.description,
Expand Down Expand Up @@ -656,16 +660,16 @@ export class GraphQLScalarType {

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

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

export type GraphQLScalarLiteralParser<TInternal> = (
valueNode: ValueNode,
variables?: Maybe<ObjMap<unknown>>,
) => Maybe<TInternal>;
) => TInternal;

export interface GraphQLScalarTypeConfig<TInternal, TExternal> {
name: string;
Expand All @@ -682,11 +686,11 @@ export interface GraphQLScalarTypeConfig<TInternal, TExternal> {
extensionASTNodes?: Maybe<ReadonlyArray<ScalarTypeExtensionNode>>;
}

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

0 comments on commit 4f21cdc

Please sign in to comment.