dotansimha / dotansimha/graphql-code-generator

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

Open
#10,029 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

### 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

```graphql
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`

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

### Screenshots or Videos

_No response_

### Platform

- OS: macOS, Windows
- NodeJS: v20.11.1
- `graphql` version: 16.8.1
- `@graphql-codegen/*` version(s): 3.3.1

### Codegen Config File

_No response_

### Additional context

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.