Cascade Directive
With the @cascade
directive, nodes that don’t have all predicates specified in the query are removed. This can be useful in cases where some filter was applied or if nodes might not have all listed predicates.
Query Example: Harry Potter movies, with each actor and characters played. With @cascade
, any character not played by an actor called Warwick is removed, as is any Harry Potter movie without any actors called Warwick. Without @cascade
, every character is returned, but only those played by actors called Warwick also have the actor name.
{
HP(func: allofterms(name@en, "Harry Potter")) @cascade {
name@en
starring{
performance.character {
name@en
}
performance.actor @filter(allofterms(name@en, "Warwick")){
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);
});
You can apply @cascade
on inner query blocks as well.
{
HP(func: allofterms(name@en, "Harry Potter")) {
name@en
genre {
name@en
}
starring @cascade {
performance.character {
name@en
}
performance.actor @filter(allofterms(name@en, "Warwick")){
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);
});
Parameterized @cascade
The @cascade
directive can optionally take a list of fields as an argument.
This changes the default behavior, considering only the supplied fields as mandatory instead of all the fields for a type.
Listed fields are automatically cascaded as a required argument to nested selection sets.
A parameterized cascade works on levels (e.g. on the root function or on lower levels), so
you need to specify @cascade(param)
on the exact level where you want it to be applied.
@cascade(predicate)
is that the predicate needs to be in the query at the same level @cascade
is.
Take the following query as an example:
{
nodes(func: allofterms(name@en, "jones indiana")) {
name@en
genre @filter(anyofterms(name@en, "action adventure")) {
name@en
}
produced_by {
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);
});
This query gets nodes that have all the terms “jones indiana” and then traverses to genre
and produced_by
.
It also adds an additional filter for genre
, to only get the ones that either have “action” or “adventure” in the name.
The results include nodes that have no genre
and nodes that have no genre
and no producer
.
If you apply a regular @cascade
without a parameter, you’ll lose the ones that had genre
but no producer
.
To get the nodes that have the traversed genre
but possibly not produced_by
, you can parameterize the cascade:
{
nodes(func: allofterms(name@en, "jones indiana")) @cascade(genre) {
name@en
genre @filter(anyofterms(name@en, "action adventure")) {
name@en
}
produced_by {
name@en
}
written_by {
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 you want to check for multiple fields, just comma separate them. For example, to cascade over produced_by
and written_by
:
{
nodes(func: allofterms(name@en, "jones indiana")) @cascade(produced_by,written_by) {
name@en
genre @filter(anyofterms(name@en, "action adventure")) {
name@en
}
produced_by {
name@en
}
written_by {
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);
});
Nesting and parameterized cascade
The cascading nature of field selection is overwritten by a nested @cascade
.
The previous example can be cascaded down the chain as well, and be overridden on each level as needed.
For example, if you only want the “Indiana Jones movies that were produced by the same person who produced a Jurassic World movie”:
{
nodes(func: allofterms(name@en, "jones indiana")) @cascade(produced_by) {
name@en
genre @filter(anyofterms(name@en, "action adventure")) {
name@en
}
produced_by @cascade(producer.film) {
name@en
producer.film @filter(allofterms(name@en, "jurassic world")) {
name@en
}
}
written_by {
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);
});
Another nested example: “Find the Indiana Jones movie that was written by the same person who wrote a Star Wars movie and was produced by the same person who produced Jurassic World”:
{
nodes(func: allofterms(name@en, "jones indiana")) @cascade(produced_by,written_by) {
name@en
genre @filter(anyofterms(name@en, "action adventure")) {
name@en
}
produced_by @cascade(producer.film) {
name@en
producer.film @filter(allofterms(name@en, "jurassic world")) {
name@en
}
}
written_by @cascade(writer.film) {
name@en
writer.film @filter(allofterms(name@en, "star wars")) {
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);
});