【发布时间】:2020-07-15 11:05:19
【问题描述】:
我需要将多个 graphQl 服务(具有相同架构)“聚合”为单个只读(仅查询)服务,从而公开来自所有服务的数据。例如:
---- domain 1 ----
"posts": [
{
"title": "Domain 1 - First post",
"description": "Content of the first post"
},
{
"title": "Domain 1 - Second post",
"description": "Content of the second post"
}
]
---- domain 2 ----
"posts": [
{
"title": "Domain 2 - First post",
"description": "Content of the first post"
},
{
"title": "Domain 2 - Second post",
"description": "Content of the second post"
}
]
我知道“拼接”并不是针对这样的 UC,而是更多地将不同的微服务组合到同一个 API 中。为了在单个 API 中使用相同的类型(名称),我通过动态地将域名附加到所有数据类型来实现“穷人命名空间”。但是,我只能使用以下两种不同类型进行查询:
query {
domain_1_posts {
title
description
}
domain_2_posts {
title
description
}
}
但是,它的数据集包含两个数组:
{
"data": {
"domain_1_posts": [
{ ...},
],
"domain_2_posts": [
{ ...},
]
}
}
我想听听您的想法,我可以做些什么来将它组合成仅包含 posts 的单个数据集?
一种想法是添加自己的解析器,它可以调用实际的解析器并将结果组合到单个数组中(如果完全支持的话)。
此外,作为 B 计划,我可以发送“域”参数进行查询,然后构造对第一个或第二个域的查询(但是,为了保持初始查询“与域无关”,例如在查询本身中不使用域名?
提前感谢所有建议...
【问题讨论】:
-
您可以像用户令牌一样在标头中发送域名(使用 apollo 链接)
-
谢谢@xadm 的建议,我会研究链接的可能性
标签: express graphql api-design