Type-graphql: DateTime error in subscription.

Created on 7 Apr 2020  路  2Comments  路  Source: MichalLytek/type-graphql

Im getting an error for DateTime field when subscription gets triggered

{"message":"Unable to serialize value '2020-04-07T09:25:08.132Z' as it's not instance of 'Date'","locations":[{"line":12,"column":3}],"path":["newMessage","sentOn"],"
  extensions":{"code":"INTERNAL_SERVER_ERROR"}}

But this works fine while querying the data from the db and just sending it directly

I solved the issue by using graphql-iso-date and using there GraphQLDateTime scalar.

I wonder whats causing the error. I dont seem to find any difference between the two

unlike TypeGraphQL DateTime

serialize(value: Date) {
    if (!(value instanceof Date)) {
      throw new Error(`Unable to serialize value '${value}' as it's not instance of 'Date'`);
    }
    return value.toISOString();
  },

GraphqlDateTime "graphql-iso-date" DateTime

 serialize (value) {
    if (value instanceof Date) {
      if (validateJSDate(value)) {
        return serializeDateTime(value)
      }
  },


export const serializeDateTime = (dateTime: Date): string => {
  return dateTime.toISOString()
}
Question Solved

Most helpful comment

I dont seem to find any difference

TypeGraphQL DateTime is throwing an error, "graphql-iso-date" DateTime is returning undefined -> null in response.

But this works fine while querying the data from the db and just sending it directly

Maybe you use some redis-based graphql subscription that seralize the payload.

You can see that here:
https://github.com/MichalLytek/type-graphql/blob/395357541cc4ca286a94fcad41a99c87b06df5b9/examples/redis-subscriptions/recipe.resolver.ts#L56-L71

All 2 comments

I dont seem to find any difference

TypeGraphQL DateTime is throwing an error, "graphql-iso-date" DateTime is returning undefined -> null in response.

But this works fine while querying the data from the db and just sending it directly

Maybe you use some redis-based graphql subscription that seralize the payload.

You can see that here:
https://github.com/MichalLytek/type-graphql/blob/395357541cc4ca286a94fcad41a99c87b06df5b9/examples/redis-subscriptions/recipe.resolver.ts#L56-L71

For others ending up here who work with UTC DateTime strings the package graphql-scalars can be used.

Was this page helpful?
0 / 5 - 0 ratings