How to save multiple query variables result as separate field in custom data structure as a result?

I’m trying to combine multiple queries into single query. In this query I’m returning a custom data structure that every field in this data structure is related to the result of one of those query variables. I am already done the query section but I don’t know how to return the result.

{
    A as var(...) {} //Already wrote this query.
    B as var(...) {} //Already wrote this query.

    // I don't know how to return those results.
    custom returned data structure {
        fieldA = As 
        fieldB = Bs
    }
}

Hi Saeed,

For what purpose?

I do not fully understand the purpose of the last block.

From what I understand of your example, it’s not possible to do this in Dgraph. Ideally you run the first two queries in the form of blocks. (No need for vars)

A “custom” block in Dgraph could only be done for the purpose of aggregation. In this case only mathematical operators could be used.

But it may be possible to support this custom usage. But you would have to make us understand this usage, how it would benefit others to spend time developing this feature.

Anyway, I recommend that you open a detailed issue in its usage according to the model described in the issues template.

if not, just use two simple blocks.

Hi Michel,
I have multiple group of nodes. Instead of retrieving every group with single query. For the performance reason, I’m trying to do those multiple queries in single query. So what is the idiomatic way to return every group’s result as JSON object field’s value.

So try like this:

{
    A as var(...) {} //Already wrote this query.
    B as var(...) {} //Already wrote this query.

    allResults(func: uid(A, B) ) {
        uid
        expand(_all_)
    }
}

For the Dgraph there is no problem of performance to have different query blocks.

1 Like

Is it ok to have multiple result blocks in root block, such as:

{
    A as var(...) {} //Already wrote this query.
    B as var(...) {} //Already wrote this query.

    aResult(func: uid(A) ) {
        uid
        expand(_all_)
    }

    bResult(func: uid(B) ) {
        uid
        expand(_all_)
    }
}

So, look. Since you wanna two “aResult and bResult” blocks
there is no need to use Var blocks. Then do the queries directly.
No need to do this whole round trip at all.

1 Like

Yes you are absolutely right, But I have to traverse the graph to find the result. That is why I’m using var blocks. In my example, I just wanted to describe my situation in very simple example.
Than you, Michel, I got the idea.

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