Schema for get employees for some boss and get boss for some employees


(Cocodrino) #1

Hi guys, first congratulation, dgraph looks awesome and pretty cool, second I’d like more easy examples, similar to the post opened by katopz, I think than the examples are a bit hard to follow compared to…for instance…the gremlin examples in its wiki, I’d like see how create a basic graphs and query them, maybe the samples in the gremlin wiki would be a good starting point

Now, I’m trying to recreate a basic schema…I want to set boss and employees, my schema would be something like:

scalar (
age:int
address: string
)

type Person {
name: string @index
age: int
address: string
is_boss_of: uid @reverse
}

probably I could create an edge in Person like is_employee_for… but I want to learn about @reverse so if I check the reverse for is_boss_of I could get what is the boss for a node


tom ---- is_boss_of ----> mike


who is the boss of mike
mike <—is_boss_of — (v)
v is tom

how can create this in dgraph?..

can you provide me an example about how get this?..thank you so much!!!


(Ashwin) #2

Hi @cocodrino!

You’re almost there. So I have created a small working example below which you could try out

  • Schema file:
scalar (
 age: int @index
 name: string @index
)

type Person {
 name: string 
 age: int
 address: string
 boss_of: uid @reverse
}
  • Data loading:
curl localhost:8080/query -XPOST -d $'mutation { 
set {
<a> <name> "alice" .
<b> <name> "bob" .
<a> <age> "10" .
<b> <age> "20" .
<b> <boss_of> <a> .
<a> <address> "London" .
<b> <address> "San Francisco" .
} 
}'
  • Some simple queries that you could do:
#Q1
{me(id:a) { name, ~boss_of {name} }}
Result:
{"me":[{"name":"alice","~boss_of":[{"name":"bob"}]}]}

#Q2
{me(gt(age,15)) { name, boss_of {name} }}
Result:
{"me":[{"boss_of":[{"name":"alice"}],"name":"bob"}]}

#Q3
{me(anyof(name, "alice bob")) { name, boss_of {name}, ~boss_of {name} }}
Result:
{
  "me": [
    {
      "boss_of": [
        {
          "name": "alice"
        }
      ],
      "name": "bob"
    },
    {
      "name": "alice",
      "~boss_of": [
        {
          "name": "bob"
        }
      ]
    }
  ]
}

Let us know if you have other questions!


Minimal Dgraph example for newbies
Minimal Dgraph example for newbies
(Todsaporn Banjerdkit) #3

I got

bash-3.2$ curl localhost:8080/query -XPOST -d $'mutation {
> set {
> <a> <name> "alice" .
> <b> <name> "bob" .
> <a> <age> "10" .
> <b> <age> "20" .
> <b> <boss_of> <a> .
> <a> <address> "London" .
> <b> <address> "San Francisco" .
> }
> }'
{"code":"ErrorOk","message":"Done","uids":{}}

Is that “ErrorOk” is normal?


(Todsaporn Banjerdkit) #4

Also

$ curl localhost:8080/query -XPOST -d $'{me(id:a) { name, ~boss_of {name} }}'
{"code":"ErrorInvalidRequest","message":"Expected ( after func name [id]"}

I think miss something, Any hint?


(Ashwin) #5

ErrorOk means its alright.

If you’re using v0.7.1, then you have to replace id with _xid_. I’ve written the queries wrt master branch where we have changed the syntax a bit.

So it’ll be

{me(_xid_:a) { name, ~boss_of {name} }}

(Todsaporn Banjerdkit) #6

Hm, weird enough. I don’t think I like it. It sound like bad thing happen tho.

Did the trick! Maybe I miss some document? Is it wrote somewhere?

Thanks


(Ashwin) #7

We’ll look at it. Thanks for the feedback!

https://wiki.dgraph.io/Query_Language#External_IDs_.28_xid_.29


(Cocodrino) #8

oh works great, thank so much @ashwin95r, I’ve a small question because seems than I’m confused in what must be declared as scalar and what not

for instance



scalar (
  age:int
  address: string
)
type  Person {
  name: string
  age: int
  address: string
  friends: uid
}

why in the documentation the age and address type are declare in both places?..what happens if I didn’t include age and address inside the scalar declaratin.

or in the example than I did this before

type Person {
name: string @index
age: int
address: string
is_boss_of: uid @reverse
}

I set the index for the name in the Person type but you remove this and set it as scalar, what is the reason about that

thank you so much!!!.


(Ashwin) #9

Hey @cocodrino,

Currently, @index can be specified only in the scalar declarations (so if you want to index some fields, you have to declare them as scalar regardless of whether you define them in objects or not).

Whereas @reverse can be used in scalar declarations or inside object declarations.

PS: I think we’ll modify this to enable specifying @index within objects soon to avoid this redundancy.


(Jansen Ignacio) #10

Hi!

I’ve just started using dgraph and I’m trying to build a schema. I tried out the one @ashwin95r put above and tried running it on the UI of v0.7.4 but it gives me Invalid mutation formatting. I also can’t find any more documentation for types in the schema. I’m wondering whether that’s been deprecated already or if there’s just something that I’m missing.

mutation {
    schema {
     scalar {
     age: int @index .
     name: string @index .
    }

    type Person {
     name: string .
     age: int .
     address: string .
     boss_of: uid @reverse .
    }
  }
}

(Ashwin) #11

Hey @jansenignacio

Yes, the type and scalar blocks are deprecated. So the right thing would be:

mutation {
    schema {
     age: int @index .
     name: string @index(exact, term) .
     address: string .
     boss_of: uid @reverse .
  }
}

(Jansen Ignacio) #12

I see. Thanks @ashwin95r! I’m curious, is that written in the release notes or was it just silently deprecated? I spent a bit of time looking for it before asking 'cause I couldn’t find it anywhere.


(Ashwin) #13

I think we just removed information about types in the docs (https://docs.dgraph.io/v0.7.4/query-language/#schema). The old examples are hence outdated. You are right, we haven’t mentioned the diff anywhere.


(Roman Heinrich) #14

Just to save people couple minutes, here is the complete code working on latest (0.7.7) release:

mutation {
  schema {
    age: int @index .
    name: string @index .
    address: string .
    boss_of: uid @reverse .
  }
}

mutation {
  set {
    <a> <name> "alice" .
    <b> <name> "bob" .
    <a> <age> "10" .
    <b> <age> "20" .
    <b> <boss_of> <a> .
    <a> <address> "London" .
    <b> <address> "San Francisco" .
  }
}


query {
  #Q1
  employee(id:a) {
    name
    ~boss_of {
      name
    }
  }

  #Q2
  boss(func: gt(age, 15)) {
    name
    boss_of {
      name
    }
  }

  #Q3
  all(func: anyofterms(name, "alice bob")) {
    name
    boss_of {
      name
    }
    ~boss_of {
      name
    }
  }
}

(Michael Compton) #15

There is also now the tour

https://tour.dgraph.io/

That has a similar introductory example


(system) #16

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