Fetch based on last relation

I am stuck in preparing query to fetch based on relation. I have a schema like this.

title: string @index(exact) .
actions: [uid] @reverse .
phase: string @index(exact) .
created_at: datetime .

type Job {
    title
    actions
}

type Action {
    phase
    created_at
}

Data like this (may not be syntactically correct, just for idea):

{
    "set": [
        {
            "title": "Job 001",
            "dgraph.type": "Job",
            "actions": [
                {
                    "phase": "review",
                    "created_at": "2020-04-20T09:30:00Z",
                    "dgraph.type": "Action"
                },
                {
                    "phase": "research",
                    "created_at": "2020-04-22T06:00:00Z",
                    "dgraph.type": "Action"
                },
                {
                    "phase": "confirm",
                    "created_at": "2020-04-25T10:20:00Z",
                    "dgraph.type": "Action"
                },
            ]
        }
    ]
}

As you can see, a Job has different actions review, research and confirm (in this order). I am trying to get the jobs that are in research phase. I think I need to order the actions on created_at with desc, check the first one and get the according jobs. But how would I do it in the query? Or is this possible to do it in a single query? Any idea will be really helpful.

Thanks

You can use reverse edge on actions to do something like this:

query {
    jobsInResearchPhase(func: type(Action)) @filter(eq(phase,"research")) {
    	uid
    	phase
    	jobs: ~actions {
    		uid
    		title
    	}
    }
}

It will give you all the jobs in “research” phase.

Thanks for your help. But, as I think, this will also list the jobs that already has the confirm phase. I am looking to get those jobs that do not have confirm phase yet. The last action of those jobs should be research. Am I able to make my point?

Ah! Does this help?

query {
    var(func: type(Action)) @filter(eq(phase,"confirm")) {
    	confirmedJobs as ~actions
    }
    
    jobsInResearchNotConfirmed(func: type(Action)) @filter(eq(phase,"research")) @cascade {
    	phase
    	created_at
    	jobs: ~actions @filter(not uid(confirmedJobs)) {
    		title
    	}
    }
}

I think that’ll do. Thank you very much.