【发布时间】:2017-10-31 08:55:38
【问题描述】:
我的问题
嗨! 我正在尝试使用 Meteor、Apollo、React 和 Sequelize 实现一个简单的分页。我使用 findAll() 函数查询我想要的内容没有问题,但由于我还想要受我的查询影响的总行数,我正在使用 findAndCountAll() 但我得到这个错误:
未处理(在 react-apollo 中)错误:GraphQL 错误:预期可迭代,但没有为字段 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
}
}
`;
我认为缺少某些东西,但我找不到它是什么。如果您还有其他需要,请告诉我。
【问题讨论】:
-
感谢@Tilekbekov Yrysbek 通过将
.then((result) => { return result.rows; });添加到我的findAndCountAll 函数中解决了这个问题