model - NestJS Graphql - 解析字段
问题描述
我正在尝试从官方文档中运行基本示例。但是,当我尝试运行代码时,出现错误:
UnhandledPromiseRejectionWarning:错误:无法确定“posts”字段的 GraphQL 主机类型。确保您的类使用适当的装饰器(例如,@ObjectType())进行装饰。
- 登陆/app.module.ts
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { join } from 'path';
import { TypeOrmModule } from '@nestjs/typeorm';
import { LandingModule } from './landing/landing.module';
@Module({
imports: [
TypeOrmModule.forRoot(),
LandingModule,
GraphQLModule.forRoot({
include: [LandingModule],
autoSchemaFile: join(process.cwd(), 'src', 'graphql', 'schema.gql'),
sortSchema: true,
installSubscriptionHandlers: true,
}),
],
})
export class AppModule {}
- 着陆/着陆.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ProfileService } from './profile.service';
import { ProfileResolver } from './profile.resolver';
import { LanguagesService } from './languages.service';
import { LanguagesResolver } from './languages.resolver';
import { Language } from './entities/language';
import { Locale } from './entities/locale';
import { Contact } from './entities/contact';
import { ContactsService } from './contacts.service';
import { ContactsResolver } from './contacts.resolver';
import { AuthorsResolver } from './author.resolver';
@Module({
imports: [TypeOrmModule.forFeature([Language, Locale, Contact])],
providers: [
// ProfileService,
// ProfileResolver,
// LanguagesService,
// LanguagesResolver,
// ContactsService,
// ContactsResolver,
AuthorsResolver,
],
})
export class LandingModule {}
- 登陆/authors.resolver.ts
import { Args, Int, Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
import { Author } from './models/author';
import { Post } from './models/post';
@Resolver(of => Author)
export class AuthorsResolver {
constructor() {}
@Query(returns => Author)
async author(@Args('id', { type: () => Int }) id: number) {
return { ...new Author(), id, firstName: 'aaaa', lastName: 'bbb' };
}
@ResolveField()
async posts(@Parent() author: Author) {
const { id } = author;
return [{ ...new Post(), id: 1, title: 'test', votes: 123 }];
}
}
- 登陆/models/author.ts
import { Field, Int, ObjectType } from '@nestjs/graphql';
import { Post } from './post';
@ObjectType()
export class Author {
@Field(type => Int)
id: number;
@Field({ nullable: true })
firstName?: string;
@Field({ nullable: true })
lastName?: string;
@Field(type => [Post])
posts: Post[];
}
- 着陆/模型/post.ts
import { Field, Int, ObjectType } from '@nestjs/graphql';
@ObjectType()
export class Post {
@Field(type => Int)
id: number;
@Field()
title: string;
@Field(type => Int, { nullable: true })
votes?: number;
}
- package.json(仅限部门)
"dependencies": {
"@nestjs/common": "^7.6.15",
"@nestjs/core": "^7.6.15",
"@nestjs/graphql": "^7.10.3",
"@nestjs/platform-express": "^7.6.15",
"@nestjs/typeorm": "^7.1.5",
"@nestjsplus/knex": "^1.0.0",
"apollo-server-express": "^2.21.2",
"geojson": "^0.5.0",
"graphql": "^15.5.0",
"graphql-tools": "^7.0.4",
"luxon": "^1.26.0",
"mysql": "^2.14.1",
"pg": "^8.5.1",
"pg-hstore": "^2.3.3",
"psql": "0.0.1",
"reflect-metadata": "^0.1.10",
"rimraf": "^3.0.2",
"rxjs": "^6.6.6",
"typeorm": "0.2.31"
},
我尝试了很多变体:
- 将模型类型传递给 @ResolveField(() => [Post])
- 尝试将@nestjs/graphql 库版本降级到早期版本(7.7.0 和更早版本)...
有人可以帮助了解我在文档中遗漏的内容以及如何更正和运行示例。
解决方案
推荐阅读
- java - java.inject.singleton 不适用于注入
- uno-platform - 覆盖 Fluent 默认颜色的最简单方法是什么?
- r - 相同(),但对于基础 R 中的环境/R6?
- jenkins - 如何确定用户可以在 Jenkins 中访问的“页数”?
- mysql - 如何在忽略特定数据库的情况下启动 mysqld
- java - Cloud Firestore API 调用不响应在 Apple M1 上本地运行的 Java GAE 标准应用程序
- python - 给定特定条件的嵌套字典列表中的值求和
- c# - 使用列表创建 linq 条件
- python - 在一列上对数据框进行分组,并在其他列中组合数据(文本)
- clojure - 如何将哈希图的所有键值对传递给函数(不传递整个哈希图)?