Problem retrieving all related information when querying by uid

Hi,
Really like Dgraph so far, most things are working great. There’s one thing that I can’t seem to figure out, not sure if it’s schema related or the way I create object.

My probem put very simply:
example, three objects I’d like to work with, they have:

objectTitle
objectDescription

two of these objects have the same title and description and when I query with the uid of one of those it only shows me the uid. Whereas when I query with the third unique object uid I can find all related information.

all object have a unique dgraph uid. when I query with another field (e.g. createdBy) I can find all three objects and the related data without any issues.

what’s going on? How can I resolve this?

This is my query:

{ object(func: uid(0x2711)){
uid
objectTitle
objectDescription
}}

[UPDATE] When searching by objectTitle I can find both of those objects with the related data.
{
(func: eq(objectTitle,“this is the object title”)
createdBy
uid
objectTitle
objectDescription
}
}
So it seems like searching by UID is not the right way to do it, looking forward to some more insights on this :slight_smile:

Based on what you described, I took the liberty of creating this to discuss with you. Any doubts about what I have created feel free to ask.

Schema

name: string @index(exact, fulltext) @count .
age: int @index(int) .
likes: uid @reverse @count .
answer: uid @reverse @count .
post.title: uid @reverse .
post.Description: uid @reverse .
owner.post: uid @reverse .
title: string @index(exact, fulltext) .
body: string @index(exact, fulltext) .

Mutation

{
  set{
    
    _:Author <name> "Lucas De Niro" .
    _:Author <type.user> " " . #Giving to node a type
    _:Author <age> "31" .

    _:Post <type.post> "" . #Giving to node a type
    _:Post <likes> <ox1> . #example of a user with UID ox2 giving a automatic like to that post
    _:Post <answer> <ox15> . #example of an answer
    _:Post <post.title> _:Title . #Normal relationship | But you need to @Reverse in case you go searching for the title or the content of the description.
    _:Post <post.Description> _:Description . #Normal relationship
    _:Author <owner.post> _:Post . #Relationship in @reverse | Author is Post's parent
    
    _:Title <title> "This is a title" .
    _:Title <type.title> "" . #Giving to node a type

    _:Description <body> "This is a very large description." .
    _:Description <type.desc> "" . #Giving to node a type

  }

Queries

{
  Users(func: has(type.user)){
    expand(_all_) { 
      expand(_all_) {
       expand(_all_) 
      } 
    }
  }
}
{
  search(func: has(type.title)) @filter(eq(title, "this is the object title”")) {
    ~post.title : postFound {
        likes  { uid  name }
        answer { uid #or expand for more 
           }
       post.Description {
          body #or expand for more if needed
         }
       post.title { title #or expand for more 
        }
      }
  }
}

Other way (using “expand” to ““debug”” the querie)


{
  search(func: has(type.title)) # @filter(eq(title, "This is a title")) 
    {
      uid
     expand(_all_)
  }
}

Best Way

{
  var(func: has(type.title)) @filter(eq(title, "This is a title")) {
    uid
 ~post.title {
    PostFound as uid }
  }

MyPost(func: uid(PostFound)) {
      uid

      likes  { uid  name }
      totalLikes : count(likes)

      answer { uid #or expand for more 
       }
      totalAnswers : count(answer)

      post.Description {
          body #or expand for more if needed
         }
      post.title { title #or expand for more
      }
  }

}

Two objects can not occupy the same space. Unless this UID you placed is from a Parent Node and the others are child nodes that you need to expand.

No two matters can occupy the same space at the same time. Sir Isaac Newton

Oh wow , this made me completely rethink how I’m structured things. Somehow missed the part that nodes can have types and didn’t realize I can set the schema like that (post.Description)!

Thank you for your help Michel and perhaps also Newton. I’ll try to adapt my json mutations to this “paradigm”

Your paradigm is so intrinsic to your mental process that you are hardly aware of its existence, until you try to communicate with someone with a different paradigm.

Donella Meadows, The Global Citizen

1 Like

There are several ways you can use Dgraph. This is a pattern that correlated with what we have in the graphoverflow example. The only difference is the “type” that it uses a indexed and unique predicate and not by empty predicates. That we can locate via “has( )”

FYI:

{
q(func: has(type.user)) {
uid
}
}

will not return results (for user,post or any other type that is specified). This syntax from the docs does seem to work:

{
q(func: has(user)) {
uid
}
}

You are only requesting the UID in the query. Add more predicates, such as “name, age, and so on”

I picked up the mutation I created above, I runned on my machine. Then I used a query and see what dgraph gave me.


{
  "data": {
    "Users": [
      {
        "name": "Lucas De Niro",
        "age": 31,
        "type.user": " ",
        "owner.post": [
          {
            "likes": [
              {
                "option": "a) Major Client"
              }
            ],
            "type.post": "",
            "post.Description": [
              {
                "body": "This is a very large description.",
                "type.desc": ""
              }
            ],
            "post.title": [
              {
                "title": "This is a title",
                "type.title": ""
              }
            ],
            "~owner.post": [
              {
                "name": "Lucas De Niro",
                "age": 31,
                "type.user": " "
              }
            ]
          }
        ]
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "processing_ns": 1000600
    },
    "txn": {
      "start_ts": 114,
      "lin_read": {
        "ids": {
          "1": 19
        }
      }
    }
  }
}

oh!, some things in the JSON are confusing because I was already using this machine. Then there were some collisions.

I updated some queries after I ran tests.

Note that copying and pasting directly from the browser (Especially from discuss) may occur of copying characters that are not recognized by Dgraph. Just make sure.

Got it, I must have done something wrong or perhaps it’s because i’m running a version that is couple months old. when I was trying it I tried expand(all) like that and also adding the user when I was working on that, but in the end just decided to go with has(user), could that be problematic later on?

On a positive note, seems like I’ve got things sorted out much better now! No conflict and retrieving, both individual objects , objects related to a user, creating new objects is going well

appreciate your help, going to look into updating existing objects next

Not really, it’s your call. You must decide your patterns. Just plan it all.

Nice!

you are welcome!