Wrap GraphQL scalar type with a special case resolver

Viewed 235

I have special GraphQL scalar type from a library to represent DateTime objects. However, I have a case where the value '0000-00-00 00:00:00' is returned from MySQL which is says it cannot parse. I essentially want to resolve that always to null.

In my schema I could always put a custom resolver with it similar to this:

import gqlDate from 'graphql-iso-date';

const MyType = new gql.GraphQLObjectType({
  name: 'Type',
  description: 'This is a GraphQL type.',
  fields: () => {
    return {
      datetime: {
        type: gqlDate.GraphQLDateTime,
        description: 'The datetime field of my type.',
        resolve: (record) => {
          return record === '0000-00-00 00:00:00' ? null : record;
        }
      }
    };
  }
});

But I was hoping there was a way to wrap this in a type that could look something like the following:

const CustomDateTimeType = new gql.GraphQLType({
    name: 'CustomDateTime',
    type: gqlDate.DateTimeType,
    resolve: (record) => {
      return record === '0000-00-00 00:00:00' ? null : record;
    }
});

The other way I could think of to do it would be a UnionType but that seems ham-fisted.

0 Answers
Related