-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e6b8a2
commit 25fcb01
Showing
2 changed files
with
35 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters