Querying on predicate by UID


(Jeff Hull) #1

Hi,

I would like to query on a predicate by UID, but I have not been able to find an example in the query language guide. Given this entity:

QUERY:
{
	person(id: <0x3e8efeb24ba23885>) {
		Name
		Description
		Class { _uid_ }
	}
}

RESULT:
{
    "person": [
        {
            "Class": [
                {
                    "_uid_": "0xd59531fa5ba5372b"
                }
            ],
            "Description": "Consultant and developer",
            "Name": "Jeff"
        }
    ]
}

I would like to write a query that will return all entities assigned the Class (“Class: uid .” in schema) with uid 0xd59531fa5ba5372b.

I tried this query and a couple other variations, but I get no results:

QUERY:
{
	entities(Class: <0xd59531fa5ba5372b>) {
		Name
		Description
		class: Class { _uid_ }
	}
}

RESULT:
{}

(Manish R Jain) #2

Not sure I understand the question. But, let me take a guess. You want to find all the other people in the same class as this person? If so, you need a reverse edge on Class, and you can run:

{
	person(id: <0x3e8efeb24ba23885>) {
		Name
		Description
		Class {
                  ~Class { Name, Description } # This would give you all people in the class.
                }
	}
}

(Jeff Hull) #3

Hi @mrjn,

Actually what I’m trying to do in general is build a richer schema on top of dgraph. The schema will have classes, properties (object is a literal), and relations (object is a uid). The schema will be modified by an admin through an application front-end, and each class (and property and relation) will have a UID in dgraph. Users, through normal activity, will then be creating and modifying entities, all of which have a class. In this case, the class “Person” is identified by UID <0xd59531fa5ba5372b>. I am building an API on top of Dgraph that modifies and accesses all of these entities in a simplified way, and the API endpoint “/entity/get/listByClass” will take in the UID of a class and return all entities with that class.

Rather than returning all of the entities with the same class as that person, I would like to return all entities with the class with UID <0xd59531fa5ba5372b>. Similar to your answer, but as the input I have available the class UID, not the person UID. I have defined a predicate called “Class” using the dgraph schema syntax, so I was hoping I could use that “Class” predicate I have defined to directly find all entities that exist in a triple: “[Entity] [Class] [UID from API]”

Does this help answer your question? Please let me know if I can clarify further. And as always, thanks for all of your help and what you guys are doing at Dgraph.


(Jeff Hull) #4

I just realized that @mrjn’s note was just one step removed from the query I needed. For anyone else trying to do the same thing, the answer was to declare a reverse edge in the dgraph schema with:

mutation {
  schema {
    Class: uid @reverse .
  }
}

And then to query on the class ID (<0xd59531fa5ba5372b>), like this:

{
	Results (id: <0xd59531fa5ba5372b>) {
		Entities: ~Class { 
			Name
			Description
			_uid_ 
		}
	}
}

Which then gives the desired result:

"Results": [
        {
            "Entities": [
                {
                    "Description": "Consultant and developer",
                    "Name": "Jeff",
                    "_uid_": "0x3e8efeb24ba23885"
                },
                {
                    "Description": "Staff Accountant",
                    "Name": "David",
                    "_uid_": "0x67792fc1fb416676"
                }
            ]
        }
    ]

(system) #5

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