GraphQL输出类型

时间:2020-09-24 08:38:53

标签: graphql nestjs typeorm

尝试构建NestJS + GraphQL + TypeORM后端,我坚持尝试创建突变和查询。教程人员正是以这种方式解决问题的,但是我仍然不明白错误是什么。 错误:无法为“ createUser”确定GraphQL输出类型。确保您的班级装饰有合适的装饰物。 所有装饰似乎都还可以。有人有同样的问题吗?

user.module.ts


   id  2020-01-01  2020-02-01  2020-03-01
0   1         1.0         NaN         NaN
1   2         NaN         2.0         NaN
2   3         NaN         NaN         3.0

user.entity.ts

import { Module } from '@nestjs/common';
import { UserService } from './user.service';
import { UsersResolver } from './user.resolver';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './user.entity';

@Module({
  imports: [TypeOrmModule.forFeature([User])],
  exports:[TypeOrmModule],
  providers: [UserService,UsersResolver ],
})
export class UserModule {}

user.resolver.ts

import {
  Column,
  Entity,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { Post } from '../post/post.entity';
import { Comment } from '../comment/comment.entity';
import { Field, ObjectType } from '@nestjs/graphql';

enum RoleEnum {
  Admin = 'Admin',
  Author = 'Author',
}

@Entity()
@ObjectType()
export class User {
  @PrimaryGeneratedColumn('uuid')
  @Field()
  id: string;

  @Column()
  @Field()
  name: string;

  @Column()
  @Field()
  email: string;

  @Column()
  @Field()
  password: string;

  @Column({
    type: 'enum',
    enum: RoleEnum,
    default: RoleEnum.Author,
  })
  @Field()
  role: RoleEnum;

  @OneToMany((type) => Post, post => post.user)
  @Field(type=>Post)
  posts: Post[];

  @OneToMany((type) => Comment, comment => comment.author)
  @Field(type=>Comment)
  comments: Comment[];
}

user.service.ts

   import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
   import { UserService } from './user.service';
   import { CreateUserDto } from './dto/createuser.dto';
   import { userInput } from './input/user.input';
   import { User } from './user.entity';
   @Resolver((of=>User))
   export class UsersResolver {
     constructor(private userService: UserService) {
     }
   
     @Mutation(()=> [CreateUserDto])
     async createUser (@Args('data') data: userInput){
       return this.userService.createUser(data)
     }   
   }

createUser.dto.ts


    import { Injectable } from '@nestjs/common';
    import { InjectRepository } from '@nestjs/typeorm';
    import { User } from './user.entity';
    import { Repository } from 'typeorm';
    import { CreateUserDto } from './dto/createuser.dto';
    
    @Injectable()
    export class UserService {
      constructor (@InjectRepository(User) private readonly userRepository: Repository<User>) {}
      async createUser (data: CreateUserDto): Promise<User> {
        const user = new User()
        user.name=data.name
        user.email=data.email
        user.role=data.role
        user.password=data.password
    
        await this.userRepository.save(user)
    
        return user
      }
    }

user.input.ts


    import { Field, ObjectType } from 'type-graphql';
    import { registerEnumType } from '@nestjs/graphql';
    
    enum RoleEnum {
      Admin = 'Admin',
      Author = 'Author',
    }
    
    registerEnumType(RoleEnum, {
      name: 'RoleEnum',
    });
    
    @ObjectType()
    export class CreateUserDto {
      @Field()  name: string;
      @Field()  email: string;
      @Field()  password: string;
      @Field(type => RoleEnum) role: RoleEnum;
    }

1 个答案:

答案 0 :(得分:1)

我注意到错误的几个可能原因,

1. user.resolver.ts

@Mutation() 装饰器应该返回类型 [User],而不是类型 [CreateUserInput]

@Mutation(()=> [User]) // change here
 async createUser (@Args('data') data: userInput){
   return this.userService.createUser(data)
 }   

2. user.input.ts

如果遇到错误:"Input" cannot be found in the registry, 将 @ArgsType() 装饰器添加到类 userInput

@InputType()
@ArgsType() //add decorator here
export class userInput {
  @Field()  name: string;
  @Field()  email: string;
  @Field()  password: string;
  @Field(type => RoleEnum) role: RoleEnum;
}

注意:我使用了 "@nestjs/graphql": "^7.10.3",