Skip to content

Instantly share code, notes, and snippets.

@MikeRyanDev
Created June 1, 2018 14:10
Show Gist options
  • Save MikeRyanDev/f0c64f70d017dad0a71e7cf0e7dac003 to your computer and use it in GitHub Desktop.
Save MikeRyanDev/f0c64f70d017dad0a71e7cf0e7dac003 to your computer and use it in GitHub Desktop.

First you have to define the scalar type in the GraphQL schema:

scalar Date

Then you have to provide a resolver for the scalar type. Here's an example implementation using Luxon:

import { DateTime } from 'luxon';

makeExecutableSchema({
  typeDefs: typeDefs,
  resolvers: {
    Date: new GraphQLScalarType({
      name: 'Date',
      description: 'Date custom scalar type',
      parseValue(value) {
        return DateTime.fromISO(value);
      },
      serialize(value) {
        return value.toUTC().toISO();
      },
      parseLiteral(ast) {
        if (ast.kind === Kind.STRING) {
          return DateTime.fromISO(ast.value);
        }
        return null;
      },
    }),
  },
  // Rest of the resolvers / mutators / queries here
});
@jeffwhelpley
Copy link

@MikeRyanDev this makes sense on the server side and works. You also need to add the resolver for Date to the client, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment