Beginner question/error with reverse edges

Hi there! I’m quite new with GraphQL and DGraph and to this point I’ve encountered a couple problems when playing with a basic graph.

What I want to do

The objective is to create a graph of Users connected to Phones, where each user node can be connected to multiple phones and viceversa. I’m also adding a facet into the edges to track the registration time of the user-phone link. Pretty simple overall.

What I did

First of all, I’m using Dgraph’s standalone image in Docker.
docker run -p 8000:8000 -p 9000:9000 -p 8080:8080 -p 9080:9080 -p 5080:5080 -it --name dgraph-learn -v ~/dgraph-learn/dgraph:/dgraph dgraph/standalone:latest

Then I build the graph schema through Ratel:

<customer.country_code>: string .
<customer.id>: int @index(int) .
<customer.name>: string @index(term) .
<customer.registration_time>: datetime .
<dgraph.drop.op>: string .
<dgraph.graphql.p_query>: string @index(sha256) .
<dgraph.graphql.schema>: string .
<dgraph.graphql.xid>: string @index(exact) @upsert .
<link_customer_phone>: uid @reverse .
<phone.country_code>: string .
<phone.phone_number>: int @index(int) .
<phone.registration_time>: datetime .
type <Customer> {
customer.country_code
customer.id
customer.registration_time
customer.name
}
type <Phone> {
phone.country_code
phone.phone_number
phone.registration_time
}
type <dgraph.graphql> {
dgraph.graphql.schema
dgraph.graphql.xid
}
type <dgraph.graphql.persisted_query> {
dgraph.graphql.p_query
}

And finally I just load two example users connected to the same phone, again through a Ratel mutation using blank nodes:

{
  set {
    
    # -- Customers
    _:u1 <customer.id> "000002" .
    _:u1 <customer.name> "Bob" .
    _:u1 <customer.country_code> "ES" .
    _:u1 <customer.registration_time> "2002-02-02" .
    _:u1 <dgraph.type> "Customer" .
    _:u2 <customer.id> "000003" .
    _:u2 <customer.name> "Charlie" .
    _:u2 <customer.country_code> "IT" .
    _:u2 <customer.registration_time> "2003-03-03" .
    _:u2 <dgraph.type> "Customer" .

    # -- Phones
    _:p1 <phone.phone_number> "222222222" .
    _:p1 <phone.country_code> "ES" .
    _:p1 <phone.registration_time> "2002-02-02" .
    _:p1 <dgraph.type> "Phone" .

    # -- Links & Facets
    _:u1 <link_customer_phone> _:p1 (since="2002-02-02") .
    _:u2 <link_customer_phone> _:p1 (since="2003-03-03") .
      
    }
}

To this point, everything works smoothly, and simple queries work. However, if I try to get all the Users connected to phone “222222222”, the Docker container stops with an error (exited 255). This is the query I am using:

{
   data(func: eq(phone.phone_number, "222222222")) {
    phone.phone_number
    ~link_customer_phone{
		customer.name
  }
}
}

Am I doing anything wrong with the query? Or is this related to data integrity?

Dgraph metadata

dgraph version: :21.03.00

Thanks in advance!

Seems to be working for me. Got the result →

"data": [
      {
        "phone.phone_number": 222222222,
        "~link_customer_phone": [
          {
            "customer.name": "Bob"
          },
          {
            "customer.name": "Charlie"
          }
        ]
      }
    ]
  }

First let me correct the schema a bit, it should be as follows →

<customer.country_code>: string @index(exact) .
<customer.id>: int @index(int) .
<customer.name>: string @index(term) .
<customer.registration_time>: datetime .
<dgraph.drop.op>: string .
<dgraph.graphql.p_query>: string @index(sha256) .
<dgraph.graphql.schema>: string .
<dgraph.graphql.xid>: string @index(exact) @upsert .
<link_customer_phone>: [uid] @reverse .
<phone.country_code>: string .
<phone.phone_number>: int @index(int) .
<phone.registration_time>: datetime .
type <Customer> {
	customer.country_code
	customer.id
	customer.registration_time
	customer.name
  link_customer_phone
}
type <Phone> {
	phone.country_code
	phone.phone_number
	phone.registration_time
}
type <dgraph.graphql> {
	dgraph.graphql.schema
	dgraph.graphql.xid
}
type <dgraph.graphql.persisted_query> {
	dgraph.graphql.p_query
}

Also are you possibly running the image on M1 ? (Docker M1 has stability issues)

1 Like

Many thanks for your reply kaustubh!

Glad to hear it worked for you without crashing, it reaffirms me that the problem is somewhere else.

(Docker M1 has stability issues)

Aahh, that must be it! I am indeed using Docker for a M1-chip Mac, so the crashes are almost surely related to this.

Regarding the changes on the schema, 2 quick questions:

  1. The definition of the edge as [uid] (between brackets) is mandatory? I just created the edge through Ratel and the brackets were not there, but next time I’ll make sure to change it in Bulk edit just in case

  2. I assume edges need to be always defined under a dgraph node type, right? So, since I left the link_user_phone outside User or Phone, this could lead to problems

Thanks again for your detailed answer, I’ll try tomorrow with these changes and let you know about the outcome :slight_smile:

So I’ll address the two questions as follows →

Regarding the changes on the schema, 2 quick questions:

  1. The definition of the edge as [uid] (between brackets) is not mandatory if it’s only a one to one relation. If it’s m to n relation then it’ll need to be an array of uid => [uid]
  2. It’s a good practice to name your predicate as objectName.predicate, so that way even if two predicates (of different objects) have the same name they will actually point to two distinct predicates (instead of overlapping on one another).
1 Like

Yep, both things make sense, noted for the future. Thanks again for answering so quickly.

However, I’ve tried again following your suggestions and I still see an error. I’ve done the following:

  1. DROP ALL
  2. Create the schema through Bulk Edit with your suggestions (essentially a copy paste)
  3. Mutate the same example I provided for Bob/Charlie
  4. Run the query to obtain all the customers connected to the phone

What I see is exactly this, and then the container fails:

Just in case it’s useful, the terminal for Docker’s response:

SIGILL: illegal instruction
PC=0x0 m=18 sigcode=0
instruction bytes:qemu: uncaught target signal 11 (Segmentation fault) - core dumped
W0513 08:56:56.551300      38 pool.go:267] Connection lost with localhost:7080. Error: rpc error: code = Unavailable desc = transport is closing

So, it might be related to the stability problems of Docker M1 or am I missing something?

Yeah, its a docker M1 stability issue. I currently use an intel Mac docker and things are stable on this

Ah, not exactly what I wanted to hear :sweat_smile:. Thank you for your insights!

1 Like