MichalLytek / MichalLytek/type-graphql

Inherited nullable field of ObjectType becomes non-nullable in subclass.

Open
#1,044 3 comments 0 reactions 1 assignee Claimed by @MichalLytek View on GitHub
Need More Info :man_shrugging: Question :grey_question:
Dominant language
TypeScript
Stars
8.1k
Forks
672
PR merge metrics
No merged PRs in 30d

Description

I have an ObjectType declared with a nullable field. An ObjectType declared as a subclass of that ObjectType which defines no additional fields generates a schema in which that field is _non_-nullable.

```
@ObjectType('UserType')
export class UserType extends withBaseEntity(BaseUser) {
@Field({nullable: true})
fullName(): string {
return `${this.firstName} ${this.lastName}`
}

@Field(_type => CustomerType, {nullable: true})
customer?: CustomerType

@Field(type => [String], {nullable: 'items'})
roles: string[];

@Field(_type => CustomerType, {nullable: true})
gatekeeperFor?: CustomerType

gatekeeperForId?: string;
}

@ObjectType('ViewerType')
export class ViewerType extends UserType {
}
```
**Expected Behavior**
In the generated schema, I would expect the gatekeeperFor field on ViewerType to be nullable, however it's non-nullable. Note also:

1. The gatekeeperFor field on UserType is correctly generated as nullable.
2. The customer field on both UserType and ViewerType (which is declared in the same way as gatekeeperFor) is correctly generated as nullable.
3. Declaring the gatekeeperFor field in the ViewerType subclass works around this problem.

**Environment (please complete the following information):**

- OS: MacOS 11.3
- Node 14.17.3
- Package version 1.1.1
- TypeScript 4.4.2

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.