Query to return empty lists?

Is there a way to return empty lists in a query? For example, if I have a simple User type like so:

type User {
    username: string
    has_friend: [User]
}

and set the username to Alice but don’t set any friends, then when I run the query:

{
    q(func: type(User)) {
        username 
        has_friend {
            username
        }
    }
}

I receive the following response in JSON:

{
    "data": {
        "q": [
            "username": "Alice"
        ]
    }
}

What would be ideal is to receive a response where has_friend is returned as an empty array, like so:

{
    "data": {
        "q": [
            "username": "Alice",
            "has_friend": []
        ]
    }
}

As this is a simple example, it would be easy to simply check on the client side if data.q[0].has_friend exists and set it to an empty array if it didn’t. But this becomes difficult for queries that are expected to return deeply nested results. I would have to check, for example, if data.q[0].has_friend && data.q[0].has_friend[i].likes_movie && data.q[0].has_friend[i].likes_movie[j].has_director ... exists in a database with a more complex schema. And that becomes more and more of a nightmare the deeper the nesting goes.

So is there a way to have queries return empty lists? Or is there an efficient way to transform this data on the client so that empty lists are included, regardless of nesting depth? Thank you.

I don’t think that an easy way exists in raw Dgraph. However, if you really care about types and empty lists, then our native GraphQL might be what you need : https://graphql.dgraph.io/

It handles lists like you are wanting.

Awesome, I will test it out. Thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.