我正在尝试查找信息,但我不明白答案,所以我来了。我需要做一个无限嵌套的评论系统,但问题是——我需要一遍又一遍地把“孩子”写到我的模式中以获得孩子,但是有没有办法使它递归?
query GetComments($postId: Int!, $skip: Int!, $take: Int!) {
getComments(postId: $postId, skip: $skip, take: $take) {
id
createdAt
updatedAt
authorId
comment
parentId
postId
author {
name
id
image
admin
moder
}
children {
id
createdAt
updatedAt
authorId
comment
parentId
postId
author {
name
id
image
admin
moder
}
}
}
}
所以你看,如果我想在一个孩子中提取新的孩子,我需要写更多的孩子。
类型定义:
type User {
id: Int
name: String
image: String
admin: Boolean
moder: Boolean
}
type Comment {
id: String
createdAt: String
updatedAt: String
authorId: Int
comment: String
postId: Int
author: User
parentId: String
children: [Comment]
}
GraphQL
的目标是获取绝对必要的数据。因此,GraphQL
不支持递归。如果您处于需要递归获取 GraphQL 数据的位置,则需要通过重新访问模型来更改逻辑,因为它与 GraphQL 所代表的相反。
你可以参考这里, https://github.com/graphql/graphql-spec/issues/91#issuecomment-206743676