我的 Nest 应用程序遇到了最奇怪的问题(生活在 Nx 中,无论它的价值如何)。我生成了一个新资源:
nx g resource --project testing
✔ Which generator would you like to use? · @nx/nest:resource
✔ What name would you like to use for this resource (plural, e.g., `users`)? · items
✔ What transport layer do you use? · graphql-code-first
✔ Would you like to generate CRUD entry points? (Y/n) · false
它被添加到Imports
主 app.module 中:
@Module({
imports: [
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
playground: false,
plugins: [ApolloServerPluginLandingPageLocalDefault()],
autoSchemaFile: "schema.gql",
}),
ItemsModule,
],
})
export class AppModule {}
然后,我添加了简单的解析器以使 GraphQL 满意:
// ./items/items.resolver.ts
import { Int, Query, Resolver } from "@nestjs/graphql";
import type { ItemsService } from "./items.service";
@Resolver()
export class ItemsResolver {
constructor(private readonly service: ItemsService) {}
@Query(() => [Int])
getAll() {
return [1, 2, 3];
}
}
所以我的 items.module 看起来像这样:
import { ItemsResolver } from "./items.resolver";
import { ItemsService } from "./items.service";
@Module({
providers: [ItemsResolver, ItemsService],
})
export class ItemsModule {}
这对我来说非常有效。但尽管如此,我仍然得到
Error: Nest can't resolve dependencies of the ItemsResolver (?). Please make sure that the argument Object at index [0] is available in the ItemsModule context.
如果我从班级中删除该服务ItemsResolver
,一切都会正常。