解析器只返回你让它返回的东西。它不是根据类型自动生成的。它的作用是:它根据您解析的对象匹配对象的类型。
在这种情况下,您的解析器将返回一个用户列表,每个用户都有一个名称和一个帖子列表,每个帖子都有一个标题、正文和作者。
是的,它似乎可以创建无限的数据流,但这正是graph 背后的想法,即所有数据都与其关系相连,反之亦然。
你可以想象返回数据是这样的:
data: {
users: [
{
name: "user 1",
posts: [
{
title: "post 1",
body: "post body",
author: {
name: "user 1",
posts: [
{
title: "post 1",
body: "post body",
author: {
name: "user 1",
posts: [
{
.....
}
]
}
}
]
}
}
]
},
{
name: "user 2",
posts: [
{
title: "post 2",
body: "post body",
author: {
name: "user 2",
posts: [
{
title: "post 2",
body: "post body",
author: {
name: "user 2",
posts: [
{
.....
}
]
}
}
]
}
}
]
}
]
}
同样,GraphQL 只返回您要求它返回的数据。因此,如果您只想要用户和帖子,则查询将类似于:
query getUsers {
users {
name
posts {
title
body
}
}
}
在这种情况下,它只会返回用户及其帖子,而不是作者。