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

A way to exculde Maybe<> type from a generated TS schema

Open pid0r-3blan opened this issue 3 years ago • 5 comments

Is your feature request related to a problem? Please describe.

For frontend we set maybeValue: T | undefined in config and in this case it's not clear why we need Maybe type at all, because TypeScript guides usually suggest to prefer optional types over T | undefined.

For example user?: User will work as good as user?: Maybe<User> and of course better than if we specify avoidOptionals: true. With avoidOptionals and typescript's strictNullChecks turned on it'll throw an error if I don't specify user for user: Maybe<User> (which is right from TS perspective, but not right from actual usage cases – if some value is optional, I prefer to not specify it at all then to write { someValue: undefined }).

Describe the solution you'd like

Add to config a way to exclude Maybe type at all from all the optional value types.

Describe alternatives you've considered

No response

Is your feature request related to a problem? Please describe.

No response

pid0r-3blan avatar Jan 26 '23 14:01 pid0r-3blan

I second that. If I have prop: SomeType in my GQL schema, all I need is prop?: SomeType in TS. the inclusion of Maybe here actually annoys me as now I have to have this in my function interfaces: const myfunc = (prop?: SomeType | null): RetType => {}. How annoying is that?! Please give us option to exclude these horrible Maybes from generated TS types!

codan84 avatar Mar 06 '23 15:03 codan84

I second that. If I have prop: SomeType in my GQL schema, all I need is prop?: SomeType in TS. the inclusion of Maybe here actually annoys me as now I have to have this in my function interfaces: const myfunc = (prop?: SomeType | null): RetType => {}. How annoying is that?! Please give us option to exclude these horrible Maybes from generated TS types!

to get rid of the | null in your function signatures you can use as config maybeValue: 'T' . it will still generate Maybe<T> but it will omit the null. eg :

const config: CodegenConfig = {
  overwrite: true,
  schema: './graphql/schema.graphql',
  documents: ['src/**/*.gql'],
  generates: {
    'src/api/types.generated.ts': {
      plugins: ['typescript'],
      config: {
        maybeValue: 'T',
      } 
  }
}

ovy9086 avatar Mar 06 '23 15:03 ovy9086

Hi @ovy9086 , I also have the same problem. Thanks for your suggestion. However, seems like the maybeValue: 'T' doesn't work with preset: 'client'?

nguyenphusi avatar Apr 20 '23 15:04 nguyenphusi

Hi @ovy9086 , I also have the same problem. Thanks for your suggestion. However, seems like the maybeValue: 'T' doesn't work with preset: 'client'?

Also having this problem

lobaak avatar Oct 02 '23 03:10 lobaak

no updates?

plxel avatar Aug 12 '24 14:08 plxel

Up vote for giving an option to exclude unnecessary type wrappers.

Dtesch9 avatar Nov 27 '24 17:11 Dtesch9

Hi all, the original issue is asking for maybeValuein typescript plugin, which is already supported. If you need this for client-preset, please open another issue to help us track 🙏

eddeee888 avatar Feb 20 '25 11:02 eddeee888