Edge data format?

Hi everyone.

I have a question about my data set. I thought I had everything figured out:

Types : 
Person 
  firstName
  lastName
  gender

Comment
  commentDate 
  length
  location

I have an edge file that looks like this:

{
  "set": [
    {
      "Personid": 32985348833579,
      "likesComment": {
        "Commentid": 2061584302097,
        "likesComment|likeDate": "2012-09-07T16:44:37.850+0000"
      }
    },
...

I’m trying to query all the people who have this edge “likesComment”, but no luck:

{
  q(func: type(Person))) {
    likesComment {
	Commentid
    }
  }
}

Is my data format set up incorrectly or am I querying incorrectly?

Thanks,
Kevin

Does your mutation/dataset has the dgraph.type?

e.g

{
  "set": [
    {
      "Personid": 32985348833579,
      "dgraph.type": "Person",
      "likesComment": {
        "Commentid": 2061584302097,
        "likesComment|likeDate": "2012-09-07T16:44:37.850+0000",
        "dgraph.type": "Comment"
      }
    },
...

Hi Michel,

I loaded the types separate from the predicates. The Person file has the dgraph.type, and the Comment file also has dgraph.type.

Is the dgraph.type also necessary for the edge/predicate file?

No, just the entities.

humm, feels like something is missing somewhere.

I’m not sure if I got what you mean by this pseudo-schema. Can you make sure to make it with the syntax?

e.g. (confirm this - are all those preds on your schema?)

type Person  {
  Personid
  firstName
  lastName
  gender
  likesComment
}

type Comment {
  Commentid
  commentDate 
  length
  location
}

Here is a snippet from my Person file:

{
  "set": [
    {
      "Personid": 933,
      "firstName": "Mahinda",
      "lastName": "Perera",
      "gender": "male",
      "birthday": "1989-12-03",
      "creationDate": "2010-02-14T15:32:10.447",
      "locationIP": "119.235.7.103",
      "browserUsed": "Firefox",
      "language": "en",
      "email": "Mahinda933@boarderzone.com;Mahinda933@hotmail.com;Mahinda933@yahoo.com;Mahinda933@zoho.com",
      "dgraph.type": "Person"
    },
...

My personLikesComment file looks like this:

{
  "set": [
    {
      "Personid": 32985348833579,
      "likesComment": {
        "Commentid": 2061584302097,
        "likesComment|likeDate": "2012-09-07T16:44:37.850+0000"
      }
    },
...

So are you’re saying I need to include the predicate “likesComment” as a Person field? Many of my predicates are created upon mutation, because they are not part of the data within the Type files, but part of the edges instead.

Yes, the edges that connects nodes must go in the schema.

If i include it as a field on the type, will I need to include it when I’m loading the Person types?

Something like this :

type Person  {
  Personid
  firstName
  lastName
  gender
  likesComment {
    commentid
  }
}

No, Dgraph’s type system doesn’t have nested. See what I have shared, it has to be like that.

Ok, got it.

So if I include all the predicates onto the the types, I’ll need to reload and it should all be connected right?

What defines what is connected is the edge. If you have already created the edge, so now you need to set it on the Type definition on the schema (actually all should be already done on your Graph Modeling).

But, I’m not sure if that is the case. Something feels odd here. It should work even without the schema. Cuz the edge exists and you have set a minimum Type Definition. Not sure, try out what I said and let me know.

What I would see as an issue is by using expand(all) and other funcs.

Wait a minute, let’s recap.

So, you already have the users. Right? and this mutation

Would be your kind of “create the relation” right?

If that so, that’s wrong. Cuz you are not using a real UID or using an Upsert Block. This separation will create new nodes and not relations. You are creating 2 new nodes every time you run that mutation. And only those two will be connected by them selfs.

Let me know exactly what you are doing step by step.

So I would need to create the relation by using the UID? Is there a way i could directly use the personid as the uid to avoid this complication?

Yes, see this if you wanna understand the basic CRUD op Graph Database: Performing CRUD operations with Dgraph (2019) - YouTube

Yes, using upsert block https://dgraph.io/docs/mutations/upsert-block/

@wood, BTW, why have you created this question at Users/GraphQL? It should be at Users/Dgraph (moved)

Is there a way to do the upserting with live loader? The examples seem to handle it one case at a time…
Would reformatting my data a certain way enable me to automate the loading process any further?

I thought there was an issue with my query… would it be better to put no topic?

No as far I can tell. There is this PR feat(live): added upsert in live loader by harshil-goel · Pull Request #6057 · dgraph-io/dgraph · GitHub which I don’t get a chance to understand how it works. It is on master only.

Nope, Dgraph works like this. Only Upserts can help you.

Didn’t got your question. What do you mean?

Creating the question under the Users/GraphQL topic. I wasn’t sure the exact topic to create it as, sorry :frowning:

I’m afraid the know the answer to this… are you saying I have to manually insert all the edges?

Okay, that’s fine. When a user chooses such topic I assume that he is familiar with it. But by reading your issue I notice that it isn’t related to GraphQL. So, all questions of any kind should go first to Users/Dgraph. And then we can move it (the staff) if needed.

But no problem at all.

You can use upsert for this. See, custom IDs aren’t handled by Dgraph. So Dgraph can’t be aware of its nature, patterns, standards, and so on. But you can still use your custom IDs, now you just need to learn how to use Upsert Block on daily basis.

Got it, i’ll put new questions in the Dgraph topic.

I see, so there’s essentially no point of having UID for each Personid because DGraph will create its own UID upon loading. And in order to create a link from one Person object to another type, I would need to know the exact UID within the system to avoid creating a duplicate Person - am I getting this correctly?

One question, does your child data knows about the parent? I mean, to be able to link them without going one by one. One of the nodes needs to be aware of the other, so we can get that information and upsert right away. If not so, not even with dark magic would be possible.

See this query:

upsert {
  query {
    q(func: eq(Personid, "32985348833579")) {
      v as uid
    }
    q2(func: eq(Commentid, "2061584302097")) {
      v2 as uid
    }
  }

  mutation {
    set {
      uid(v) <likesComment> uid(v2) (likeDate="2012-09-07T16:44:37.850+0000") .
    }
  }
}

That would be a simple example. But to do an upsert that connects every node. Some of them have to be the source of truth of the context. If your data comes from other DB, maybe they export that info. Need to evaluate.

I think you mean having “custom” ID system right? Yeah, in that case it’s up to you to deal with other IDs systems. I see that other users use third party IDs, cuz they combine Dgraph with other DBs. So, in their case makes sense they do that work.

Precisely.