【问题标题】:possible to count result list elements in type-graphql可以计算 type-graphql 中的结果列表元素
【发布时间】:2019-09-20 05:57:15
【问题描述】:

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

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

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

有可能吗?

【问题讨论】:

    标签: node.js graphql typegraphql


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-25
      • 2021-02-09
      • 1970-01-01
      • 2014-05-14
      • 2020-03-16
      相关资源
      最近更新 更多