Graphql date field type must be Output Type but got: undefined

1.4k Views Asked by At

I have a graphql server running with node/expres and ES6, I am trying to migrate to typescript, when I want to do graphql schema I have experiencing some problems with date types. I know that graphql does not contain a native date type, but in my ES6 implementation, I have used graphql-date to suply this limitation.

import {
    GraphQLObjectType,
    GraphQLSchema,
    GraphQLID,
} from 'graphql';

import GraphQLDate from 'graphql-date';

const events = new GraphQLObjectType({
    name: 'events',
    description: 'This represent an Event',
    fields: () => {
        return {
            id: {
                type: GraphQLID,
                resolve(event) {
                    return event.id;
                }
            },
            start: {
                type: GraphQLDate,
                resolve(event) {
                    return event.start;
                }
            }
        };
    }
});

the problem is that in my project with typescript when run server I get this message:

Error: events.start field type must be Output Type but got: undefined.
1

There are 1 best solutions below

0
On

The issue here is that in TypeScript import Foo from 'foo' expects the module 'foo' to export a module.exports.default property. The graphql-date module instead does the classic Node.js pattern of overwriting module.exports.

To support this in TypeScript, you can mix the old and new syntax via:

import GraphQLDate = require('graphql-date');