Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested query to unknown level GraphQL [duplicate]

Tags:

graphql

I have a tree structure. Let's say Folder for example.

How do I query nested queries to N level using GraphQL.

Let's take an example as I have following properties in class.

public class DocumentField
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public List<DocumentField> Children { get; set; }
    }

Server side is designed in a way that if children are there then it will include as children. But graphql layer is restricting it because my query which is following.

Following query will not bring result of children.

query ($folderId: Int!) {
                              folder(folderId: $folderId) {
                                id,
                                name
                              }
                            }

Following query gives error as: nested query Field children of type DocumentFieldICollection must have a sub selection

query ($folderId: Int!) {
                          folder(folderId: $folderId) {
                            id,
                            name,
                            children
                          }
                        }
like image 505
Anonymous Creator Avatar asked May 26 '19 15:05

Anonymous Creator


People also ask

Why is GraphQL not widely used?

GraphQL makes some tasks more complex For example, in an application that uses a few fields the same way each time, using GraphQL adds more complexity because of things like types, queries, mutators, resolvers, and higher-order components, From a maintenance perspective, this is especially detrimental.

Can we provide arguments to every field and nested objects in GraphQL?

But in GraphQL, every field and nested object can get its own set of arguments, making GraphQL a complete replacement for making multiple API fetches. You can even pass arguments into scalar fields, to implement data transformations once on the server, instead of on every client separately.

What 2 pieces make up a GraphQL schema?

The GraphQLSchema object is the core of a GraphQL server When using any of these libraries, your development process is centered around a GraphQLSchema object, consisting of two major components: the schema definition. the actual implementation in the form of resolver functions.

What is an exclamation point in GraphQL?

By default, all value types in GraphQL can result in a null value. If a value type includes an exclamation point, it means that value cannot be null.


1 Answers

GraphQL does not allow to query such recursive structure down to all levels dynamically. You have to explicitly specify how many levels that you want to query. For example , to query to down 4 levels , the query looks liked :

query ($folderId: Int!) {
  folder(folderId: $folderId) {
    id
    name
    children {
       id 
       name
       children{
          id
          name
          children{
            id
            name
          }
       }
    }
  }
 }

You can use fragment to reduce some duplication in this query. For details , please see this.

like image 69
Ken Chan Avatar answered Nov 15 '22 07:11

Ken Chan