GraphQL错误:使用Sequelize findAndCountAll函数时预期为Iterable

时间:2017-05-30 18:49:09

标签: sequelize.js react-apollo

我的问题

嗨! 我尝试使用Meteor,Apollo,React和Sequelize实现简单的分页。我使用 findAll()函数查询我想要的东西没有问题,但由于我还想要查询影响的总行数,我使用的是 findAndCountAll()但是我得到这个错误:

  

未处理(在react-apollo中)错误:GraphQL错误:预期为Iterable,但没有为字段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
    }
  }
`;

我认为缺少了一些东西,但我找不到它是什么。如果您还有其他需要,请告诉我。

1 个答案:

答案 0 :(得分:0)

findAndCountAll函数返回一个具有2个属性{count: rowsCount, rows: arrayOfModelInstances}的对象。你应该迭代rows