Dgraph is a graph databases. It stores data in a similar fashion for any type of collections. To differentiate the collection list, we may add a predicate, let's say "_type" indicate the type of collection / table. Example: User will have _type='users' and Role will have _type='roles'. ID field in dgraph is named "uid"
Client may connect to server nodes on port 9080 for grpc connection. Server nodes uses grpc 7080 to talk to zero node. You may call http admin rest call to server node on port 8080.
https://docs.dgraph.io/deploy#ports-used-by-different-nodes
query an object by id. with_role returns roles. "roles" indicate in the below code is a alias name to "with_role"
{
user(func: uid(0xc351)) {
first_name
last_name
email
roles: with_role {
uid
name
}
}
}
To know how relation works for "with_role", refer here
To sort the list of objects, the predicate/field need to be indexed. The index type will depend on the filter / sorting and predicate type. You may add multiple index to a predicate.
- string - @index(fulltext / exact / term)
- int / float- @index(int) / @index(float)
- dateTime @index(day/hour)
example:
first_name: string @index(term) @index(fulltext) .
To query list, example below shows 2 different type of query.
- roles - list all roles, with user that has relation with_role with respective role
- users - with pagination limit of 10, offset 0 (since not indicated), ordered by created_at desc with field set "with_role"
- roles_count - count all roles
query all($id: string){
roles(func: eq(_type, "roles")) {
uid
name
created_at
updated_at
users: ~with_role {
uid
email
}
}
users(func: eq(_type, "users"), first: 10, orderdesc: created_at) @filter(has(with_role)) {
uid
email
first_name
last_name
ip_address
created_at
updated_at
roles : with_role {
uid
name
}
company : work_for {
uid
code
name
status
country
created_at
updated_at
}
}
roles_count(func: eq(_type, "roles")) {
count(uid)
}
}
for more queries references:
update / create object using json aka. mutation
{
"set": [
{ "first_name": "ahmad", "last_name": "bin osman", "email": "test@example.com" }
]
}
update using RDF mutation
- <0x01> - uid
- - predicate / field
- "Alice" - value for the field
- @ru | @fr - suffix for respective language
mutation {
set {
<0x01> <name> "Alice" .
<0x01> <name> "Алисия"@ru .
<0x01> <name> "Adélaïde"@fr .
}
}
for more mutations:
to delete a single field
mutation {
delete {
<0x01> <first_name> * .
}
}
you may also specify specify value to delete.
Example below delete the role by uid <0x02> relation from user 0x01.
Role 0x02 will remain in database
mutation {
delete {
<0x01> <with_role> <0x02> .
}
}
to delete entire object / record (not tested)
mutation {
delete {
<0x01> * * .
}
}
to delete everything (not tested)
mutation {
delete {
* * * .
}
}
To declare a relation between two object, for example user with roles, 0x01 is the user uid, and 0x02 is the role uid.
mutation {
set {
<0x01> <with_role> <0x02> .
}
}
Ensure that you have set the schema for the predicate like below:
with_role: uid @reverse .
@reverse is indicate how roles can query users from the opposite direction. with @reverse declared, you may now query users from role
{
role(func: uid(0x02)) {
uid
name
users: ~with_role {
uid
first_name
last_name
email
}
}
}
Please note that if you attempt to mutate delete one or more relation,then query for only the relation field, please add also uid predicate of the main object. This is to avoid no record found error in DGO library. Example:
Don't do this:
{
user(func: uid(0x01)) {
roles: with_role {
uid
name
}
}
}
Do this:
{
user(func: uid(0x01)) {
uid
roles: with_role {
uid
name
}
}
}
For other schema queries
(https://docs.dgraph.io/v0.7.4/query-language/#fetching-schema)