Created
November 21, 2021 08:07
-
-
Save click2install/f6bd270e212aba3efc4601335075a61a to your computer and use it in GitHub Desktop.
[DateTime] - A GraphQL ScalarType for Luxon DateTime
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
import type { GraphQLScalarTypeConfig } from "graphql"; | |
import { GraphQLScalarType } from "graphql"; | |
import { DateTimeResolver } from "graphql-scalars"; | |
import { DateTime } from "luxon"; | |
export const GraphQLDateTimeConfig: GraphQLScalarTypeConfig<DateTime, DateTime> = | |
{ | |
name: "DateTime", | |
description: "An ISO8601 date-time string represented by a Luxon DateTime instance.", | |
serialize(value) | |
{ | |
if (value instanceof DateTime) | |
{ | |
return value; | |
} | |
const date: Date = DateTimeResolver.serialize(value); | |
return DateTime.fromJSDate(date); | |
}, | |
parseValue(value) | |
{ | |
if (value instanceof DateTime) | |
{ | |
return value; | |
} | |
const date: Date = DateTimeResolver.parseValue(value); | |
return DateTime.fromJSDate(date); | |
}, | |
parseLiteral(ast) | |
{ | |
const date: Date = DateTimeResolver.parseLiteral(ast, null); | |
return DateTime.fromJSDate(date); | |
}, | |
extensions: | |
{ | |
codegenScalarType: "DateTime | string" | |
} | |
}; | |
export const LuxonDateTimeResolver = new GraphQLScalarType(GraphQLDateTimeConfig); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment