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?
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
};
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
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 valueI tried using
avoidOptionals: { inputValue: true, defaultValue: false }
, butinputValue: 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
types.ts
and checkMyInput
optionalWithDefault
andoptionalWithoutDefault
fields ofMyInput
are both optionalExpected behavior
optionalWithDefault
is?
optional, butoptionalWithoutDefault
is not because the client is expected to explicitly usenull
Screenshots or Videos
No response
Platform
graphql
version: 16.8.1@graphql-codegen/*
version(s): 3.3.1Codegen Config File
No response
Additional context
No response