dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.81k stars 1.32k forks source link

Input fields with defaults should be optional for client and fields without defaults should not #10029

Open AlexanderKulia opened 3 months ago

AlexanderKulia commented 3 months ago

Which packages are impacted by your issue?

@graphql-codegen/typescript-operations, @graphql-codegen/typescript

Describe the bug

I use graphql-codegen to generate client types with an sdk for calling a GraphQL server

input MyInput {
  mandatory: Float!
  optionalWithoutDefault: Float
  optionalWithDefault: Float = Null
}

From client's perspective: optional input fields withouts defaults should not be optional. If the field is omitted from the input object, the server will throw an error because an explicit null is expected. Input fields with defaults can be optional because the server will fill in the default value

I tried using avoidOptionals: { inputValue: true, defaultValue: false }, but inputValue: true seems to take priority and makes all input fields non-optional. Is there a way to achieve the behavior I want?

Your Example Website or App

https://codesandbox.io/p/devbox/competent-moon-xrqsz6?file=%2Ftypes.ts

Steps to Reproduce the Bug or Issue

  1. Open Sandbox
  2. Open types.ts and check MyInput
  3. optionalWithDefault and optionalWithoutDefault fields of MyInput are both optional

Expected behavior

optionalWithDefault is ? optional, but optionalWithoutDefault is not because the client is expected to explicitly use null

export type MyInput = {
  mandatory: Scalars['Float']['input'];
  optionalWithDefault?: InputMaybe<Scalars['Float']['input']>; // you can omit the property entirely
  optionalWithoutDefault: InputMaybe<Scalars['Float']['input']>; // you have to provide null
};

Screenshots or Videos

No response

Platform

Codegen Config File

No response

Additional context

No response