【问题标题】:Nest.js Graphql Nested InputType throws reference errorNestjs Graphql嵌套输入类型抛出引用错误
【发布时间】:2021-04-24 05:28:12
【问题描述】:

在 Nest.js graphql 中使用 Code First 方法我正在尝试创建一个有嵌套输入类型的突变。服务器抛出以下错误

__metadata("design:type", MenuInput)
                              ^
ReferenceError: Cannot access 'MenuInput' before initialization
at Object.<anonymous> (.../dist/order.input.js:26:31)

dist 文件如下所示

__decorate([
    graphql_1.Field(_type => MenuInput, { nullable: true }),
    __metadata("design:type", MenuInput)
], OrderInput.prototype, "menu", void 0);

输入类如下所示

import { Field, InputType, Int } from '@nestjs/graphql';

@InputType()
export class OrderInput {
  @Field()
  orderName: string;

  @Field(_type => MenuInput)
  menu: MenuInput;
}

@InputType()
class MenuInput {
  @Field()
  id: string;
}

解析器

在解析器中,我需要一个 OrderInputs 数组

  @Mutation(_returns => OrderModel)
  async createOrder(
    @Args('newOrderData', { type: () => [OrderInput] })
    newOrderData: OrderInput[],
  ): Promise<OrderModel[]> {
    return this.orderService.createOrder(newOrderData);
  }

到目前为止我尝试了什么:

试图在现场设置{ nullable: true }(运气不好)。

不确定是什么导致了这个问题。

包.json

  "dependencies": {
    "@nestjs/common": "^7.5.1",
    "@nestjs/core": "^7.5.1",
    "@nestjs/graphql": "^7.9.5",
    "@nestjs/platform-express": "^7.5.1",
    "apollo-server-express": "^2.19.2",
    "graphql": "^15.4.0",
    "graphql-tools": "^7.0.2",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.6.3"
  },
  "devDependencies": {
    "@nestjs/cli": "^7.5.1",
    "@nestjs/schematics": "^7.1.3",
    "@nestjs/testing": "^7.5.1",
    "@types/express": "^4.17.8",
    "@types/jest": "^26.0.15",
    "@types/node": "^14.14.6",
    "@types/supertest": "^2.0.10",
    "@typescript-eslint/eslint-plugin": "^4.6.1",
    "@typescript-eslint/parser": "^4.6.1",
    "eslint": "^7.12.1",
    "eslint-config-prettier": "7.1.0",
    "eslint-plugin-prettier": "^3.1.4",
    "jest": "^26.6.3",
    "prettier": "^2.1.2",
    "supertest": "^6.0.0",
    "ts-jest": "^26.4.3",
    "ts-loader": "^8.0.8",
    "ts-node": "^9.0.0",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^4.0.5"

我们不能嵌套 InputType 还是这是一个错误?非常感谢任何帮助。

【问题讨论】:

    标签: typescript graphql nestjs


    【解决方案1】:

    这是 typescript 及其装饰器如何工作的一部分。在使用它的值之后,您正在定义一个类。要么在文件中向上移动类,要么将其移动到自己的文件中。

    【讨论】:

    • 天啊!!我不敢相信我根本没有看到:face_palm。非常感谢
    猜你喜欢
    • 2021-02-20
    • 2020-07-07
    • 2017-03-07
    • 2021-06-27
    • 2021-05-30
    • 2018-07-15
    • 2021-02-09
    • 2019-05-20
    • 2019-08-15
    相关资源
    最近更新 更多