Meteor + react-apollo, how to design the following resolver?

I am using react-apollo on meteor with mysql and sequelize, I am still a beginner in JS.
Lets assume I have the following resolver function on my apollo-server:

 export default resolvers = {
     Query: {
         posts(_, args){
             return Post.findAndCountAll({ where: args   });
         },
         numberOfPosts(){
             return /// the number of selected posts
         }
     }

I would like to select some data from the database where some conditions are met and then count the amount of selected rows and return them in the field “numberOfPosts”.
findAndCountAll() returns an object, which contains the selected rows and the count. I would like to get my post() to return only the selected rows, and my numberOfPosts() to return only the count of the selected posts. Right now, both is returned by posts().

My schema is:

 type Post {
  id: Int
  date: Float
  text: String
}

 type NumberOfPosts{
  total: Int
  filtered: Int
}

type Query {
  posts(
   id: Ind,
   offset: Int,
   limit: Int,
   filter: String): [Post]
  numberOfPosts:[NumberOfPosts] 
}

schema {
  query: Query
}

The Goal is to receive data in the following format:

{
  "data": {
    "numberOfPosts": [
      {
        "total": 1000,
        "filtered": 21
      }
    ],
    "posts": [
      {
        "id": 4,
        "date": 5105626122,
        "text": "jzybiwutudi"
      },
      ...
    ]
  }
}

My work so far:
Try 1:

  let selectedCount;
export default resolvers = {
    Query: {
        posts(_, args){
            return Post.findAndCountAll({where: args}).then(
                function (results) {
                    selectedCount = results.count;
                    return results.rows
                });
        },
        numberOfPosts(){
            return selectedCount
        }
    }}

So I am defining a helping variable outside of resolvers, and set it to the number of selected rows, then the count is returned in numberOfPosts(), which works, but the problem with this is, return results.rows causes an error, and I do not understand why.

another issue is, that selectedCount is always the previous number of rows

Try 2

Another solution that seems to work is to Pass the arguments twice into the GraphQL query, like so:

{
  numberOfPosts(filter: "example") {
    total
    filtered
  }
  posts(filter: "example") {
    id
    date
    text
  }
}

Then both resolver functions know the same arguments, so I can select and count the same posts. But this looks not right to me, since I have to pass the same args twice, they will also be transmitted twice…

You have 2 resolvers( posts and numberOfPosts ), and they are independent of each other. So the one knows nothing of the other( they shouldn’t know, this is a good thing).

The only way to have numberOfPosts include the filtered count, is to send the same filter-params in the numberOfPosts query. Indeed doubling the need to send the params twice( and having to hit your dB twice.

The most logical solution is to include a filteredNumberOfPosts inside your posts-query itself, alongside the [filteredPosts], returning

type FilteredPosts {
  filteredPosts: [Post],
  numberOfFilteredPosts: Int
}

by resolving posts() to a type of FilteredPosts which in turn resolves to an object with the filteredPosts and numberOfFilteredPosts. Only the filteredPosts query knows the params, and therefor can know the answer.

Which only leaves the numberOfPosts query to resolve the total number of posts.

( But if your always sending over all filtered results( no pagination or so), you can just as easily calculate the filteredCount clientside with a Posts.length )

Thank you, I guess the lack of experience with schemas led to this issue, now I see this from another perspective and it works. :slight_smile: