DQL: Tips and Tricks
Get Sample Data
Use the has function to get some sample nodes.
- Query
- Go
- Java
- Python
- JavaScript (gRPC)
- JavaScript (HTTP)
- Curl
{
result(func: has(director.film), first: 10) {
uid
expand(_all_)
}
}
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"google.golang.org/grpc"
"github.com/dgraph-io/dgo/v230"
"github.com/dgraph-io/dgo/v230/protos/api"
)
func main() {
conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(conn))
ctx := context.Background()
txn := dgraphClient.NewTxn()
defer txn.Discard(ctx)
query := `{
result(func: has(director.film), first: 10) {
uid
expand(_all_)
}
}`
resp, err := txn.Query(ctx, query)
if err != nil {
log.Fatal(err)
}
var result map[string]interface{}
json.Unmarshal(resp.Json, &result)
fmt.Printf("%+v\n", result)
}
import io.dgraph.DgraphClient;
import io.dgraph.DgraphGrpc;
import io.dgraph.DgraphProto;
import io.dgraph.Transaction;
import java.util.Map;
public class App {
public static void main(String[] args) {
ManagedChannel channel = ManagedChannelBuilder
.forAddress("localhost", 9080)
.usePlaintext()
.build();
DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel);
DgraphClient dgraphClient = new DgraphClient(stub);
String query = "{
result(func: has(director.film), first: 10) {
uid
expand(_all_)
}
}";
Transaction txn = dgraphClient.newTransaction();
try {
DgraphProto.Response response = txn.query(query);
System.out.println(response.getJson().toStringUtf8());
} finally {
txn.discard();
}
}
}
import grpc
from dgraph import DgraphClient, Txn
def main():
client_stub = DgraphClient("localhost:9080")
client = DgraphClient(client_stub)
query = """{
result(func: has(director.film), first: 10) {
uid
expand(_all_)
}
}"""
txn = client.txn()
try:
response = txn.query(query)
print(response.json)
finally:
txn.discard()
if __name__ == "__main__":
main()
const dgraph = require("dgraph-js");
const grpc = require("grpc");
async function main() {
const clientStub = new dgraph.DgraphClientStub(
"localhost:9080",
grpc.credentials.createInsecure()
);
const dgraphClient = new dgraph.DgraphClient(clientStub);
const query = `{
result(func: has(director.film), first: 10) {
uid
expand(_all_)
}
}`;
const txn = dgraphClient.newTxn();
try {
const res = await txn.query(query);
const json = res.getJson();
console.log(JSON.stringify(JSON.parse(json), null, 2));
} finally {
await txn.discard();
}
}
main().catch((e) => {
console.error(e);
});
const fetch = require("node-fetch");
async function main() {
const query = `{
result(func: has(director.film), first: 10) {
uid
expand(_all_)
}
}`;
const response = await fetch("http://localhost:8080/query", {
method: "POST",
headers: {
"Content-Type": "application/dql"
},
body: query
});
const result = await response.json();
console.log(JSON.stringify(result, null, 2));
}
main().catch((e) => {
console.error(e);
});
curl -X POST http://localhost:8080/query \
-H "Content-Type: application/dql" \
-d '{
result(func: has(director.film), first: 10) {
uid
expand(_all_)
}
}'
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.
- Query
- Go
- Java
- Python
- JavaScript (gRPC)
- JavaScript (HTTP)
- Curl
{
uids(func: has(director.film), first: 1) {
uid
expand(_all_) { u as uid }
}
result(func: uid(u)) {
count(uid)
}
}
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"google.golang.org/grpc"
"github.com/dgraph-io/dgo/v230"
"github.com/dgraph-io/dgo/v230/protos/api"
)
func main() {
conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(conn))
ctx := context.Background()
txn := dgraphClient.NewTxn()
defer txn.Discard(ctx)
query := `{
uids(func: has(director.film), first: 1) {
uid
expand(_all_) { u as uid }
}
result(func: uid(u)) {
count(uid)
}
}`
resp, err := txn.Query(ctx, query)
if err != nil {
log.Fatal(err)
}
var result map[string]interface{}
json.Unmarshal(resp.Json, &result)
fmt.Printf("%+v\n", result)
}
import io.dgraph.DgraphClient;
import io.dgraph.DgraphGrpc;
import io.dgraph.DgraphProto;
import io.dgraph.Transaction;
import java.util.Map;
public class App {
public static void main(String[] args) {
ManagedChannel channel = ManagedChannelBuilder
.forAddress("localhost", 9080)
.usePlaintext()
.build();
DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel);
DgraphClient dgraphClient = new DgraphClient(stub);
String query = "{
uids(func: has(director.film), first: 1) {
uid
expand(_all_) { u as uid }
}
result(func: uid(u)) {
count(uid)
}
}";
Transaction txn = dgraphClient.newTransaction();
try {
DgraphProto.Response response = txn.query(query);
System.out.println(response.getJson().toStringUtf8());
} finally {
txn.discard();
}
}
}
import grpc
from dgraph import DgraphClient, Txn
def main():
client_stub = DgraphClient("localhost:9080")
client = DgraphClient(client_stub)
query = """{
uids(func: has(director.film), first: 1) {
uid
expand(_all_) { u as uid }
}
result(func: uid(u)) {
count(uid)
}
}"""
txn = client.txn()
try:
response = txn.query(query)
print(response.json)
finally:
txn.discard()
if __name__ == "__main__":
main()
const dgraph = require("dgraph-js");
const grpc = require("grpc");
async function main() {
const clientStub = new dgraph.DgraphClientStub(
"localhost:9080",
grpc.credentials.createInsecure()
);
const dgraphClient = new dgraph.DgraphClient(clientStub);
const query = `{
uids(func: has(director.film), first: 1) {
uid
expand(_all_) { u as uid }
}
result(func: uid(u)) {
count(uid)
}
}`;
const txn = dgraphClient.newTxn();
try {
const res = await txn.query(query);
const json = res.getJson();
console.log(JSON.stringify(JSON.parse(json), null, 2));
} finally {
await txn.discard();
}
}
main().catch((e) => {
console.error(e);
});
const fetch = require("node-fetch");
async function main() {
const query = `{
uids(func: has(director.film), first: 1) {
uid
expand(_all_) { u as uid }
}
result(func: uid(u)) {
count(uid)
}
}`;
const response = await fetch("http://localhost:8080/query", {
method: "POST",
headers: {
"Content-Type": "application/dql"
},
body: query
});
const result = await response.json();
console.log(JSON.stringify(result, null, 2));
}
main().catch((e) => {
console.error(e);
});
curl -X POST http://localhost:8080/query \
-H "Content-Type: application/dql" \
-d '{
uids(func: has(director.film), first: 1) {
uid
expand(_all_) { u as uid }
}
result(func: uid(u)) {
count(uid)
}
}'
Search on non-indexed predicates
Use the has function among the value variables to search on non-indexed predicates.
- Query
- Go
- Java
- Python
- JavaScript (gRPC)
- JavaScript (HTTP)
- Curl
{
var(func: has(festival.date_founded)) {
p as festival.date_founded
}
query(func: eq(val(p), "1961-01-01T00:00:00Z")) {
uid
name@en
name@ru
name@pl
festival.date_founded
festival.focus { name@en }
festival.individual_festivals { total : count(uid) }
}
}
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"google.golang.org/grpc"
"github.com/dgraph-io/dgo/v230"
"github.com/dgraph-io/dgo/v230/protos/api"
)
func main() {
conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(conn))
ctx := context.Background()
txn := dgraphClient.NewTxn()
defer txn.Discard(ctx)
query := `{
var(func: has(festival.date_founded)) {
p as festival.date_founded
}
query(func: eq(val(p), "1961-01-01T00:00:00Z")) {
uid
name@en
name@ru
name@pl
festival.date_founded
festival.focus { name@en }
festival.individual_festivals { total : count(uid) }
}
}`
resp, err := txn.Query(ctx, query)
if err != nil {
log.Fatal(err)
}
var result map[string]interface{}
json.Unmarshal(resp.Json, &result)
fmt.Printf("%+v\n", result)
}
import io.dgraph.DgraphClient;
import io.dgraph.DgraphGrpc;
import io.dgraph.DgraphProto;
import io.dgraph.Transaction;
import java.util.Map;
public class App {
public static void main(String[] args) {
ManagedChannel channel = ManagedChannelBuilder
.forAddress("localhost", 9080)
.usePlaintext()
.build();
DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel);
DgraphClient dgraphClient = new DgraphClient(stub);
String query = "{
var(func: has(festival.date_founded)) {
p as festival.date_founded
}
query(func: eq(val(p), \"1961-01-01T00:00:00Z\")) {
uid
name@en
name@ru
name@pl
festival.date_founded
festival.focus { name@en }
festival.individual_festivals { total : count(uid) }
}
}";
Transaction txn = dgraphClient.newTransaction();
try {
DgraphProto.Response response = txn.query(query);
System.out.println(response.getJson().toStringUtf8());
} finally {
txn.discard();
}
}
}
import grpc
from dgraph import DgraphClient, Txn
def main():
client_stub = DgraphClient("localhost:9080")
client = DgraphClient(client_stub)
query = """{
var(func: has(festival.date_founded)) {
p as festival.date_founded
}
query(func: eq(val(p), "1961-01-01T00:00:00Z")) {
uid
name@en
name@ru
name@pl
festival.date_founded
festival.focus { name@en }
festival.individual_festivals { total : count(uid) }
}
}"""
txn = client.txn()
try:
response = txn.query(query)
print(response.json)
finally:
txn.discard()
if __name__ == "__main__":
main()
const dgraph = require("dgraph-js");
const grpc = require("grpc");
async function main() {
const clientStub = new dgraph.DgraphClientStub(
"localhost:9080",
grpc.credentials.createInsecure()
);
const dgraphClient = new dgraph.DgraphClient(clientStub);
const query = `{
var(func: has(festival.date_founded)) {
p as festival.date_founded
}
query(func: eq(val(p), "1961-01-01T00:00:00Z")) {
uid
name@en
name@ru
name@pl
festival.date_founded
festival.focus { name@en }
festival.individual_festivals { total : count(uid) }
}
}`;
const txn = dgraphClient.newTxn();
try {
const res = await txn.query(query);
const json = res.getJson();
console.log(JSON.stringify(JSON.parse(json), null, 2));
} finally {
await txn.discard();
}
}
main().catch((e) => {
console.error(e);
});
const fetch = require("node-fetch");
async function main() {
const query = `{
var(func: has(festival.date_founded)) {
p as festival.date_founded
}
query(func: eq(val(p), "1961-01-01T00:00:00Z")) {
uid
name@en
name@ru
name@pl
festival.date_founded
festival.focus { name@en }
festival.individual_festivals { total : count(uid) }
}
}`;
const response = await fetch("http://localhost:8080/query", {
method: "POST",
headers: {
"Content-Type": "application/dql"
},
body: query
});
const result = await response.json();
console.log(JSON.stringify(result, null, 2));
}
main().catch((e) => {
console.error(e);
});
curl -X POST http://localhost:8080/query \
-H "Content-Type: application/dql" \
-d '{
var(func: has(festival.date_founded)) {
p as festival.date_founded
}
query(func: eq(val(p), "1961-01-01T00:00:00Z")) {
uid
name@en
name@ru
name@pl
festival.date_founded
festival.focus { name@en }
festival.individual_festivals { total : count(uid) }
}
}'
Sort edge by nested node values
Dgraph sorting is based on a single level of the subgraph. To sort a level by the values of a deeper level, use query variables to bring nested values up to the level of the edge to be sorted.
Example: Get all actors from a Steven Spielberg movie sorted alphabetically.
The actor's name is not accessed from a single traversal from the starring edge;
the name is accessible via performance.actor.
- Query
- Go
- Java
- Python
- JavaScript (gRPC)
- JavaScript (HTTP)
- Curl
{
spielbergMovies as var(func: allofterms(name@en, "steven spielberg")) {
name@en
director.film (orderasc: name@en, first: 1) {
starring {
performance.actor {
ActorName as name@en
}
# Stars is a uid-to-value map mapping
# starring edges to performance.actor names
Stars as min(val(ActorName))
}
}
}
movies(func: uid(spielbergMovies)) @cascade {
name@en
director.film (orderasc: name@en, first: 1) {
name@en
starring (orderasc: val(Stars)) {
performance.actor {
name@en
}
}
}
}
}
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"google.golang.org/grpc"
"github.com/dgraph-io/dgo/v230"
"github.com/dgraph-io/dgo/v230/protos/api"
)
func main() {
conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(conn))
ctx := context.Background()
txn := dgraphClient.NewTxn()
defer txn.Discard(ctx)
query := `{
spielbergMovies as var(func: allofterms(name@en, "steven spielberg")) {
name@en
director.film (orderasc: name@en, first: 1) {
starring {
performance.actor {
ActorName as name@en
}
# Stars is a uid-to-value map mapping
# starring edges to performance.actor names
Stars as min(val(ActorName))
}
}
}
movies(func: uid(spielbergMovies)) @cascade {
name@en
director.film (orderasc: name@en, first: 1) {
name@en
starring (orderasc: val(Stars)) {
performance.actor {
name@en
}
}
}
}
}`
resp, err := txn.Query(ctx, query)
if err != nil {
log.Fatal(err)
}
var result map[string]interface{}
json.Unmarshal(resp.Json, &result)
fmt.Printf("%+v\n", result)
}
import io.dgraph.DgraphClient;
import io.dgraph.DgraphGrpc;
import io.dgraph.DgraphProto;
import io.dgraph.Transaction;
import java.util.Map;
public class App {
public static void main(String[] args) {
ManagedChannel channel = ManagedChannelBuilder
.forAddress("localhost", 9080)
.usePlaintext()
.build();
DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel);
DgraphClient dgraphClient = new DgraphClient(stub);
String query = "{
spielbergMovies as var(func: allofterms(name@en, \"steven spielberg\")) {
name@en
director.film (orderasc: name@en, first: 1) {
starring {
performance.actor {
ActorName as name@en
}
# Stars is a uid-to-value map mapping
# starring edges to performance.actor names
Stars as min(val(ActorName))
}
}
}
movies(func: uid(spielbergMovies)) @cascade {
name@en
director.film (orderasc: name@en, first: 1) {
name@en
starring (orderasc: val(Stars)) {
performance.actor {
name@en
}
}
}
}
}";
Transaction txn = dgraphClient.newTransaction();
try {
DgraphProto.Response response = txn.query(query);
System.out.println(response.getJson().toStringUtf8());
} finally {
txn.discard();
}
}
}
import grpc
from dgraph import DgraphClient, Txn
def main():
client_stub = DgraphClient("localhost:9080")
client = DgraphClient(client_stub)
query = """{
spielbergMovies as var(func: allofterms(name@en, "steven spielberg")) {
name@en
director.film (orderasc: name@en, first: 1) {
starring {
performance.actor {
ActorName as name@en
}
# Stars is a uid-to-value map mapping
# starring edges to performance.actor names
Stars as min(val(ActorName))
}
}
}
movies(func: uid(spielbergMovies)) @cascade {
name@en
director.film (orderasc: name@en, first: 1) {
name@en
starring (orderasc: val(Stars)) {
performance.actor {
name@en
}
}
}
}
}"""
txn = client.txn()
try:
response = txn.query(query)
print(response.json)
finally:
txn.discard()
if __name__ == "__main__":
main()
const dgraph = require("dgraph-js");
const grpc = require("grpc");
async function main() {
const clientStub = new dgraph.DgraphClientStub(
"localhost:9080",
grpc.credentials.createInsecure()
);
const dgraphClient = new dgraph.DgraphClient(clientStub);
const query = `{
spielbergMovies as var(func: allofterms(name@en, "steven spielberg")) {
name@en
director.film (orderasc: name@en, first: 1) {
starring {
performance.actor {
ActorName as name@en
}
# Stars is a uid-to-value map mapping
# starring edges to performance.actor names
Stars as min(val(ActorName))
}
}
}
movies(func: uid(spielbergMovies)) @cascade {
name@en
director.film (orderasc: name@en, first: 1) {
name@en
starring (orderasc: val(Stars)) {
performance.actor {
name@en
}
}
}
}
}`;
const txn = dgraphClient.newTxn();
try {
const res = await txn.query(query);
const json = res.getJson();
console.log(JSON.stringify(JSON.parse(json), null, 2));
} finally {
await txn.discard();
}
}
main().catch((e) => {
console.error(e);
});
const fetch = require("node-fetch");
async function main() {
const query = `{
spielbergMovies as var(func: allofterms(name@en, "steven spielberg")) {
name@en
director.film (orderasc: name@en, first: 1) {
starring {
performance.actor {
ActorName as name@en
}
# Stars is a uid-to-value map mapping
# starring edges to performance.actor names
Stars as min(val(ActorName))
}
}
}
movies(func: uid(spielbergMovies)) @cascade {
name@en
director.film (orderasc: name@en, first: 1) {
name@en
starring (orderasc: val(Stars)) {
performance.actor {
name@en
}
}
}
}
}`;
const response = await fetch("http://localhost:8080/query", {
method: "POST",
headers: {
"Content-Type": "application/dql"
},
body: query
});
const result = await response.json();
console.log(JSON.stringify(result, null, 2));
}
main().catch((e) => {
console.error(e);
});
curl -X POST http://localhost:8080/query \
-H "Content-Type: application/dql" \
-d '{
spielbergMovies as var(func: allofterms(name@en, "steven spielberg")) {
name@en
director.film (orderasc: name@en, first: 1) {
starring {
performance.actor {
ActorName as name@en
}
# Stars is a uid-to-value map mapping
# starring edges to performance.actor names
Stars as min(val(ActorName))
}
}
}
movies(func: uid(spielbergMovies)) @cascade {
name@en
director.film (orderasc: name@en, first: 1) {
name@en
starring (orderasc: val(Stars)) {
performance.actor {
name@en
}
}
}
}
}'
Obtain unique results by using variables
To obtain unique results, assign the node's edge to a variable. The variable can now be used to iterate over the unique nodes.
Example: Get all unique genres from all of the movies directed by Steven Spielberg.
- Query
- Go
- Java
- Python
- JavaScript (gRPC)
- JavaScript (HTTP)
- Curl
{
var(func: eq(name@en, "Steven Spielberg")) {
director.film {
genres as genre
}
}
q(func: uid(genres)) {
name@.
}
}
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"google.golang.org/grpc"
"github.com/dgraph-io/dgo/v230"
"github.com/dgraph-io/dgo/v230/protos/api"
)
func main() {
conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(conn))
ctx := context.Background()
txn := dgraphClient.NewTxn()
defer txn.Discard(ctx)
query := `{
var(func: eq(name@en, "Steven Spielberg")) {
director.film {
genres as genre
}
}
q(func: uid(genres)) {
name@.
}
}`
resp, err := txn.Query(ctx, query)
if err != nil {
log.Fatal(err)
}
var result map[string]interface{}
json.Unmarshal(resp.Json, &result)
fmt.Printf("%+v\n", result)
}
import io.dgraph.DgraphClient;
import io.dgraph.DgraphGrpc;
import io.dgraph.DgraphProto;
import io.dgraph.Transaction;
import java.util.Map;
public class App {
public static void main(String[] args) {
ManagedChannel channel = ManagedChannelBuilder
.forAddress("localhost", 9080)
.usePlaintext()
.build();
DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel);
DgraphClient dgraphClient = new DgraphClient(stub);
String query = "{
var(func: eq(name@en, \"Steven Spielberg\")) {
director.film {
genres as genre
}
}
q(func: uid(genres)) {
name@.
}
}";
Transaction txn = dgraphClient.newTransaction();
try {
DgraphProto.Response response = txn.query(query);
System.out.println(response.getJson().toStringUtf8());
} finally {
txn.discard();
}
}
}
import grpc
from dgraph import DgraphClient, Txn
def main():
client_stub = DgraphClient("localhost:9080")
client = DgraphClient(client_stub)
query = """{
var(func: eq(name@en, "Steven Spielberg")) {
director.film {
genres as genre
}
}
q(func: uid(genres)) {
name@.
}
}"""
txn = client.txn()
try:
response = txn.query(query)
print(response.json)
finally:
txn.discard()
if __name__ == "__main__":
main()
const dgraph = require("dgraph-js");
const grpc = require("grpc");
async function main() {
const clientStub = new dgraph.DgraphClientStub(
"localhost:9080",
grpc.credentials.createInsecure()
);
const dgraphClient = new dgraph.DgraphClient(clientStub);
const query = `{
var(func: eq(name@en, "Steven Spielberg")) {
director.film {
genres as genre
}
}
q(func: uid(genres)) {
name@.
}
}`;
const txn = dgraphClient.newTxn();
try {
const res = await txn.query(query);
const json = res.getJson();
console.log(JSON.stringify(JSON.parse(json), null, 2));
} finally {
await txn.discard();
}
}
main().catch((e) => {
console.error(e);
});
const fetch = require("node-fetch");
async function main() {
const query = `{
var(func: eq(name@en, "Steven Spielberg")) {
director.film {
genres as genre
}
}
q(func: uid(genres)) {
name@.
}
}`;
const response = await fetch("http://localhost:8080/query", {
method: "POST",
headers: {
"Content-Type": "application/dql"
},
body: query
});
const result = await response.json();
console.log(JSON.stringify(result, null, 2));
}
main().catch((e) => {
console.error(e);
});
curl -X POST http://localhost:8080/query \
-H "Content-Type: application/dql" \
-d '{
var(func: eq(name@en, "Steven Spielberg")) {
director.film {
genres as genre
}
}
q(func: uid(genres)) {
name@.
}
}'
Usage of checkpwd boolean
Store the result of checkpwd in a query variable and then match it against 1 (checkpwd is true) or 0 (checkpwd is false).
- Query
- Go
- Java
- Python
- JavaScript (gRPC)
- JavaScript (HTTP)
- Curl
{
exampleData(func: has(email)) {
uid
email
check as checkpwd(pass, "1bdfhJHb!fd")
}
userMatched(func: eq(val(check), 1)) {
uid
email
}
userIncorrect(func: eq(val(check), 0)) {
uid
email
}
}
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"google.golang.org/grpc"
"github.com/dgraph-io/dgo/v230"
"github.com/dgraph-io/dgo/v230/protos/api"
)
func main() {
conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(conn))
ctx := context.Background()
txn := dgraphClient.NewTxn()
defer txn.Discard(ctx)
query := `{
exampleData(func: has(email)) {
uid
email
check as checkpwd(pass, "1bdfhJHb!fd")
}
userMatched(func: eq(val(check), 1)) {
uid
email
}
userIncorrect(func: eq(val(check), 0)) {
uid
email
}
}`
resp, err := txn.Query(ctx, query)
if err != nil {
log.Fatal(err)
}
var result map[string]interface{}
json.Unmarshal(resp.Json, &result)
fmt.Printf("%+v\n", result)
}
import io.dgraph.DgraphClient;
import io.dgraph.DgraphGrpc;
import io.dgraph.DgraphProto;
import io.dgraph.Transaction;
import java.util.Map;
public class App {
public static void main(String[] args) {
ManagedChannel channel = ManagedChannelBuilder
.forAddress("localhost", 9080)
.usePlaintext()
.build();
DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel);
DgraphClient dgraphClient = new DgraphClient(stub);
String query = "{
exampleData(func: has(email)) {
uid
email
check as checkpwd(pass, \"1bdfhJHb!fd\")
}
userMatched(func: eq(val(check), 1)) {
uid
email
}
userIncorrect(func: eq(val(check), 0)) {
uid
email
}
}";
Transaction txn = dgraphClient.newTransaction();
try {
DgraphProto.Response response = txn.query(query);
System.out.println(response.getJson().toStringUtf8());
} finally {
txn.discard();
}
}
}
import grpc
from dgraph import DgraphClient, Txn
def main():
client_stub = DgraphClient("localhost:9080")
client = DgraphClient(client_stub)
query = """{
exampleData(func: has(email)) {
uid
email
check as checkpwd(pass, "1bdfhJHb!fd")
}
userMatched(func: eq(val(check), 1)) {
uid
email
}
userIncorrect(func: eq(val(check), 0)) {
uid
email
}
}"""
txn = client.txn()
try:
response = txn.query(query)
print(response.json)
finally:
txn.discard()
if __name__ == "__main__":
main()
const dgraph = require("dgraph-js");
const grpc = require("grpc");
async function main() {
const clientStub = new dgraph.DgraphClientStub(
"localhost:9080",
grpc.credentials.createInsecure()
);
const dgraphClient = new dgraph.DgraphClient(clientStub);
const query = `{
exampleData(func: has(email)) {
uid
email
check as checkpwd(pass, "1bdfhJHb!fd")
}
userMatched(func: eq(val(check), 1)) {
uid
email
}
userIncorrect(func: eq(val(check), 0)) {
uid
email
}
}`;
const txn = dgraphClient.newTxn();
try {
const res = await txn.query(query);
const json = res.getJson();
console.log(JSON.stringify(JSON.parse(json), null, 2));
} finally {
await txn.discard();
}
}
main().catch((e) => {
console.error(e);
});
const fetch = require("node-fetch");
async function main() {
const query = `{
exampleData(func: has(email)) {
uid
email
check as checkpwd(pass, "1bdfhJHb!fd")
}
userMatched(func: eq(val(check), 1)) {
uid
email
}
userIncorrect(func: eq(val(check), 0)) {
uid
email
}
}`;
const response = await fetch("http://localhost:8080/query", {
method: "POST",
headers: {
"Content-Type": "application/dql"
},
body: query
});
const result = await response.json();
console.log(JSON.stringify(result, null, 2));
}
main().catch((e) => {
console.error(e);
});
curl -X POST http://localhost:8080/query \
-H "Content-Type: application/dql" \
-d '{
exampleData(func: has(email)) {
uid
email
check as checkpwd(pass, "1bdfhJHb!fd")
}
userMatched(func: eq(val(check), 1)) {
uid
email
}
userIncorrect(func: eq(val(check), 0)) {
uid
email
}
}'