Date and Json in type definition for graphql

Have a look at custom scalars: https://www.apollographql.com/docs/graphql-tools/scalars.html

create a new scalar in your schema:

scalar Date
    
type MyType {
    created: Date
}

and create a new resolver:

import { GraphQLScalarType } from 'graphql';
import { Kind } from 'graphql/language';

const resolverMap = {
    Date: new GraphQLScalarType({
        name: 'Date',
        description: 'Date custom scalar type',
        parseValue(value) {
            return new Date(value); // value from the client
        },
        serialize(value) {
            return value.getTime(); // value sent to the client
        },
        parseLiteral(ast) {
            if (ast.kind === Kind.INT) {
            return parseInt(ast.value, 10); // ast value is always in string format
            }
            return null;
        },
    })
};

Leave a Comment