1

Is there a way to make fields of a special scalar type to be converted to date objects by graphql-request? Not just alias type, but have Date object, like Apollo can do with resolvers?

Here's my codegen.ts file:

import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
  overwrite: true,
  schema: './my-schema.graphql',
  documents: ['src', '!src/graphql'],
  generates: {
    './schema.graphql': {
      plugins: ['schema-ast'],
    },
    'src/gql/': {
      preset: 'client',
      plugins: [
        'typescript-operations',
      ],
      config: {
        scalars: {
          UUID: 'string',
          LocalDateTime: 'Date', // here: how to convert this scalar to Date?
        },
      },
    },
  },
};

I've tried typescript-resolvers plugin, but it is used to create a resolvers object, which doesn't seem to be of any use for graphql-request.

Spadar Shut
  • 15,111
  • 5
  • 47
  • 54
  • I've only found this so far https://gist.github.com/tomfa/849adb5b7037b9b23ba59bf0d73c801b – Spadar Shut May 09 '23 at 14:58
  • Did you try [graphql-scalars](https://www.npmjs.com/package/graphql-scalars)? – Michel Floyd May 09 '23 at 15:42
  • Yes, what it does is suggests to do some config and add its scalars to a resolvers object. But with `graphql-request` there's nowhere to use these resolvers. – Spadar Shut May 09 '23 at 18:40
  • In my use of graphql scalars I haven't needed to modify resolvers to handle dates. However dates always get packaged as strings in the JSON payload and I need to convert them to dates again in the client. JSON can't carry a javascript date object directly afaict. – Michel Floyd May 09 '23 at 18:53

0 Answers0