A希望通过在apollo-federation中扩展评论与帖子。
创始人
2024-09-29 07:00:17
0
  1. 首先,在apollo-federation中定义一个Comment服务和一个Post服务:

// Comment服务 type Comment @key(fields: "id") { id: ID! postId: ID! text: String! }

extend type Query { getCommentById(id: ID!): Comment }

// Post服务 type Post @key(fields: "id") { id: ID! title: String! authorId: ID! }

extend type Query { getPostById(id: ID!): Post }

  1. 然后,通过使用JS代码扩展Comment服务和Post服务来连接它们:

// 扩展Comment服务 const resolvers = { Comment: { post(comment) { return { __typename: 'Post', id: comment.postId }; }, }, }; const server = new ApolloServer({ schema: buildFederatedSchema([ { typeDefs: CommentServiceTypeDefs, resolvers, }, ]), });

// 扩展Post服务 const resolvers = { Post: { comments(post) { return [ { __typename: 'Comment', id: '1', text: 'Comment 1 for Post 1', postId: '1' }, { __typename: 'Comment', id: '2', text: 'Comment 2 for Post 1', postId: '1' }, { __typename: 'Comment', id: '3', text: 'Comment 1 for Post 2', postId: '2' }, { __typename: 'Comment', id: '4', text: 'Comment 2 for Post 2', postId: '2' }, ].filter(comment => comment.postId === post.id); }, }, }; const server = new ApolloServer({ schema: buildFederatedSchema([ { typeDefs: PostServiceTypeDefs, resolvers, }, ]), });

  1. 最后,在Apollo Federation Gateway中设置服务:

// Apollo Federation Gateway中的服务设置 const gateway = new ApolloGateway({ serviceList: [ { name: 'comment-service', url: '[Comment Service URL]' }, { name: 'post-service', url: '[Post Service URL]' }, ], });

const server = new ApolloServer({ gateway, subscriptions: false, });

// 启动Apollo Federation Gateway服务器 server.listen().then(({ url }) => { console.log(� Gateway ready at ${url}); });

相关内容

热门资讯

总算清楚!hhpoker到底可... 总算清楚!hhpoker到底可以作弊码,pokemmo手机脚本辅助器,2025新版教程(有挂方法);...
记者爆料!约局吧能不能开挂,w... 此外,数据分析德州(约局吧能不能开挂)辅助神器app还具备辅助透视行为开挂功能,通过对客户约局吧能不...
技术分享!wepoker辅助器... 技术分享!wepoker辅助器官方,sohoo辅助,软件教程(有挂技巧)准备好在wepoker辅助器...
信息共享!cloudpoker... 这是一款非常优秀的cloudpoker怎么开挂 ia辅助检测软件,能够让你了解到cloudpoker...
推荐十款!wepoker透视脚... 推荐十款!wepoker透视脚本视频,hhpoker买挂,安装教程(有挂神器);科技安装教程;136...
科普攻略!aapoker辅助怎... 科普攻略!aapoker辅助怎么用,hhpoker有后台操作吗,安装教程(有挂技巧)1、点击下载安装...
玩家必看教程!竞技联盟破解版最... 玩家必看教程!竞技联盟破解版最新版,云扑克有透视吗,技巧教程(有挂辅助)这是由厦门游乐互动科技有限公...
信息共享!wepoker怎么设... 1、信息共享!wepoker怎么设置房间,wepoker辅助真的假的,揭秘教程(有挂攻略);详细教程...
教程辅助!hhpoker是正规... 教程辅助!hhpoker是正规的吗,约局吧怎么看有没有挂,技巧教程(有挂透明);实战中需综合运用上述...
一分钟快速了解!哈糖大菠萝有挂... 《一分钟快速了解!哈糖大菠萝有挂吗5个常用方法,agpoker辅助,2025版教程(有挂技巧)》 哈...