Dgraph Example Queries (JSON)

Hello, coders! In this page I’ve gather different Dgraph example queries.

Most of the examples are taken or extended version of examples from https://tour.dgraph.io/.

Follow this and this to create a schema and load an example data.

For more information on Dgraph.io and it’s usage with Go check my another article called “Dgraph Graph Database: How to Install and Use With Go?”.

Query #1: find by name

{
  find_michael(func: eq(name@., "Michael")) {
    uid
    name@.
    age
  }
}
{
  michaels_friends(func: eq(name, "Michael")) {
    name
    age
    owns_pet {
      name@.
    }
    friend {
      name@.
    }
  }
}

Query #3: get schema information

schema(pred: [name, age, friend, owns_pet]) {
  type
  index
}

By adding type and index in body we request to include type and index information about the scheme in the response.

Query #4: get by annotated language tag

{
  language_support(func: allofterms(name@hi, "अमित")) {
    name@bn:hi:en
    age
    friend {
      name@ko:ru
      age
    }
  }
}

Syntax name@bn:hi:en specifies an order preference in which name field must be returned. More information here.

Query #5: get all nodes count by a specific field name

{
  nodeCount(func: has(<message>)) {
    nodeCount: count(uid)
  }
}
{
    q(func: type(Log)) {
      uid
      created
    	message
    }
}

Query #7: query and filter by field’s type

{
  q(func: has(parent)) {
    uid
    parent @filter(type(Person)) {
      uid
      name
    }
  }
}

Check source for more information.