MichalLytek / MichalLytek/type-graphql
nullable inputs/args are not covered in the docs very well
- Dominant language
- TypeScript
- Stars
- 8.1k
- Forks
- 672
- PR merge metrics
- No merged PRs in 30d
Description
Firstly, this feels a bit stupid to be noticing after using type-graphql for 2 years but here it goes...
**Describe the issue**
[The docs](https://typegraphql.com/docs/validation.html) seemingly made it clear to me that having nullable was equivalent to an optional type... e.g.
```
@Field({ nullable: true })
averageRating?: number;
```
and/or
```
@InputType()
export class RecipeInput {
@Field()
title: string;
@Field({ nullable: true })
description?: string;
}
```
and this is mostly true for fields but it comes to args/inputs its misleading ... as I've just found out while debugging an issue in my project.
The correct typescript types for this should be
```
@Field({ nullable: true })
averageRating?: number | null;
```
and/or
```
@InputType()
export class RecipeInput {
@Field()
title: string;
@Field({ nullable: true })
description?: string | null;
}
```
I haven't seen this mentioned in the docs (and it feels like I'm stating the obvious) but when using nullable, `null` is a valid input for args/fields/etc and I've just realized a bunch of code with optional chaining is very broken now 😆
**Are you able to make a PR that fix this?**
Would be good to get it confirmed that I'm not crazy in noticing this and it's not already mentioned somewhere I haven't seen... its a bit late where I am and I've also been chasing this issue all afternoon only to realize this now... 😭
**Additional context**
Specifically, I've been working on Query with the following signature...
```
@Query(() => SomeResponse)
async allThings(
@Arg('filter', { nullable: true }) filter?: SomeFilter,
): Promise> {
// ...
console.log(filter?.search); // undefined or SomeFilter right?
}
```
and just realized this is a very valid GQL query for it:
```
{
allThings(filter: null) {
id
}
}
```
and with this, `console.log(filter?.search)` will now error because `"Cannot read properties of null (reading 'search')"`
Contributor guide
Research direction
Start with the validation documentation at https://typegraphql.com/docs/validation.html and compare its nullable field examples with the nullable @Arg and input cases described here. Update the documentation to explain that nullable GraphQL arguments and inputs may receive null, include the corrected TypeScript types, and show the expected handling for a null value.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql, typescript
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100