graphqldgraph

How to do nested query filtering in graph


I have some movie data in my Dgraph

[
    {
        name: movie1
        release: 2016
        mainActor: {
            uid: 0x12
            name: actor1
            birth: 1990
        }
    },
    {
        name: movie2
        release: 2017
        mainActor: {
            uid: 0x15
            name: actor2
            birth: 1991
        }
    },
    {
        name: movie3
        release: 2018
        mainActor: {
            uid: 0x12
            name: actor1
            birth: 1990
        }
    }
]

I want to get all movies that have mainActor set as actor1. This is the query I have currently:

{
    movies(func: type(Movie)) {
        name
        release
        mainActor @filter(uid(0x12)) {
            name
            birth
        }
    }
}

Expected output:

[
    {
        name: movie1
        release: 2016
        mainActor: {
            uid: 0x12
            name: actor1
            birth: 1990
        }
    },
    {
        name: movie3
        release: 2018
        mainActor: {
            uid: 0x12
            name: actor1
            birth: 1990
        }
    }
]

However, the actual output is a bit weird and doesn't do what I want it to.

[
    {
        name: movie1
        release: 2016
        mainActor: {
            uid: 0x12
            name: actor1
            birth: 1990
        }
    },
    {
        name: movie2
        release: 2017
    },
    {
        name: movie3
        release: 2018
        mainActor: {
            uid: 0x12
            name: actor1
            birth: 1990
        }
    }
]

What am I doing wrong here? I want to filter out the main result based on the nested object.


Solution

  • As it turns out, the @cascade(paramName) can be used to filter out results that don't have a specific property. In my case, since I don't have a mainActor after filtering them out by uid, I could use @cascade(mainActor) to remove all movies that don't have a mainActor predicate.

    {
        movies(func: type(Movie)) 
        @cascade(mainActor)
        {
            name
            release
            mainActor @filter(uid(0x12)) {
                name
                birth
            }
        }
    }