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...