Skip to content

Instantly share code, notes, and snippets.

@click2install
Created November 21, 2021 08:07
Show Gist options
  • Save click2install/f6bd270e212aba3efc4601335075a61a to your computer and use it in GitHub Desktop.
Save click2install/f6bd270e212aba3efc4601335075a61a to your computer and use it in GitHub Desktop.
[DateTime] - A GraphQL ScalarType for Luxon DateTime
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