Skip to content

Commit

Permalink
fix: Duration scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
willruggiano committed Nov 12, 2024
1 parent 4e6b8a2 commit 25fcb01
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
25 changes: 25 additions & 0 deletions schema/system/resolvers/Duration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { GraphQLError, GraphQLScalarType, Kind } from "graphql";
import z from "myzod";

const parser = z.number({ coerce: true });

export const Duration = new GraphQLScalarType<number, number>({
name: "Duration",
description: "Duration represented in seconds",
serialize: value => parser.parse(value),
parseValue: value => parser.parse(value),
parseLiteral: ast => {
if (ast.kind !== Kind.FLOAT && ast.kind !== Kind.INT) {
throw new GraphQLError(
`Can only validate numerics as Duration but got a: ${ast.kind}`,
{
nodes: ast,
},
);
}
return parser.parse(ast.value);
},
extensions: {
codegenScalarType: "number",
},
});
15 changes: 10 additions & 5 deletions schema/system/scalars/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import type { GraphQLScalarType } from "graphql";
import {
GraphQLDuration,
// GraphQLDuration,
GraphQLLocale,
GraphQLTimeZone,
GraphQLURL,
} from "graphql-scalars";
import { CronExpression } from "../resolvers/CronExpression";
import { Entity } from "../resolvers/Entity";
import { Duration } from "../resolvers/Duration";

export const resolvers: Record<string, GraphQLScalarType> = {
CronExpression: CronExpression,
Duration: GraphQLDuration,
// Duration: GraphQLDuration,
Duration: Duration,
Entity: Entity,
Locale: GraphQLLocale,
TimeZone: GraphQLTimeZone,
Expand All @@ -22,8 +24,10 @@ export {
CronExpression as CronExpressionResolver,
Entity,
Entity as EntityResolver,
GraphQLDuration as Duration,
GraphQLDuration as DurationResolver,
Duration,
Duration as DurationResolver,
// GraphQLDuration as Duration,
// GraphQLDuration as DurationResolver,
GraphQLLocale as Locale,
GraphQLLocale as LocaleResolver,
GraphQLTimeZone as TimeZone,
Expand All @@ -34,7 +38,8 @@ export {

export const config = {
CronExpression: CronExpression.extensions.codegenScalarType,
Duration: GraphQLDuration.extensions.codegenScalarType,
// Duration: GraphQLDuration.extensions.codegenScalarType,
Duration: Duration.extensions.codegenScalarType,
Entity: Entity.extensions.codegenScalarType,
Locale: GraphQLLocale.extensions.codegenScalarType,
TimeZone: GraphQLTimeZone.extensions.codegenScalarType,
Expand Down

0 comments on commit 25fcb01

Please sign in to comment.