Kan*_*now 8 node.js typescript graphql nestjs
我正在尝试使用Nest.js、GraphQL和MongoDB创建简单的应用程序。我想使用TypeORM和TypeGraphql来生成我的模式并与 localhost 数据库建立连接,但是我无法运行我的服务器,nest start
因为我收到此错误:
UnhandledPromiseRejectionWarning:错误:无法确定 getArticles 的 GraphQL 输出类型
我不知道为什么我会收到这个错误。我的班级ArticleEntity
没有任何非主要类型,所以应该没有任何问题。我试图删除() => ID
从@Field()
提起装饰_id
的ArticleEntity
类,但它并没有帮助
文章解析器
@Resolver(() => ArticleEntity)
export class ArticlesResolver {
constructor(
private readonly articlesService: ArticlesService) {}
@Query(() => String)
async hello(): Promise<string> {
return 'Hello world';
}
@Query(() => [ArticleEntity])
async getArticles(): Promise<ArticleEntity[]> {
return await this.articlesService.findAll();
}
}
Run Code Online (Sandbox Code Playgroud)
文章服务
@Injectable()
export class ArticlesService {
constructor(
@InjectRepository(ArticleEntity)
private readonly articleRepository: MongoRepository<ArticleEntity>,
) {}
async findAll(): Promise<ArticleEntity[]> {
return await this.articleRepository.find();
}
}
Run Code Online (Sandbox Code Playgroud)
文章实体
@Entity()
export class ArticleEntity {
@Field(() => ID)
@ObjectIdColumn()
_id: string;
@Field()
@Column()
title: string;
@Field()
@Column()
description: string;
}
Run Code Online (Sandbox Code Playgroud)
文章DTO
@InputType()
export class CreateArticleDTO {
@Field()
readonly title: string;
@Field()
readonly description: string;
}
Run Code Online (Sandbox Code Playgroud)
如果您需要其他任何评论
Dan*_*den 10
ArticleEntity
应与装饰@ObjectType
如装饰这里的文档。
@Entity()
@ObjectType
export class ArticleEntity {
...
}
Run Code Online (Sandbox Code Playgroud)
就我而言,我使用的是@ObjectType
装饰器,但我是从type-graphql
. 我改为导入@nestjs/graphql
,问题解决了。
import { ObjectType } from '@nestjs/graphql';
Run Code Online (Sandbox Code Playgroud)
请参阅此处以获取 GitHub 上的相关讨论。
归档时间: |
|
查看次数: |
7469 次 |
最近记录: |