Is your feature request related to a problem? Please describe.
@InputType({ implements: IFoo })
@baronTommy
Unfortunatelly, GraphQL spec doesn't support implementing interfaces by input types. Only object types can implement graphql interfaces:
type Foo implements IFoo & IBar {
# ...
}
However, you can use TS interfaces for achieving type safety:
interface IFoo {
foo: string;
}
@InputType()
class FooInput implements IFoo {
@Field()
foo: string;
}
@InputType()
class AddTask {
@Field()
title: string;
@Field()
body?: string;
}
@InputType()
class UpdateTask {
@Field()
id: number;
@Field()
title: string;
@Field()
body?: string;
}
↓
Without duplication
I want to do this
If you can not
I give up 🍎
↓
@InterfaceType()
abstract class IBase {
@Field()
title: string;
@Field()
body?: string;
}
@InputType({ implements: IBase })
class AddTask {
}
@InputType({ implements: IBase })
class UpdateTask {
@Field()
id: number;
}
@baronTommy Just use extends and inheritance:
@InterfaceType()
abstract class IBase {
@Field()
title: string;
@Field()
body?: string;
}
@InputType()
class UpdateTask extends IBase {
@Field()
id: number;
}
You can see it working in examples:
👀
type-graphql/examples/interfaces-inheritance/employee/employee.input.ts
Is it bad usage?
"type-graphql": "^0.17.1",
@InterfaceType()
abstract class IBase {
@Field()
title: string;
@Field()
body?: string;
}
@InputType()
class UpdateTask2 extends IBase {
@Field()
id: number;
}
@InputType()
class AddTask2 extends IBase {
}
(node:39056) UnhandledPromiseRejectionWarning: Error: Generating schema error
at Function.<anonymous> (/Volumes/workspace/todo/server/node_modules/type-graphql/dist/schema/schema-generator.js:19:23)
at Generator.next (<anonymous>)
at fulfilled (/Volumes/workspace/todo/server/node_modules/tslib/tslib.js:104:62)
(node:39056) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:39056) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Is it bad usage?
Yes, it has to be an input type, not an interfaces type, as GraphQL distinguishes output types (interfaces) from input types (inputs) and you can't mix them.
Please read the examples and docs carefully and ask community for help on chat:
https://gitter.im/type-graphql/Lobby