How to determine the uid of a schema based on 3 other schemas?

I’m having trouble writing a query. could you help me figure out what the problem is and how to write a query better.
I created the following schema (why this is done is an interesting question, but so far I have indicated only the necessary fields):

client_id: string @index(exact, fulltext, trigram) @upsert .
client_secret: password .
has_keys: [uid] @count @reverse .
has_access: uid @reverse .
type: uid @reverse .
cabinet_name: string @index(exact, fulltext, trigram) @lang @upsert .
has_cabinets: [uid] @count @reverse .
belongs_to: uid @reverse .
inventary_number: int @index(int) @upsert .

type user {
	client_id: string
	client_secret: password
	has_keys: [cabinet_key]
}

type cabinet_key {
	has_access: cabinet
}

type cabinet {
	type: cabinet_type
}

type cabinet_type {
	cabinet_name: string
}

type organization {
	has_cabinets: [cabinet]
}

type office {
	has_cabinets: [cabinet]
	belongs_to: organization
}

type device{
	has_cabinets: [cabinet]
	belongs_to: office
	inventary_number: int
}

The most difficult part of the question for me. Let’s say we have a set of entities and I know the values of the client_id (user), uid (user), cabinet_name (cabinet_type) and inventary_number (device) fields. Having 4 of these fields, I need to find out the corresponding uid (cabinet_key).
I have already tried to write a query like this:

{
  dev as var(func: type(device)) @filter(eq(inventary_number, "13265165818136521"))
  cabinetsDevInv as var(func: type(cabinet)) @filter(eq(~has_cabinets, dev))
    
  cabinetTypeDev as var(func: type(cabinet_type)) @filter(eq(cabinet_name, "dev"))
  cabinetsDev as var(func: type(cabinet)) @filter(eq(type, cabinetTypeDev))  
    
  cabinets as var(func: type(cabinet)) @filter(eq(cabinetsDevInv, cabinetsDev))  
    
  cabinetsKeys as var(func: type(cabinet_key)) @filter(eq(has_access, cabinets))
    
  getPrachkomatKey(func: uid(0x759d)) {
    has_keys @filter(uid(cabinetsKeys)) {
      uid
    }
  }
}

But I’m having trouble understanding the syntax. Can you help me figure out what I should pay attention to and what I’m doing wrong?

In response I get the following:

Error Name: t

Message: Some variables are defined but not used Defined:[cabinetTypeDev cabinets cabinetsKeys cabinetsDev cabinetsDevInv dev] Used:[cabinetsKeys]

Raw Error:

{
  "name": "t",
  "url": "http://localhost:8080/query?timeout=20s&debug=true",
  "errors": [
    {
      "message": "Some variables are defined but not used\nDefined:[cabinetTypeDev cabinets cabinetsKeys cabinetsDev cabinetsDevInv dev]\nUsed:[cabinetsKeys]\n",
      "extensions": {
        "code": "ErrorInvalidRequest"
      }
    }
  ]
}
1 Like

Can you explain in words what kind of query you are trying to execute? It is slightly difficult to track among all these variables and schema. :slight_smile:

Hi @ForsaiR,

I think your query should look something like:

{
  find_cabinet_key(func: eq(cabinet_name, "<your-cabinet-name>")) {
      cabinet_id:~type {
          uid
          cabinet_key_id: ~has_access {
              uid
          }
      }
  }
}

Roughly speaking, I have 3 variables:

  • “uid” or “client_id” of type user;
  • “cabinet_name” (name of the cabinet used) of type “cabinet_type”;
  • “inventary_number” (unique device number) of type “device”.

Let’s say “cabinet_name” = “phone”, and “inventary_number” = “123”.
First of all, I need all the “cabinet” referenced by “device” having “inventary_number” = “123”. In this case, there should be only those “cabinet” that refer to “cabinet_type” with the field “cabinet_name” = “phone”. After that, we need all the cabinet_keys that refer (has_access) to the received cabinet. Well, perhaps the final step is to determine the only true “uid” of the type “cabinet_key” to which the “user” type refers (“has_keys”) based on its “uid” or “client_id”.
Hope clearly explained. If there was an opportunity, I would add pictures.

2 Likes

I think what @Rahul has suggested should work, another way to write those queries would be as follows:

#We store cabinets from the device with a given inventory number in a variable `cabinets`

var(func: eq(inventory_number, 123)){
 cabinets as has_cabinets
}

#We store the relevant cabinet_keys from the `cabinets` 
var(func: uid(cabinets)){
 cabinet_keys as ~has_access
}

#Finally get client_ids which have access to the above keys.
GetUsers(func: uid(cabinet_keys)){
 ~has_keys{
    uid
    client_id
  }
}
1 Like

Thank you, very much helped.