无法从 TypeScript 反射系统推断 GraphQL 类型您需要为“Address”类的“id”提供显式类型

问题描述

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...