DQL: Tips and Tricks
Get Sample Data
Use the has
function to get some sample nodes.
{
result(func: has(director.film), first: 10) {
uid
expand(_all_)
}
}
curl -H "Content-Type: application/dql" 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/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"google.golang.org/grpc"
)
var (
dgraph = flag.String("d", "127.0.0.1:9080", "Dgraph Alpha address")
)
func main() {
flag.Parse()
conn, err := grpc.Dial(*dgraph, grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))
resp, err := dg.NewTxn().Query(context.Background(), `{
result(func: has(director.film), first: 10) {
uid
expand(_all_)
}
}`)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Response: %s\n", resp.Json)
}
import io.dgraph.DgraphClient;
import io.dgraph.DgraphGrpc;
import io.dgraph.DgraphGrpc.DgraphStub;
import io.dgraph.DgraphProto.Response;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.Map;
public class App {
public static void main(final String[] args) {
ManagedChannel channel =
ManagedChannelBuilder.forAddress("localhost", 9080).usePlaintext(true).build();
DgraphStub stub = DgraphGrpc.newStub(channel);
DgraphClient dgraphClient = new DgraphClient(stub);
String query = "{\n "+"result(func: has(director.film), first: 10) {\n "+"uid\n "+"expand(_all_)\n "+"}\n "+"}";
Response res = dgraphClient.newTransaction().query(query);
System.out.printf("Response: %s", res.getJson().toStringUtf8());
}
}
import pydgraph
import json
def main():
client_stub = pydgraph.DgraphClientStub("localhost:9080")
client = pydgraph.DgraphClient(client_stub)
query = """{
result(func: has(director.film), first: 10) {
uid
expand(_all_)
}
}"""
res = client.txn(read_only=True).query(query)
print('Response: {}'.format(json.loads(res.json)))
client_stub.close()
if __name__ == '__main__':
try:
main()
except Exception as e:
print('Error: {}'.format(e))
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 response = await dgraphClient.newTxn().query(query);
console.log("Response: ", JSON.stringify(response.getJson()));
clientStub.close();
}
main().then().catch((e) => {
console.log("ERROR: ", e);
});
const dgraph = require("dgraph-js-http");
async function main() {
const clientStub = new dgraph.DgraphClientStub("http://localhost:8080");
const dgraphClient = new dgraph.DgraphClient(clientStub);
const query = `{
result(func: has(director.film), first: 10) {
uid
expand(_all_)
}
}`;
const response = await dgraphClient.newTxn().query(query);
console.log("Response: ", JSON.stringify(response.data));
}
main().then().catch((e) => {
console.log("ERROR: ", e);
});
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.
{
uids(func: has(director.film), first: 1) {
uid
expand(_all_) { u as uid }
}
result(func: uid(u)) {
count(uid)
}
}
curl -H "Content-Type: application/dql" 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/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"google.golang.org/grpc"
)
var (
dgraph = flag.String("d", "127.0.0.1:9080", "Dgraph Alpha address")
)
func main() {
flag.Parse()
conn, err := grpc.Dial(*dgraph, grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))
resp, err := dg.NewTxn().Query(context.Background(), `{
uids(func: has(director.film), first: 1) {
uid
expand(_all_) { u as uid }
}
result(func: uid(u)) {
count(uid)
}
}`)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Response: %s\n", resp.Json)
}
import io.dgraph.DgraphClient;
import io.dgraph.DgraphGrpc;
import io.dgraph.DgraphGrpc.DgraphStub;
import io.dgraph.DgraphProto.Response;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.Map;
public class App {
public static void main(final String[] args) {
ManagedChannel channel =
ManagedChannelBuilder.forAddress("localhost", 9080).usePlaintext(true).build();
DgraphStub stub = DgraphGrpc.newStub(channel);
DgraphClient dgraphClient = new DgraphClient(stub);
String query = "{\n "+"uids(func: has(director.film), first: 1) {\n "+"uid\n "+"expand(_all_) { u as uid }\n "+"}\n "+"\n "+"result(func: uid(u)) {\n "+"count(uid)\n "+"}\n "+"}";
Response res = dgraphClient.newTransaction().query(query);
System.out.printf("Response: %s", res.getJson().toStringUtf8());
}
}
import pydgraph
import json
def main():
client_stub = pydgraph.DgraphClientStub("localhost:9080")
client = pydgraph.DgraphClient(client_stub)
query = """{
uids(func: has(director.film), first: 1) {
uid
expand(_all_) { u as uid }
}
result(func: uid(u)) {
count(uid)
}
}"""
res = client.txn(read_only=True).query(query)
print('Response: {}'.format(json.loads(res.json)))
client_stub.close()
if __name__ == '__main__':
try:
main()
except Exception as e:
print('Error: {}'.format(e))
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 response = await dgraphClient.newTxn().query(query);
console.log("Response: ", JSON.stringify(response.getJson()));
clientStub.close();
}
main().then().catch((e) => {
console.log("ERROR: ", e);
});
const dgraph = require("dgraph-js-http");
async function main() {
const clientStub = new dgraph.DgraphClientStub("http://localhost:8080");
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 response = await dgraphClient.newTxn().query(query);
console.log("Response: ", JSON.stringify(response.data));
}
main().then().catch((e) => {
console.log("ERROR: ", e);
});
Search on non-indexed predicates
Use the has
function among the value variables to search on non-indexed predicates.
{
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) }
}
}
curl -H "Content-Type: application/dql" 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
name@en
name@ru
name@pl
festival.date_founded
festival.focus { name@en }
festival.individual_festivals { total : count(uid) }
}
}' | python -m json.tool | less
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"google.golang.org/grpc"
)
var (
dgraph = flag.String("d", "127.0.0.1:9080", "Dgraph Alpha address")
)
func main() {
flag.Parse()
conn, err := grpc.Dial(*dgraph, grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))
resp, err := dg.NewTxn().Query(context.Background(), `{
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) }
}
}`)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Response: %s\n", resp.Json)
}
import io.dgraph.DgraphClient;
import io.dgraph.DgraphGrpc;
import io.dgraph.DgraphGrpc.DgraphStub;
import io.dgraph.DgraphProto.Response;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.Map;
public class App {
public static void main(final String[] args) {
ManagedChannel channel =
ManagedChannelBuilder.forAddress("localhost", 9080).usePlaintext(true).build();
DgraphStub stub = DgraphGrpc.newStub(channel);
DgraphClient dgraphClient = new DgraphClient(stub);
String query = "{\n "+"var(func: has(festival.date_founded)) {\n "+"p as festival.date_founded\n "+"}\n "+"query(func: eq(val(p), \"1961-01-01T00:00:00Z\")) {\n "+"uid\n "+"name@en\n "+"name@ru\n "+"name@pl\n "+"festival.date_founded\n "+"festival.focus { name@en }\n "+"festival.individual_festivals { total : count(uid) }\n "+"}\n "+"}";
Response res = dgraphClient.newTransaction().query(query);
System.out.printf("Response: %s", res.getJson().toStringUtf8());
}
}
import pydgraph
import json
def main():
client_stub = pydgraph.DgraphClientStub("localhost:9080")
client = pydgraph.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) }
}
}"""
res = client.txn(read_only=True).query(query)
print('Response: {}'.format(json.loads(res.json)))
client_stub.close()
if __name__ == '__main__':
try:
main()
except Exception as e:
print('Error: {}'.format(e))
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 response = await dgraphClient.newTxn().query(query);
console.log("Response: ", JSON.stringify(response.getJson()));
clientStub.close();
}
main().then().catch((e) => {
console.log("ERROR: ", e);
});
const dgraph = require("dgraph-js-http");
async function main() {
const clientStub = new dgraph.DgraphClientStub("http://localhost:8080");
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 response = await dgraphClient.newTxn().query(query);
console.log("Response: ", JSON.stringify(response.data));
}
main().then().catch((e) => {
console.log("ERROR: ", e);
});
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
.
{
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
}
}
}
}
}
curl -H "Content-Type: application/dql" localhost:8080/query -XPOST -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
}
}
}
}
}' | python -m json.tool | less
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"google.golang.org/grpc"
)
var (
dgraph = flag.String("d", "127.0.0.1:9080", "Dgraph Alpha address")
)
func main() {
flag.Parse()
conn, err := grpc.Dial(*dgraph, grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))
resp, err := dg.NewTxn().Query(context.Background(), `{
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
}
}
}
}
}`)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Response: %s\n", resp.Json)
}
import io.dgraph.DgraphClient;
import io.dgraph.DgraphGrpc;
import io.dgraph.DgraphGrpc.DgraphStub;
import io.dgraph.DgraphProto.Response;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.Map;
public class App {
public static void main(final String[] args) {
ManagedChannel channel =
ManagedChannelBuilder.forAddress("localhost", 9080).usePlaintext(true).build();
DgraphStub stub = DgraphGrpc.newStub(channel);
DgraphClient dgraphClient = new DgraphClient(stub);
String query = "{\n "+"spielbergMovies as var(func: allofterms(name@en, \"steven spielberg\")) {\n "+"name@en\n "+"director.film (orderasc: name@en, first: 1) {\n "+"starring {\n "+"performance.actor {\n "+"ActorName as name@en\n "+"}\n "+"# Stars is a uid-to-value map mapping\n "+"# starring edges to performance.actor names\n "+"Stars as min(val(ActorName))\n "+"}\n "+"}\n "+"}\n "+"\n "+"movies(func: uid(spielbergMovies)) @cascade {\n "+"name@en\n "+"director.film (orderasc: name@en, first: 1) {\n "+"name@en\n "+"starring (orderasc: val(Stars)) {\n "+"performance.actor {\n "+"name@en\n "+"}\n "+"}\n "+"}\n "+"}\n "+"}";
Response res = dgraphClient.newTransaction().query(query);
System.out.printf("Response: %s", res.getJson().toStringUtf8());
}
}
import pydgraph
import json
def main():
client_stub = pydgraph.DgraphClientStub("localhost:9080")
client = pydgraph.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
}
}
}
}
}"""
res = client.txn(read_only=True).query(query)
print('Response: {}'.format(json.loads(res.json)))
client_stub.close()
if __name__ == '__main__':
try:
main()
except Exception as e:
print('Error: {}'.format(e))
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 response = await dgraphClient.newTxn().query(query);
console.log("Response: ", JSON.stringify(response.getJson()));
clientStub.close();
}
main().then().catch((e) => {
console.log("ERROR: ", e);
});
const dgraph = require("dgraph-js-http");
async function main() {
const clientStub = new dgraph.DgraphClientStub("http://localhost:8080");
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 response = await dgraphClient.newTxn().query(query);
console.log("Response: ", JSON.stringify(response.data));
}
main().then().catch((e) => {
console.log("ERROR: ", e);
});
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.
{
var(func: eq(name@en, "Steven Spielberg")) {
director.film {
genres as genre
}
}
q(func: uid(genres)) {
name@.
}
}
curl -H "Content-Type: application/dql" localhost:8080/query -XPOST -d '
{
var(func: eq(name@en, "Steven Spielberg")) {
director.film {
genres as genre
}
}
q(func: uid(genres)) {
name@.
}
}' | python -m json.tool | less
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"google.golang.org/grpc"
)
var (
dgraph = flag.String("d", "127.0.0.1:9080", "Dgraph Alpha address")
)
func main() {
flag.Parse()
conn, err := grpc.Dial(*dgraph, grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))
resp, err := dg.NewTxn().Query(context.Background(), `{
var(func: eq(name@en, "Steven Spielberg")) {
director.film {
genres as genre
}
}
q(func: uid(genres)) {
name@.
}
}`)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Response: %s\n", resp.Json)
}
import io.dgraph.DgraphClient;
import io.dgraph.DgraphGrpc;
import io.dgraph.DgraphGrpc.DgraphStub;
import io.dgraph.DgraphProto.Response;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.Map;
public class App {
public static void main(final String[] args) {
ManagedChannel channel =
ManagedChannelBuilder.forAddress("localhost", 9080).usePlaintext(true).build();
DgraphStub stub = DgraphGrpc.newStub(channel);
DgraphClient dgraphClient = new DgraphClient(stub);
String query = "{\n "+"var(func: eq(name@en, \"Steven Spielberg\")) {\n "+"director.film {\n "+"genres as genre\n "+"}\n "+"}\n "+"\n "+"q(func: uid(genres)) {\n "+"name@.\n "+"}\n "+"}";
Response res = dgraphClient.newTransaction().query(query);
System.out.printf("Response: %s", res.getJson().toStringUtf8());
}
}
import pydgraph
import json
def main():
client_stub = pydgraph.DgraphClientStub("localhost:9080")
client = pydgraph.DgraphClient(client_stub)
query = """{
var(func: eq(name@en, "Steven Spielberg")) {
director.film {
genres as genre
}
}
q(func: uid(genres)) {
name@.
}
}"""
res = client.txn(read_only=True).query(query)
print('Response: {}'.format(json.loads(res.json)))
client_stub.close()
if __name__ == '__main__':
try:
main()
except Exception as e:
print('Error: {}'.format(e))
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 response = await dgraphClient.newTxn().query(query);
console.log("Response: ", JSON.stringify(response.getJson()));
clientStub.close();
}
main().then().catch((e) => {
console.log("ERROR: ", e);
});
const dgraph = require("dgraph-js-http");
async function main() {
const clientStub = new dgraph.DgraphClientStub("http://localhost:8080");
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 response = await dgraphClient.newTxn().query(query);
console.log("Response: ", JSON.stringify(response.data));
}
main().then().catch((e) => {
console.log("ERROR: ", e);
});
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
).
{
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
}
}
curl -H "Content-Type: application/dql" localhost:8080/query -XPOST -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
}
}' | python -m json.tool | less
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"google.golang.org/grpc"
)
var (
dgraph = flag.String("d", "127.0.0.1:9080", "Dgraph Alpha address")
)
func main() {
flag.Parse()
conn, err := grpc.Dial(*dgraph, grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))
resp, err := dg.NewTxn().Query(context.Background(), `{
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
}
}`)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Response: %s\n", resp.Json)
}
import io.dgraph.DgraphClient;
import io.dgraph.DgraphGrpc;
import io.dgraph.DgraphGrpc.DgraphStub;
import io.dgraph.DgraphProto.Response;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.Map;
public class App {
public static void main(final String[] args) {
ManagedChannel channel =
ManagedChannelBuilder.forAddress("localhost", 9080).usePlaintext(true).build();
DgraphStub stub = DgraphGrpc.newStub(channel);
DgraphClient dgraphClient = new DgraphClient(stub);
String query = "{ \n "+"exampleData(func: has(email)) {\n "+"uid\n "+"email\n "+"check as checkpwd(pass, \"1bdfhJHb!fd\")\n "+"}\n "+"userMatched(func: eq(val(check), 1)) {\n "+"uid\n "+"email\n "+"}\n "+"userIncorrect(func: eq(val(check), 0)) {\n "+"uid\n "+"email\n "+"}\n "+"}";
Response res = dgraphClient.newTransaction().query(query);
System.out.printf("Response: %s", res.getJson().toStringUtf8());
}
}
import pydgraph
import json
def main():
client_stub = pydgraph.DgraphClientStub("localhost:9080")
client = pydgraph.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
}
}"""
res = client.txn(read_only=True).query(query)
print('Response: {}'.format(json.loads(res.json)))
client_stub.close()
if __name__ == '__main__':
try:
main()
except Exception as e:
print('Error: {}'.format(e))
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 response = await dgraphClient.newTxn().query(query);
console.log("Response: ", JSON.stringify(response.getJson()));
clientStub.close();
}
main().then().catch((e) => {
console.log("ERROR: ", e);
});
const dgraph = require("dgraph-js-http");
async function main() {
const clientStub = new dgraph.DgraphClientStub("http://localhost:8080");
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 response = await dgraphClient.newTxn().query(query);
console.log("Response: ", JSON.stringify(response.data));
}
main().then().catch((e) => {
console.log("ERROR: ", e);
});