GraphQL error: Expected Iterable while using Sequelize findAndCountAll function

GraphQL error: Expected Iterable while using Sequelize findAndCountAll function

我的问题

嗨! 我正在尝试使用 Meteor、Apollo、React 和 Sequelize 实现一个简单的分页。我可以使用 findAll() 函数查询我想要的内容,但由于我还想要受查询影响的总行数,因此我正在使用 findAndCountAll() 但我收到此错误:

Unhandled (in react-apollo) Error: GraphQL error: Expected Iterable, but did not find one for field Query.books

resolvers.js

export const resolvers = {
  Query: {
    async books(root, args, context) {
      return  Book.findAndCountAll({ 
        where: {},
        offset: args.offset,
        limit: args.limit,
      });
    }
  }
};

我的架构

export const typeDefs =`
type Book {
  id: String!
  title: String!
  author: String!
  price: Float!
  quantity: Float!
  isbn: String!
  publisher: String!
  description: String
  collectionName: String
  collectionNumber: Int
  serieName: String
  serieNumber: Int
}

type Query {
  books(limit: Int, offset: Int): [Book]
}
`;

我的模型

const BookModel = db.define('book', {
  title: { type: Sequelize.STRING },
  author: { type: Sequelize.STRING },
  price: { type: Sequelize.FLOAT },
  quantity: { type: Sequelize.FLOAT },
  isbn: { type: Sequelize.STRING },
  publisher: { type: Sequelize.STRING },
  description: { type: Sequelize.STRING },
  collectionName: { type: Sequelize.STRING },
  collectionNumber: { type: Sequelize.INTEGER },
  serieName: { type: Sequelize.STRING },
  serieNumber: { type: Sequelize.INTEGER },
});

我的 graphQL 查询

const allBooks = gql`
  query BooksForDisplay($limit: Int, $offset: Int) {
    books(limit: $limit, offset: $offset) {
      id,
      title,
      author,
      price,
      quantity,
      isbn,
      publisher,
      description,
      collectionName,
      collectionNumber,
      serieName,
      serieNumber
    }
  }
`;

我觉得少了什么,但找不到是什么。如果您还需要什么,请告诉我。

findAndCountAll 结果 returns 具有 2 个属性的对象 {count: rowsCount, rows: arrayOfModelInstances}。你应该迭代 rows