kratam
07/29/2018, 2:06 PMposts
query which is retrieved by an API call. The comments
field for every post
is retrieved by another API call, the requests are batched with dataloader. Is it possible to filter the returned posts
based on the comments
field (e.g. only return posts
with comments
)? My problem is that the comments
field is only available after I returned the promise from the posts
query. Is it possible to filter/transform the returned data after every related promises resolved?divyendu
07/29/2018, 4:16 PMkratam
07/29/2018, 6:27 PMposts
and comments
are retrieved via an external API call (e.g. `fetch('someapi/posts'). So initially the posts
array can't be filtered by the comments
because they're retrieved separately. E.g.:
// Posts.js
export const Posts = {
comments: (parent, args, ctx, info) => {
return ctx.externalApi.getCommentsForPost(parent.id)
}
}
// Query.js
async posts(parent, args, ctx, info) {
const posts = ctx.externalApi.getPosts()
// ideally I'd be able to filter the posts
// based on the comments property
// but the prop is not there, it'll be
// populated by Posts() above
return posts
}
divyendu
07/29/2018, 6:38 PMkratam
07/29/2018, 6:44 PMposts
based on their comments
- the problem is that the comments
are not available when I receive the posts
. I have to return the promise (which will resolve to the posts array) in order for the comments
resolver function to run. So somehow I want to modify the results after return
...kratam
07/29/2018, 6:46 PMtype Query {
posts(onlyCommented: Boolean): [Post]
}
type Post {
id: Int!
comments: [Comment]
}
type Comment {
id: Int!
message: String!
}
kratam
07/29/2018, 9:17 PMdivyendu
07/30/2018, 6:11 AMkratam
07/30/2018, 7:06 AMkratam
07/30/2018, 7:26 AMdivyendu
07/30/2018, 7:35 AMdivyendu
07/30/2018, 7:35 AM