Skip to content

nullable inputs/args are not covered in the docs very well #1236

@South-Paw

Description

@South-Paw

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 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<Response<ISomething>> {
  // ...

  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')"

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions