给定“用户有许多链接”(这意味着链接是由用户创建的) DB实体关系,我想开发API来获取与用户一起的链接,以便返回的数据不包含重复的用户。
换句话说,而不是这个请求:
query {
links {
id
user {
id email
}
}
}
这将返回以下数据:
{
"data": {
"links": [
{
"id": 1,
"user": {
"id": 2,
"email": "user2@example.com"
}
},
{
"id": 2,
"user": {
"id": 2,
"email": "user2@example.com"
}
}
]
}
}
我想提出这样一个请求(请注意“引用”列):
query {
links {
id
userId
}
references {
users {
id
email
}
}
}
返回没有重复项的关联用户:
{
"data": {
"links": [
{
"id": 1,
"userId": 2
},
{
"id": 2,
"userId": 2
},
],
"references": {
"users": [
{
"id": 2,
"email": "user2@example.com"
}
]
}
}
}
这应该会减少客户端和服务器之间传输的数据量,从而增加一些速度提升。
这个想法在任何语言上都有现成的通用实现吗?(理想情况下,查找Ruby)
发布于 2021-04-21 13:15:01
它不是查询或服务器角色来规范化数据。
..。但是你可以实现一些:
edges
/nodes
,仅限nodes
或两者兼而有之); links {
egdes {
node {
id
title
url
authorId
# possible but limited usage with heavy weights
# author {
# id
# email
# }
}
}
pageInfo {
hasNextPages
}
referencedUsers {
id
email
}
}
其中:
User
有类型,props;referencedUsers
is [User!]
User
type;node.author
is User
类型;标准化Graphql客户端,如Apollo
,可以轻松地访问缓存的用户字段,而无需单独发出请求。
你可以渲染(react?)一些<User/>
组件(在<Link />
组件中)将node.authorId
作为参数传递,如<User id={authorId} />
。用户组件可以通过useQuery
挂钩cache-only
策略来读取用户属性/字段。
请参阅Apollo文档了解详细信息。您应该自己实现它,并将其记录下来,以帮助/指导API用户。
https://stackoverflow.com/questions/67195239
复制