Type-graphql: extend class for InputType and ArgsType

Created on 22 May 2020  路  5Comments  路  Source: MichalLytek/type-graphql

Describe the Bug
I have a generic class that I want to use as a base class for both an InputType and an ArgsType, but when extending teh class on the InputType I get Input Object type must define one or more fields.

This is what I have

class PkColumns {
  @Field(() => ID)
  companyId: string;

  @Field(() => ID)
  processId: string;
}

@InputType()
export class PkColumnsInput extends PkColumns {
}

@ArgsType()
export class DeleteOneArgs extends PkColumns {
}

PkColumnsInput and DeleteOneArgs may or may not have more fields

Is that even possible to do?

Question Solved

All 5 comments

https://typegraphql.com/docs/inheritance.html

Note that both the subclass and the parent class must be decorated with the same type of decorator, like @ObjectType() in the example Person -> Student above. Mixing decorator types across parent and child classes is prohibited and might result in a schema building error, e.g. we can't decorate the subclass with @ObjectType() and the parent with @InputType().

However in your case you can try to place both decorators on top of the base class

@ArgsType()
@InputType()
class PkColumns {
 // ...
}

But if the base class needs to be decorated with ObjectType() too, this doesn't work anymore, correct?

Asking because I notice that is very tempting to find a way to share the properties definition between ObjectType and InputType.
Hence why having a base class with no decorators seems a tempting solution, but that doesn't work.

a way to share the properties definition between ObjectType and InputType

https://typegraphql.com/docs/faq.html#situations-frequently-arise-where-inputtype-and-objecttype-have-exactly-the-same-shape-how-can-i-share-the-definitions

Amazing. Thanks! 鉂わ笍

Thanks @MichalLytek that worked!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tongtwist picture tongtwist  路  3Comments

MichalLytek picture MichalLytek  路  3Comments

maplesteve picture maplesteve  路  3Comments

Asim13se picture Asim13se  路  3Comments

winuxue picture winuxue  路  4Comments