Report Issue Edit Page

GraphQL+-: Tips and Tricks

Get Sample Data

Use the has function to get some sample nodes.

Editing query...
{
  result(func: has(director.film), first: 10) {
    uid
    expand(_all_)
  }
}
curl localhost:8080/query -XPOST -d '
{ result(func: has(director.film), first: 10) { uid expand(_all_) } }' | python -m json.tool | less
package main

import (
  "context"
  "flag"
  "fmt"
  "log"
  "github.com/dgraph-io/dgraph/client"
  "github.com/dgraph-io/dgraph/protos/api"
  "google.golang.org/grpc"
)

var (
  dgraph = flag.String("d", "127.0.0.1:9080", "Dgraph server address")
)

func main() {
  flag.Parse()
  conn, err := grpc.Dial(*dgraph, grpc.WithInsecure())
  if err != nil {
    log.Fatal(err)
  }
  defer conn.Close()

  
  dg := client.NewDgraphClient(api.NewDgraphClient(conn))
  
  
  resp, err := dg.NewTxn().Query(context.Background(), `blahblah`)
  
  if err != nil {
    log.Fatal(err)
  }
  fmt.Printf("Response: %s\n", resp.Json)
}
Response

Count number of connecting nodes

Use expand(_all_) to expand the nodes’ edges, then assign them to a variable. The variable can now be used to iterate over the unique neighboring nodes. Then use count(uid) to count the number of nodes in a block.

Editing query...
{
  uids(func: has(director.film), first: 1) {
    uid
    expand(_all_) { u as uid }
  }

  result(func: uid(u)) {
    count(uid)
  }
}
curl localhost:8080/query -XPOST -d '
{ uids(func: has(director.film), first: 1) { uid expand(_all_) { u as uid } } result(func: uid(u)) { count(uid) } }' | python -m json.tool | less
package main

import (
  "context"
  "flag"
  "fmt"
  "log"
  "github.com/dgraph-io/dgraph/client"
  "github.com/dgraph-io/dgraph/protos/api"
  "google.golang.org/grpc"
)

var (
  dgraph = flag.String("d", "127.0.0.1:9080", "Dgraph server address")
)

func main() {
  flag.Parse()
  conn, err := grpc.Dial(*dgraph, grpc.WithInsecure())
  if err != nil {
    log.Fatal(err)
  }
  defer conn.Close()

  
  dg := client.NewDgraphClient(api.NewDgraphClient(conn))
  
  
  resp, err := dg.NewTxn().Query(context.Background(), `blahblah`)
  
  if err != nil {
    log.Fatal(err)
  }
  fmt.Printf("Response: %s\n", resp.Json)
}
Response

Search on non-indexed predicates

Use the has function among the value variables to search on non-indexed predicates.

Editing query...
{
  var(func: has(festival.date_founded)) {
    p as festival.date_founded
  }
  query(func: eq(val(p), "1961-01-01T00:00:00Z")) {
      uid
      [email protected] 
      [email protected] 
      [email protected]
      festival.date_founded
      festival.focus { [email protected] }
      festival.individual_festivals { total : count(uid) }
  }
}
curl localhost:8080/query -XPOST -d '
{ var(func: has(festival.date_founded)) { p as festival.date_founded } query(func: eq(val(p), "1961-01-01T00:00:00Z")) { uid [email protected] [email protected] [email protected] festival.date_founded festival.focus { [email protected] } festival.individual_festivals { total : count(uid) } } }' | python -m json.tool | less
package main

import (
  "context"
  "flag"
  "fmt"
  "log"
  "github.com/dgraph-io/dgraph/client"
  "github.com/dgraph-io/dgraph/protos/api"
  "google.golang.org/grpc"
)

var (
  dgraph = flag.String("d", "127.0.0.1:9080", "Dgraph server address")
)

func main() {
  flag.Parse()
  conn, err := grpc.Dial(*dgraph, grpc.WithInsecure())
  if err != nil {
    log.Fatal(err)
  }
  defer conn.Close()

  
  dg := client.NewDgraphClient(api.NewDgraphClient(conn))
  
  
  resp, err := dg.NewTxn().Query(context.Background(), `blahblah`)
  
  if err != nil {
    log.Fatal(err)
  }
  fmt.Printf("Response: %s\n", resp.Json)
}
Response