问题描述
import { ObjectType,ID,Int,Field } from 'type-graphql';
@ObjectType()
export default class Address {
@Field(type => ID)
id: String;
@Field()
type: string;
@Field()
title: string;
@Field()
location: string;
}
信息:ts-node-dev 版本。 1.0.0(使用 ts-node 9.1.1 版,typescript 4.0.5 版)
使用此命令启动应用程序时 ts-node-dev --respawn server.shop.ts
我有以下错误。 (无法从 TypeScript 反射系统推断 GraphQL 类型。您需要为“Address”类的“id”提供显式类型。)
解决方法
定义一个接口并用 @InterfaceType()
装饰器装饰它
@InterfaceType()
abstract class IAddress {
@Field(type => ID)
id: string;
@Field()
type: string;
@Field()
title: string;
@Field()
location: string;
}
然后像这样在你的 Address 类中实现它
@ObjectType({ implements: IAddress })
export default class Address implements IAddress {
id: string;
type: string;
title: string;
location: string
}
阅读有关定义接口的更多信息 here