Multiple Query Blocks with DQL
Inside a single query, multiple query blocks are allowed, and each block can have a name. Multiple query blocks are executed in parallel, and they don’t need to be related in any way.
Query Example: “All of Angelina Jolie’s films, with genres, and Peter Jackson’s films since 2008”
{
AngelinaInfo(func:allofterms(name@en, "angelina jolie")) {
name@en
actor.film {
performance.film {
genre {
name@en
}
}
}
}
DirectorInfo(func: eq(name@en, "Peter Jackson")) {
name@en
director.film @filter(ge(initial_release_date, "2008")) {
Release_date: initial_release_date
Name: name@en
}
}
}
curl -H "Content-Type: application/dql" localhost:8080/query -XPOST -d '
blahblah' | 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(), `blahblah`)
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 = "blahblah";
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 = """blahblah"""
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 = `blahblah`;
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 = `blahblah`;
const response = await dgraphClient.newTxn().query(query);
console.log("Response: ", JSON.stringify(response.data));
}
main().then().catch((e) => {
console.log("ERROR: ", e);
});
If queries contain some overlap in answers, the result sets are still independent.
Query Example: “The movies Mackenzie Crook has acted in and the movies Jack Davenport has acted in”
The results sets overlap because both have acted in the Pirates of the Caribbean movies, but the results are independent and both contain the full answers sets.
{
Mackenzie(func:allofterms(name@en, "Mackenzie Crook")) {
name@en
actor.film {
performance.film {
uid
name@en
}
performance.character {
name@en
}
}
}
Jack(func:allofterms(name@en, "Jack Davenport")) {
name@en
actor.film {
performance.film {
uid
name@en
}
performance.character {
name@en
}
}
}
}
curl -H "Content-Type: application/dql" localhost:8080/query -XPOST -d '
blahblah' | 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(), `blahblah`)
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 = "blahblah";
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 = """blahblah"""
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 = `blahblah`;
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 = `blahblah`;
const response = await dgraphClient.newTxn().query(query);
console.log("Response: ", JSON.stringify(response.data));
}
main().then().catch((e) => {
console.log("ERROR: ", e);
});
Variable (var
) blocks
Variable blocks (var
blocks) start with the keyword var
and are not returned
in the query results, but do affect the contents of query results.
Query Example: “Angelina Jolie’s movies ordered by genre”
{
var(func:allofterms(name@en, "angelina jolie")) {
name@en
actor.film {
A AS performance.film {
B AS genre
}
}
}
films(func: uid(B), orderasc: name@en) {
name@en
~genre @filter(uid(A)) {
name@en
}
}
}
curl -H "Content-Type: application/dql" localhost:8080/query -XPOST -d '
blahblah' | 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(), `blahblah`)
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 = "blahblah";
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 = """blahblah"""
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 = `blahblah`;
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 = `blahblah`;
const response = await dgraphClient.newTxn().query(query);
console.log("Response: ", JSON.stringify(response.data));
}
main().then().catch((e) => {
console.log("ERROR: ", e);
});
Multiple var
blocks
You can also use multiple var
blocks within a single query operation. You can
use variables from one var
block in any of the subsequent blocks, but not
within the same block.
Query Example: “Movies containing both Angelina Jolie and Morgan Freeman sorted by name”
{
var(func:allofterms(name@en, "angelina jolie")) {
name@en
actor.film {
A AS performance.film
}
}
var(func:allofterms(name@en, "morgan freeman")) {
name@en
actor.film {
B as performance.film @filter(uid(A))
}
}
films(func: uid(B), orderasc: name@en) {
name@en
}
}
curl -H "Content-Type: application/dql" localhost:8080/query -XPOST -d '
blahblah' | 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(), `blahblah`)
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 = "blahblah";
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 = """blahblah"""
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 = `blahblah`;
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 = `blahblah`;
const response = await dgraphClient.newTxn().query(query);
console.log("Response: ", JSON.stringify(response.data));
}
main().then().catch((e) => {
console.log("ERROR: ", e);
});
Combining multiple var
blocks
You could get the same query results by logically combining both both var
blocks
in the films block, as follows:
{
var(func:allofterms(name@en, "angelina jolie")) {
name@en
actor.film {
A AS performance.film
}
}
var(func:allofterms(name@en, "morgan freeman")) {
name@en
actor.film {
B as performance.film
}
}
films(func: uid(A,B), orderasc: name@en) @filter(uid(A) AND uid(B)) {
name@en
}
}
The root uid
function unions the uid
s from var
A
and B
, so you need a
filter to intersect the uid
s from var
A
and B
.