使用联合解决 M:M(多对多)
使用联合解决 M:M(多对多)
服务时解决多对多关系?
如何在使用 GraphQL 联合微 活动。他们有m:m关系。 “User_Id”未存储在“Events”表中“Event_Id”未存储在“Users”表中。
在这里,我创建了 3 个微服务 + 1 个 Api 网关:
- 用户
- 事件
- User_Events(桥接表)
“user_events”表同时具有 id、用户和 ID。可以从此桥接表解析事件实体。
现在我关心的是:
(A)“user”表不包含“event_ids”,如何编写查询来获取用户详细信息&他/她参加的所有活动?
(B)“events”表不保存“user_ids”,如何编写查询来获取事件详细信息&所有用户作为其参与者?
请帮助我使用 graphQL 联合微服务解决此问题。
Resolve M:M (many to many) using Federation
How to resolve many to many relationships while using GraphQL Federation Micro-Services?
Lets say Users & Events. They have m:m relationship.
"User_Id" is not stored in "Events" table & "Event_Id" is not stored in "Users" table.
Here i have created 3 micro-services + 1 Api Gateway :
- Users
- Events
- User_Events (bridge table)
"user_events" table have both the ids, user & event entities can be resolved from this bridge table.
Now my concern is:
(A) "user" table does not hold "event_ids", how to write a query to get user details & all the events he/she is attending ?
(B) "events" table does not hold "user_ids", how to write a query to get event details & all the users as its attendees ?
Please help me to resolve this using graphQL Federation Micro-Services.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能对您有帮助:-) Apollo 服务器:将参数传递给嵌套解析器。
与联合服务的工作方式相同=> https://docs.nestjs.com/graphql/federation。
请注意,在不破坏关注点分离的情况下,目前不可能对联合多对多关系进行过滤/排序/分页: https://github.com/apollographql/federation/issues/359
This might help you :-) Apollo Server: pass arguments to nested resolvers.
Works the same way with federated services => https://docs.nestjs.com/graphql/federation.
Just notice that filtering/sorting/pagination of federated many to many relations is not possible at the moment without breaking separation of concern: https://github.com/apollographql/federation/issues/359
我不知道你使用什么框架,但在 NestJS 中是可能的。
如果 User_Events (桥接表)位于您的 Users 中,
您可以使用如下所示
在 User 服务中
在 Event 服务中
关键是在一个服务上实现resolveReference,并在另一个服务上返回 __typename。
如果考虑性能,可以在resolveReference中使用dataloader
I don't know what framework you use, but it is possible as below in NestJS.
If User_Events (bridge table) is in your Users
You can use like following
In User service
In Event Service
The key is to implement the resolveReference on one service and return __typename on the other service.
If you considering the performance, you can use dataloader in resolveReference