简体   繁体   中英

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. 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; 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. 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 .

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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