简体   繁体   中英

How to stitch schemas using apollo graphQL?

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.

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})

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); } },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM