我是typeGraphQL的新手,我的问题是可能对结果列表元素进行计数

我有模型类型的书

import { Field, ID, ObjectType, Root } from "type-graphql";
import { Model, Column } from "sequelize-typescript";


@ObjectType()
export default class Books extends Model<Books>
{

    @Field(() => ID)
    @Column
    id: number;

    @Field(() => String)
    @Column
    bookName: string;

}


在解析器中查询

@Query(() => [Books])
async getBooks()
{
    return Books.findAll()
}


在graphiQL中运行查询时

getTest
{
    id
    bookName
}


得到回应

"getBooks" :
[
    {
        "id": "1",
        "bookName": "bookOne"
    },
    {
        "id": "2",
        "bookName": "bookTwo"
    }
]


但是我需要添加一个附加字段,例如获取所有接收到的项目的totalCount。
如何正确做?
现在我们必须创建一个单独的查询,但是对于大样本来说,这是非常不便的,并且会导致复杂和大型查询的重复

@Query(() => [Books])
async countBooks()
{
    return Books.findAll().length
}


我尝试为元素的数量和模型本身创建一个单独的联合类型

@ObjectType()
export default class UnionType extends Model<UnionType>
{

    @Field(() => [Books])
    books: Books[];

    @Field(() => Int)
    totalCount(@Root() parent : UnionType) : number
    {
        return parent.books.length;
    }

}


并在解析器中运行下一个查询

@Query(() => [UnionType])
async getBooksAndCountAll()
{
    let union : any = {}

    union.books = Books.findAll();
    union.totalCount = union.books.length;

    return union;
}


但是运行查询时graphiQL有错误
error "message": "Expected Iterable, but did not find one for field Query.getBooks.",
据我了解,它没有传输模型期望的数据

我试图使用createUnionType

import { createUnionType } from "type-graphql";
const SearchResultUnion = createUnionType({
    name: "Books", // the name of the GraphQL union
    types: [Books, CountType], // array of object types classes
  });


在哪里UnionType

@ObjectType()
export default class CountType extends Model<CountType>
{
    @Field(() => Int, { nullable : true })
    totalCount: number;
}


在解析器中查询

    @Query(returns => [SearchResultUnion])
    async getBooks(
    ): Promise<Array<typeof SearchResultUnion>>{

        return new Promise((resolve, reject) => {
            Books.findAll()
            .then(books => {

                let totalCount = books.length;

                return [...books, ...totalCount];
            });
        });
    }


但是字符串return [...books, ...totalCount];на...totalCount中有错误
Type 'number' must have a '[Symbol.iterator]()' method that returns an iterator.

如果您不通过...totalCount,则请求有效,但是分别没有已有的totalCount

getTest{
__typename
  ... on Books
  {
    id
    bookName
  }
}


请求

 "getBooks": [
    {
        "id": "1",
        "bookName": "bookOne"
    },
    {
        "id": "2",
        "bookName": "bookTwo"
    }
  ]


因此,因此我需要一个请求

getTest
{
    totalCount
    __typename
      ... on Books{
        id
        bookName
      }
}


可能吗?

最佳答案

https://github.com/19majkel94/的答案
谢谢,MichałLytek

@ObjectType()
export default class GetBooksResponse {
    @Field(() => [Books])
    books: Books[];

    @Field(() => Int)
    totalCount : number;
}

@Query(() => GetBooksResponse)
async getBooks() {
  const books = await Books.findAll();
  return { books, totalCount: books.length };
}

09-18 22:44