【发布时间】:2017-06-27 07:20:17
【问题描述】:
我正在学习 graphql,我想我发现了它的一个缺陷。 假设我们有这样的架构
type Hero {
name: String
friends: [Person]
}
type Person {
name: String
}
还有两个查询
{
hero {
name
friends {
name
}
}
}
还有这个
{
hero {
name
}
}
还有一个关系型数据库,它有两个对应的表Heros 和Persons。
如果我的理解是正确的,我无法解决此查询,因此对于第一个查询,生成的 sql 查询将是
select Heros.name, Persons.name
from Heros, Persons
where Hero.name = 'Some' and Persons.heroid = Heros.id
第二个
select Heros.name, Persons.name from Heros
这样只有查询真正需要的字段才会从数据库中加载。
我说的对吗? 此外,如果 graphql 能够仅返回查询所需的数据,而不是对完整架构有效的数据,我认为这是可能的,对吧?
【问题讨论】:
-
@p0k8_ 为什么不呢?
-
@p0k8_
it's not good make own custom batching request to database, because the user may not query for the nested type。如果用户没有查询嵌套类型,则 sql 中不应该有任何连接,正如我在第二个查询中所展示的那样。要么你不明白我在问什么,要么我不明白你在说什么。 -
这正是我写Join Monster时想要解决的问题。它仅在实际请求嵌套类型时生成 SQL 并动态添加连接。它可以任意深入并使用 postgres(很快还会使用 mariadb)进行分页。