首页 > 解决方案 > NestJS Graphql - 解析字段

问题描述

我正在尝试从官方文档中运行基本示例。但是,当我尝试运行代码时,出现错误:

UnhandledPromiseRejectionWarning:错误:无法确定“posts”字段的 GraphQL 主机类型。确保您的类使用适当的装饰器(例如,@ObjectType())进行装饰。


    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 {}


    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 {}


    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 }];
        }
    }


    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[];
    }


    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;
    
    }

"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"
   },

我尝试了很多变体:

有人可以帮助了解我在文档中遗漏的内容以及如何更正和运行示例。

标签: modelgraphqlnestjs

解决方案


推荐阅读