0

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

  1. 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" } } ] }

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

user837593
  • 337
  • 1
  • 5
  • 25

2 Answers2

1

here is how I solved above issue

  1. response.filter retuns an array and since I was looking for an array of comments with specific post id, just putting condition inside map function helped.
  2. dataSources.mvrpAPI.getAllComments() gives Promise . To get real Object I used "await" and since await can only be used from async function, I made comments function async.

    async comments(post, {postid}, {dataSources}){
         const response =  await dataSources.mvrpAPI.getAllComments();
           return response.filter(comment => comment.postId === post.id);
         }
      },   
    
user837593
  • 337
  • 1
  • 5
  • 25
0

You are declaring comments as a list on the Post schema, and on the resolver you are returning a single object

return ( { __typename: "Post", postId:post.id });

that's the reason for the "Expected Iterable" error.

I'm not familiar with that database api, but this shouldn't be hard with most api's.

In mongoose it would be something like

async (post,_, {model}) => model.Comment.find({postId: post.id})
  • Thanks for your reply Gabriel. You are right that return ( { __typename: "Post", postId:post.id }); returns single object. I figured issue and posting solution in my comment below. Thanks again – user837593 May 22 '20 at 12:56