GraphQL error: Expected Iterable while using Sequelize findAndCountAll function

Viewed 2292

My issue

Hi! I'm trying to implement a simple pagination using Meteor, Apollo, React and Sequelize. I have no problem querying what I want using the findAll() function but since I also want the total number of rows affected by my query I am using findAndCountAll() but I get this error :

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

My schema

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

My model

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

My graphQL query

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

I think something is missing but I cannot find what it is. Please let me know if you need anything else.

1 Answers
Related