I am a java programmer learning graphQL. I have dataset as below sample where comment has postId, but post doestn't have comment information.
comments
{
"postId": 1,
"id": 1,
"name": "id labore ex et quam laborum",
"email": "Eliseo@gardner.biz",
"body": "laudantium enim quasi"
}
post
{
"userid": 3,
"id": 1,
"title": "Post 1"
}
Using Apollo Federation
Can I have comment details in Post response?
{ "data": { "posts": [ { "userid": 3, "id": 1, "title": "Post 1" "comments": { "id": 1, "name": "id labore ex et quam laborum", "email": "Eliseo@gardner.biz", "body": "laudantium enim quasi" } } ] }
I need to basically use following algorithm
- get all comments
- Filter comments with given postId
- collect all matching comments and return from resolver function
Below is the post.js code
type Post @key(fields: "id"){ id: ID! userid: Int! title: String! comments: [Comment] } extend type Comment @key(fields: "id" ){ id: ID! @external } const resolvers = { Post: { comments(post){ return ( { __typename: "Post", postId:post.id }); } Query: { post: (root, { id }, { dataSources }) => dataSources.mvrpAPI.getAPost(id), posts: (root, args, { dataSources }) => dataSources.mvrpAPI.getAllPosts()}
With above resolver's comment method I am getting below error
"message": "Expected Iterable, but did not find one for field
\"Post.comments\".",
then I tried below resolver method and this cannot recognize mvrpAPI, even though it works in query section of resolvers
async comments(post, {dataSources}){
const allComments = dataSources.mvrpAPI.getAllComments();;
return allComments.postId.findAll(
{ __typename: "Post", postId:post.id }
);
}
}
Can someone help on how to write above mentioned logic (in point 2) in graphql.