Graphql-code-generator: nullable fields are `T | null | undefined` instead of `T | null`

Created on 8 Aug 2019  路  2Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug
nullable fields are T | null | undefined instead of T | null

To Reproduce
Steps to reproduce the behavior:

  1. My GraphQL schema:
type Character {
  name: String!
  nickname: String
}
  1. My codegen.yml config file:
overwrite: true
schema: "http://localhost:4000/graphql"
documents: "src/**/*.graphql"
generates:
  src/generated/graphql.generated.tsx:
    config:
      skipTypename: true
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-react-apollo"

Expected behavior
I expect the generated models nickname property to be T | null:

Expected

type Character = {
  name: Scalars['String'],
  nickname: Maybe<Scalars['String']>,
}

not T | null | undefined:

Actual

type Character = {
  name: Scalars['String'],
  nickname?: Maybe<Scalars['String']>,
}

Environment:

  • OS: macOS Mojave
  • "@graphql-codegen/cli": "^1.5.0",
  • "@graphql-codegen/typescript": "1.5.0",
  • "@graphql-codegen/typescript-operations": "^1.5.0",
  • "@graphql-codegen/typescript-react-apollo": "1.5.0",
  • NodeJS: v11.10.1

Additional context
I saw issue #138 and understand people have different opinions on this. But we also have the config: maybeValue which you can use to set Maybe to be T | null | undefined..
Maybe the models properties could be reverted back to no-undefined and the default for maybeValue could be changed to T | null | undefined...?

Most helpful comment

How did I not see that reading the docs 馃槻

Thanks @grxy !

All 2 comments

@LarsJK I dealt with the recently and the solution was to set avoidOptionals: true in my config. See https://graphql-code-generator.com/docs/plugins/typescript#avoidoptionals-boolean-default-value-false for more info.

How did I not see that reading the docs 馃槻

Thanks @grxy !

Was this page helpful?
0 / 5 - 0 ratings