Conditionally treating array response as singular value

What I want to do

A knowledge base like Wikidata has properties that are reusable independent of singular or plural usage. I.e. they don’t distinguish between [uid] and uid at a global level but leaves that to context-specific constraints (for example in the context of a certain class, the property is restricted to a singular value).

Take for example the property headOfGovernment and it’s inverse headOfGovernmentOf documented here:

This property is for example used as {USA} headOfGovernment {JoeBiden}:

https://www.wikidata.org/wiki/Q30

As you can see, the property allows for multiple values and a facet called “rank=preferred” (preferred rank - Wikidata) is used to treat Joe Biden as the most actual value. (That’s what the little up-arrow indicates.)

image

It would likely involve a lot of redundancy for Wikidata and similar knowledge graphs to define both singular and plural properties and their inverses – imagine having to define something like the below for the same use case:

headOfGovernment <> headOfGovernmentOf
headsOfGovernment <> headsOfGovernmentOf
or:
headOfGovernment <> headOfGovernment
historicalHeadsOfGovernment <> historicalHeadsOfGovernmentOf

This will quickly lead to a breakdown of semantics and redundancy where new properties are introduced, instead of constrained as needed.

The query layer

So let’s say we treated all properties in Dgraph the way Wikidata does (not making an a priori assumption about how many values are given).

What problems would we then face at the query layer when it comes to sort, group, aggregate operations that I guess assume a singular value to operate on?

Take again the example this property configured as multi-value (in both directions):

headOfGovernment [uid]
headOfGovernmentOf [uid]

How could one now query for something like “Sort all headOfGovernment (with rank=“preferred”) by name?”

The problem here, I assume is that the resulting set of property values is an array but we want to treat it as a singular value. In other words it seems we want a mechanism to determine (at query-time) whether we are certain that we only want to operate on a single value (the first or only returned) versus an array.

I’d be very happy for any thoughts about how such a strategy would play out with DQLs capabilities. And more specifically about how to treat or cast an array response as a single value.

I’m attaching some reference data below for any experimentation:

Reference data

{

  "set": [

    {
      "dgraph.type" : "Country",
  		"name" : "USA",
  	  "uid": "_:USA",
  
      "headOfGovernment" : [
  			{
     "dgraph.type" : "Human",
  			"uid": "_:JoeBiden",
      	"name": "JoeBiden",
  			"headOfGovernment|rank" : "preferred"
},
{
       "dgraph.type" : "Human",
  			"uid": "_:DonaldTrump",
      	"name": "DonaldTrump",
  			"headOfGovernment|rank" : "default"
},
{
       "dgraph.type" : "Human",
  			"uid": "_:BarackObama",
      	"name": "BarackObama",
  			"headOfGovernment|rank" : "default"
				}
        
			]
    }
    
  ]
}