Apollo Count information (pagination)

Hi,

How can I get information about count inside my query?
This is my query which return paginated companies list.
How I can get count of MongoCompanies.find({}) and pass it?

  Query: {
    getAllCompanies(root, { limit, skip }) {
      return MongoCompanies.find({}, { skip, limit }).fetch();
    },
  },

Did you try MongoCompanies.find({}).count()?

But I would like to return data and count information :slight_smile:

Oh so you would need to put it in your schema so it will be apart of your graph.

  type MongoCompanies {
    id: Int!,
    company_name: String,
    company_count: Int
  }

Then because company_count is not apart of your original MongoCompanies schema (I’m assuming id and company_name is here) you will have to add its custom functionality to your resolver:

const resolvers = {
  Query: {
    async getAllCompanies(root, { limit, skip }) {
      return MongoCompanies.find({}, { skip, limit }).fetch();
    },
  },
  Mutation: { },
  MongoCompanies: {
    company_count(root) {
      return MongoCompanies.find({}).count();    
    }
  }
};

@townmulti Thank you for your help.
Hmmm… Is this approach optimal?
I think MongoCompanies.find({}).count(); is done as many times as companies I get :fearful:

For example:
If I’m getting 20 companies, I’m calling count() 20 times?

Hopefully someone else can chime in with some more optimal solutions, but here are a few other suggestions.

It’s possible that you could only put the count on one company and skip the others.

You could return your own object back from the query with the count as a property.

You could put the count in it’s very own query and put it on its own small component.