可以计算type-graphql中的结果列表元素

时间:2019-05-01 17:27:03

标签: node.js graphql type-graphql

我是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
      }
}

有可能吗?

1 个答案:

答案 0 :(得分:0)

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

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