graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

Better support for `graphql-scalars`

Open charlypoly opened this issue 3 years ago • 5 comments

DX proposal:

{
   // ...
   scalars: "graphql-scalars#clientTypes"
   // ...
}

Map to the primitive (serialized) value, always, and not use the GraphQLScalar or anything from graphql-scalars lib.


Related https://github.com/dotansimha/graphql-code-generator/issues/8412, https://github.com/dotansimha/graphql-code-generator/issues/8296#issuecomment-1256916743

charlypoly avatar Sep 29 '22 12:09 charlypoly

I would also suggest writing in more details, maybe a guide around the whole integration of these two tools as sometimes there is a lot of confusion around them and what's possible and what not

Urigo avatar Sep 29 '22 12:09 Urigo

Fixing this would also obviate https://github.com/dotansimha/graphql-code-generator/issues/7763

lensbart avatar Oct 05 '22 20:10 lensbart

this seems to break usage with noexports and using the generated files in tsconfig include. While it does insert the correct type tsc does not like imports in definition files

sschneider-ihre-pvs avatar Nov 24 '22 14:11 sschneider-ihre-pvs

I know this is in the v5 roadmap, but is there a temporary workaround?

Currently my graphql schema has

scalar DateTime

This generarates

export type Scalars = {
  ID: { input: string; output: string; }
  String: { input: string; output: string; }
  Boolean: { input: boolean; output: boolean; }
  Int: { input: number; output: number; }
  Float: { input: number; output: number; }
  DateTime: { input: any; output: any; }
};

Is there any way to force DateTime to be type string instead of any?

I tried the suggestion here, but it looks like the client preset and the typescript plugin work independently and don't affect each other.

Here's my current codegen configuration:

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  schema: './src/gql/**.graphql',
  documents: ['src/**/*.{ts,tsx}'],
  generates: {
    './src/generated/gql/': {
      preset: 'client',
      presetConfig: {
        fragmentMasking: { unmaskFunctionName: 'getFragmentData' },
      },
    },
  },
  hooks: { afterAllFileWrite: ['prettier --write'] },
};

export default config;

nareshbhatia avatar Jan 21 '24 18:01 nareshbhatia

Ok, figured it out. Had to add a config.scalars in the preset itself:

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  schema: './src/gql/**.graphql',
  documents: ['src/**/*.{ts,tsx}'],
  generates: {
    './src/generated/gql/': {
      preset: 'client',
      presetConfig: {
        // see https://the-guild.dev/graphql/codegen/plugins/presets/preset-client#the-usefragment-helper
        fragmentMasking: { unmaskFunctionName: 'getFragmentData' },
      },
      config: {
        scalars: {
          DateTime: 'string',
        },
      },
    },
  },
  hooks: { afterAllFileWrite: ['prettier --write'] },
};

export default config;

nareshbhatia avatar Jan 21 '24 21:01 nareshbhatia