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

Input types required filed with default has optional type

Open ziimakc opened this issue 3 years ago • 1 comments

Describe the bug

For schema:

type Query {
    test(
        a: Int # can be null
        b: Int = 1  # can be null if user pass null explicitly
        c: Int! # can't be null
        d: Int! = 1 # can't be null
    ): Int!
}

Generated types are:

export type QueryTestArgs = {
  a?: InputMaybe<Scalars['Int']>;
  b?: InputMaybe<Scalars['Int']>;
  c: Scalars['Int'];
  d?: Scalars['Int']; // should not be optional
};

Your Example Website or App

https://codesandbox.io/s/aged-leftpad-6dcvit

Expected behavior

Generated types are:

export type QueryTestArgs = {
  a?: InputMaybe<Scalars['Int']>;
  b?: InputMaybe<Scalars['Int']>;
  c: Scalars['Int'];
  d: Scalars['Int']; // not optional
};

Platform

"graphql": 16.5.0 "@graphql-codegen/cli": "2.9.1" "@graphql-codegen/typescript": "2.7.2" "@graphql-codegen/typescript-operations": "2.5.2"

Codegen Config File

schema: schema.graphql
documents: document.graphql
generates:
  types.ts:
    plugins:
      - typescript
      - typescript-operations

Additional context

No response

ziimakc avatar Jul 21 '22 16:07 ziimakc

It seems works as expect if you set:

    config:
      avoidOptionals: 
        defaultValue: true

Not sure why it's not behaving the same by default.

ziimakc avatar Jul 22 '22 17:07 ziimakc