简体   繁体   English

使用 golang 在 graphql 中递归

[英]recursion in graphql using golang

i am using https://github.com/99designs/gqlgen in golang for graphql, want to create an api that returns tree like structure, however i want to give a depth control instead of writing nested selections in the queries.我在 golang 中使用https://github.com/99designs/gqlgen为 graphql,想创建一个 api,但我想在查询中给出一个返回树状结构的深度,而不是写嵌套选择。 is there a way to achieve this?有没有办法做到这一点? Want to achieve some thing like this,想要实现这样的事情,

query listAll{
  node: getNodes(parentId: "1235") {
    ID
    Name
    node: Children @recursive(depth: 10) {
        ID
        Name
    }
  }
}

GraphQL has a principle for explicitly asking for what you need; GraphQL 有一个原则是明确询问你需要什么; it has no "recurse" directive.它没有“递归”指令。 In other words, if you want nodes to the depth of 10, you'll have to write a query with 10 nestings.换句话说,如果您想要深度为 10 的节点,则必须编写一个包含 10 个嵌套的查询。 Obviously you can generate the text of the query from your program to be whatever you wish, dynamically.显然,您可以从程序中动态生成任何您想要的查询文本。

If you're worried about the potential complexity of these queries and how much data they can demand, check out gqlgen's FixedComplexityLimit extension as described in the documentation .如果您担心这些查询的潜在复杂性以及它们可能需要多少数据,请查看 gqlgen 的FixedComplexityLimit扩展,如文档中所述。

func main() {
    c := Config{ Resolvers: &resolvers{} }
        srv := handler.NewDefaultServer(blog.NewExecutableSchema(c))
        srv.Use(extension.FixedComplexityLimit(5)) // This line is key
    r.Handle("/query", srv)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM