GraphQL 端点在 Nest.js 中返回空对象

标签 graphql nestjs sequelize-typescript

我正在使用 Nest.js 和 Sequelize-Typescript 构建 GraphQL API。

当我调用 delete 和 update mutations 时,我得到了一个 null 对象,但操作已经完成。我需要输入 {nullable: true} 因为我收到一条错误消息 Cannot return null for non-nullable field 。我该如何解决?我需要端点返回更新的对象以在前面显示信息

error img

book.dto.ts

import { ObjectType, Field, Int, ID } from 'type-graphql';

@ObjectType()
export class BookType {
    @Field(() => ID, {nullable: true})
    readonly id: number;
    @Field({nullable: true})
    readonly title: string;
    @Field({nullable: true})
    readonly author: string;
}

book.resolver.ts

import {Args, Mutation, Query, Resolver} from '@nestjs/graphql';
import { Book } from './model/book.entity';
import { BookType } from './dto/book.dto';
import { CreateBookInput } from './input/createBook.input';
import { UpdateBookInput } from './input/updateBook.input';
import { BookService } from './book.service';

@Resolver('Book')
export class BookResolver {
    constructor(private readonly bookService: BookService) {}

    @Query(() => [BookType])
    async getAll(): Promise<BookType[]> {
        return await this.bookService.findAll();
    }

    @Query(() => BookType)
    async getOne(@Args('id') id: number) {
        return await this.bookService.find(id);
    }

    @Mutation(() => BookType)
    async createItem(@Args('input') input: CreateBookInput): Promise<Book> {
        const book = new Book();
        book.author = input.author;
        book.title = input.title;
        return await this.bookService.create(book);
    }

    @Mutation(() => BookType)
    async updateItem(
        @Args('input') input: UpdateBookInput): Promise<[number, Book[]]> {
        return await this.bookService.update(input);
    }

    @Mutation(() => BookType)
    async deleteItem(@Args('id') id: number) {
        return await this.bookService.delete(id);
    }

    @Query(() => String)
    async hello() {
        return 'hello';
    }
}

book.service.ts

import {Inject, Injectable} from '@nestjs/common';
import {InjectRepository} from '@nestjs/typeorm';
import {Book} from './model/book.entity';
import {DeleteResult, InsertResult, Repository, UpdateResult} from 'typeorm';

@Injectable()
export class BookService {
    constructor(@Inject('BOOKS_REPOSITORY') private readonly bookRepository: typeof Book) {}

    findAll(): Promise<Book[]> {
        return this.bookRepository.findAll<Book>();
    }

    find(id): Promise<Book> {
       return this.bookRepository.findOne({where: {id}});
    }

    create(data): Promise<Book> {
        return data.save();
    }

    update(data): Promise<[number, Book[]]> {
        return this.bookRepository.update<Book>(data, { where: {id: data.id} });
    }

    delete(id): Promise<number> {
        return this.bookRepository.destroy({where: {id}});
    }
}

最佳答案

您可以在解析器查询中设置选项参数来修复它

@Query(() => BookType, { nullable: true })

关于GraphQL 端点在 Nest.js 中返回空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59866516/

相关文章:

javascript - 使用Facebook的DataLoader传递参数

nestjs - 如何在 Nestjs DatabaseModule 中使用 ConfigService

node.js - 如何以正确的方式处理 Sequelize 验证错误?

node.js - LOCK 在sequelize -> types/transaction.d.ts 中定义为ENUM 和接口(interface)

javascript - 如何在 forEach 中运行 useQuery?

php - 我可以将 Guzzle 用于 GraphQL API 消费吗?

arrays - 类验证器不验证数组

nestjs - 如何使用 NestJs 中的拦截器修改来自 PUT 的请求和响应

angular - 在 Sequelize 中返回对象而不是一个对象的数组

javascript - 类型\"me\"的字段\"User\"必须具有一系列子字段