GraphQL基于父节点的最佳过滤方式
因此,客户机需要使用这种结构的响应GraphQL基于父节点的最佳过滤方式,graphql,Graphql,因此,客户机需要使用这种结构的响应 user |_ trophies <-- user earned trophies |_ trophy_count: Int <-- how many times a user earned a trophy 用户 |_奖杯是个好办法 你需要中间的类型。 type User { trophies: [UserTrophy] } type UserTrophy { trophy: Trophy trophy_coun
user
|_ trophies <-- user earned trophies
|_ trophy_count: Int <-- how many times a user earned a trophy
用户
|_奖杯是个好办法
你需要中间的类型。
type User {
trophies: [UserTrophy]
}
type UserTrophy {
trophy: Trophy
trophy_count: Int
}
type Trophy{
# only trophy related properties
}
然而,客户的查询将发生一些变化
User{
trophies{
trophy{
...
}
trophy_count
}
糟糕的解决方案(避免)
如果您必须按照上面的响应结构返回,那么有一些方法,但它们的可维护性/可读性较差,如果以与您预期不同的方式查询,则可能会导致问题
将user\u id
作为参数传递给trophy\u count
(查询多个用户时无效)
in-tropy\u计数解析程序
if(typeof parent.user_id == 'undefined') throw new Error("Must be queried with user type");
// continue logic ...
从不在上下文中传递用户\u id?查询执行水平进行(在加载奖杯计数之前加载奖杯之前,将加载所有用户),这意味着用户将覆盖上下文,并且只有最后一个用户id将实际传递给所有用户的奖杯计数
解析程序
请随意提出任何其他解决方案
User{
trophies{
...
trophy_count(user_id: ID) # <-- filter by user_id in the resolver
}
return {
...trophy,
user_id: parent.id
};
if(typeof parent.user_id == 'undefined') throw new Error("Must be queried with user type");
// continue logic ...