Groupby inside block without displaying the result

Let say I have the following schema:

<asset>: default .
<a.label>: string @index(term) @lang .
<a.tag>: uid .

<tag>: default .
<t.label>: string @index(exact) .

<user>: default .
<u.asset>: uid @count @reverse .
<u.name>: string .

And I have the following query (working fine):

query all($userId: string) {
	profile(func: uid($userId)) {
		name: u.name
		total_assets: count(u.asset)
	}

	var(func: uid($userId)) {
		assetIds as u.asset
		u.asset @groupby(a.tag) {
			tagsCount as count(uid)
		}
	}

	assets(func: uid(assetIds)) {
		label: a.label
	}

	tags(func: uid(tagsCount)) {
		label: t.label
		count : val(tagsCount)
	}
}

How do I move everything inside var block to be inside profile block? I tried this:

	profile(func: uid($userId)) {
		name: u.name
		total_assets: count(u.asset)
		assetIds as u.asset
		UNUSEDVAR as u.asset @groupby(a.tag) {
			tagsCount as count(uid)
		}
	}

But Ratel complains:

Message: Some variables are defined but not used
Defined:[assetIds tagsCount UNUSEDVAR]
Used:[assetIds tagsCount]

The reason I put UNUSEDVAR is to suppress it from appearing in the result of profile.

Side question: Is this query performant/memory-efficient? Considering I need to retrieve all assetIds to use it in assets().

You cannot suppress the output of stuff you put inside a named query block (e.g profile in your case). If you need to declare variables but not have them show up in the results you need to use the var block.

For the other question, the performance should be linear on the number of assets. But getting the list is not very expensive (Dgraph is already storing the list of values for this predicate-uid pair) and using the uid function itself is cheap once you have those assets. I would say performance would be fine (unless you are retrieving a huge list). It’s not something I would worry about at this point.

1 Like

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