NestJS模块未向其解析器注入依赖项

时间:2019-10-17 16:20:57

标签: dependency-injection nestjs

我一直在开发基于NestJS的API,这是我正在经历相当奇怪的行为的代码的一部分。

问题:PagePartModule不会导入我添加到导入中的任何模块(在本例中为ImageModule),因此当尝试在PagePartModule的解析器中注入ImageService时,我变得未定义。请参见下面的代码:

// page-part.module.ts
@Module({
  imports: [ImageModule],
  providers: [PagePartImageResolver],
})
export class PagePartModule {}



// page-part-image.resolver.ts
@Resolver(of => PagePartImage)
export class PagePartImageResolver {

  constructor(
    private readonly imageService: ImageService
  ) {}

  @ResolveProperty('image', type => Image, { nullable: true })
  async image(@Root() pagePart: PagePart) {
    // NOTE: pagePart is filled with correct data
    // ERROR: "Cannot read property 'find' of undefined",
    // this.imageService is undefined
    return await this.imageService.find({ id: pagePart.imageId });
  }
}


// image.module.ts
@Module({
  imports: [DataloaderModule],
  providers: [ImageResolver, ImageService],
  exports: [ImageService],
})
export class ImageModule {}



// image.service.ts
@Injectable()
export class ImageService {}

请注意,我是:
1)在ImageModule导出装饰器中添加ImageService
2)ImageService使用@Injectable()装饰
3)通过在PagePartModule上的导入来导入ImageModule(提供ImageService)
4)通过page-part-image.resolver.ts

中的构造函数注入ImageService

我尝试过的事情:
-NestJS在构建时不会引发任何错误;一旦我查询了解析器,就会显示“图像”字段错误-说Cannot read property 'find' of undefined-> imageService未定义:未注入。
-如果将ImageModule注入其他模块,则一切正常。
-即使我创建了属于PagePartModule的一部分的服务,并将其放在PagePartModule提供程序中,它仍然没有注入到指定的解析器中-这很奇怪(但是尝试将创建的服务再次导入到其他模块中也可以按预期进行)。 -"emitDecoratorMetadata": true存在于我的tsconfig.json

我相信我正在做注射工作所需的一切。 (API有十几个模块,我在其中使用了相同的方法,并且可以按预期工作)

对于可能导致此问题的任何指导,我将不胜感激。

"dependencies": {
    "@nestjs/common": "^6.8.3",
    "@nestjs/core": "^6.8.3",
    "@nestjs/graphql": "^6.5.3",
    "@nestjs/jwt": "^6.1.1",
    "@nestjs/passport": "^6.1.0",
    "@nestjs/platform-express": "^6.8.3",
    "@nestjs/typeorm": "^6.2.0",
    "apollo-server-express": "^2.9.6",
    "aws-sdk": "^2.551.0",
    "axios": "^0.19.0",
    "crc-32": "^1.2.0",
    "dataloader": "^1.4.0",
    "dotenv": "^8.2.0",
    "graphql": "^14.5.8",
    "graphql-tools": "^4.0.5",
    "helmet": "^3.21.1",
    "lodash": "^4.17.15",
    "nestjs-redis": "^1.2.3",
    "passport": "^0.4.0",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "pg": "^7.12.1",
    "reflect-metadata": "^0.1.12",
    "rimraf": "^2.6.2",
    "rxjs": "^6.3.3",
    "type-graphql": "^0.17.5",
    "typeorm": "^0.2.19",
    "uuid": "^3.3.3"
  },
  "devDependencies": {
    "@nestjs/testing": "^6.0.0",
    "@types/express": "^4.16.0",
    "@types/jest": "^23.3.13",
    "@types/node": "^10.14.22",
    "@types/supertest": "^2.0.7",
    "axios-mock-adapter": "^1.17.0",
    "concurrently": "^4.1.0",
    "jest": "^24.9.0",
    "nodemon": "^1.19.4",
    "prettier": "^1.15.3",
    "supertest": "^3.4.1",
    "ts-jest": "24.0.2",
    "ts-loader": "^6.2.0",
    "ts-node": "8.1.0",
    "tsconfig-paths": "3.8.0",
    "tslint": "5.16.0",
    "typescript": "3.4.3",
    "wait-on": "^3.2.0",
    "webpack": "^4.41.2",
    "webpack-cli": "^3.3.9",
    "webpack-node-externals": "^1.7.2"
  },

1 个答案:

答案 0 :(得分:0)

// incorrectly imported decorator
import { Resolver } from 'type-graphql';  

// correctly imported decorator in NestJS
import { Resolver } from '@nestjs/graphql';