apollographql / apollographql/apollo-tooling
Typescript: optional TYPE fields are not really optional.
- Dominant language
- TypeScript
- Stars
- 3k
- Forks
- 460
- PR merge metrics
- No merged PRs in 30d
Description
**cli**
Hi,
I saw similar issues, but they are closed and that is not working for me. See below.
GQL schema:
```
type Account {
id: ID!
title: String!
credit: Int
balance: Int
}
type Query {
listAccounts: [Account]
}
```
GQL query:
```
query listAccounts {
listAccounts {
id
title
credit
balance
}
}
```
Generated TS:
```
export interface listAccounts_listAccounts {
id: string;
title: string;
credit: number | null;
balance: number | null;
}
```
Inputs are generated OK, but types aren't. I expect generated TS to look like this:
```
export interface listAccounts_listAccounts {
id: string;
title: string;
credit?: number | null;
balance?: number | null;
}
```
I guess for most users this is not real issue, since INPUTS are generated with ? making fields really optional in typescript. And I guess most users create input objects using generated interfaces, but don't use interfaces from queries to create objects.
I personally use these interfaces to create objects. What I do I re-export this with more friendly name:
```
export type Account = listAccounts_listAccounts;
```
And then I use this to create objects:
```
const acc: Account = {
id: "...",
title: "...",
credit: null,
balance: null
}
```
As you already understand, I don't want to add these non sense NULL fields, I want to omit them and make my code cleaner and easier to write and maintain:
```
const acc: Account = {
id: "...",
title: "..."
}
```
Does this make sense to anybody else? :)
Thanks!
Contributor guide
Research direction
The issue names no source file or test. Start by reproducing the CLI's TypeScript generation with the provided schema and query, then trace the generator responsible for nullable output fields. Done means generated nullable fields can be omitted while retaining their null type, with existing input generation still working.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100