Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cascade directive at subqueries #4006

Merged
merged 8 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2260,6 +2260,8 @@ func parseDirective(it *lex.ItemIterator, curp *GraphQuery) error {
} else {
return item.Errorf("Facets parsing failed.")
}
} else if item.Val == "cascade" {
curp.Cascade = true
} else if peek[0].Typ == itemLeftRound {
// this is directive
switch item.Val {
Expand Down
4 changes: 2 additions & 2 deletions query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func treeCopy(gq *gql.GraphQuery, sg *SubGraph) error {

args := params{
Alias: gchild.Alias,
Cascade: sg.Params.Cascade,
Cascade: gchild.Cascade || sg.Params.Cascade,
Expand: gchild.Expand,
Facet: gchild.Facets,
FacetOrder: gchild.FacetOrder,
Expand Down Expand Up @@ -1219,7 +1219,7 @@ func (sg *SubGraph) populateVarMap(doneVars map[string]varValue, sgPath []*SubGr
return err
}
sgPath = sgPath[:len(sgPath)-1] // Backtrack
if !sg.Params.Cascade {
if !child.Params.Cascade {
continue
}

Expand Down
321 changes: 317 additions & 4 deletions query/query4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ package query

import (
"context"
// "encoding/json"
// "fmt"
// "strings"
"testing"

"github.com/stretchr/testify/require"
// "google.golang.org/grpc/metadata"
)

func TestDeleteAndReaddIndex(t *testing.T) {
Expand Down Expand Up @@ -341,3 +337,320 @@ func TestTypeExpandLang(t *testing.T) {
require.JSONEq(t, `{"data": {"q":[
{"make":"Toyota","model":"Prius", "model@jp":"プリウス", "year":2009}]}}`, js)
}

func TestCascadeSubQuery1(t *testing.T) {
query := `
{
me(func: uid(0x01)) {
name
full_name
gender
friend @cascade {
name
full_name
friend {
name
full_name
dob
age
}
}
}
}
`
js := processQueryNoErr(t, query)
require.JSONEq(t, `
{
"data": {
"me": [
{
"name": "Michonne",
"full_name": "Michonne's large name for hashing",
"gender": "female"
}
]
}
}`, js)
}

func TestCascadeSubQuery2(t *testing.T) {
query := `
{
me(func: uid(0x01)) {
name
full_name
gender
friend {
name
full_name
friend @cascade {
name
full_name
dob
age
}
}
}
}
`
js := processQueryNoErr(t, query)
require.JSONEq(t, `
{
"data": {
"me": [
{
"name": "Michonne",
"full_name": "Michonne's large name for hashing",
"gender": "female",
"friend": [
{
"name": "Rick Grimes",
"friend": [
{
"name": "Michonne",
"full_name": "Michonne's large name for hashing",
"dob": "1910-01-01T00:00:00Z",
"age": 38
}
]
},
{
"name": "Glenn Rhee"
},
{
"name": "Daryl Dixon"
},
{
"name": "Andrea"
}
]
}
]
}
}`, js)
}

func TestCascadeRepeatedMultipleLevels(t *testing.T) {
// It should have result same as applying @cascade at the top level friend predicate.
query := `
{
me(func: uid(0x01)) {
name
full_name
gender
friend @cascade {
name
full_name
friend @cascade {
name
full_name
dob
age
}
}
}
}
`
js := processQueryNoErr(t, query)
require.JSONEq(t, `
{
"data": {
"me": [
{
"name": "Michonne",
"full_name": "Michonne's large name for hashing",
"gender": "female"
}
]
}
}`, js)
}

func TestCascadeSubQueryWithFilter(t *testing.T) {
query := `
{
me(func: uid(0x01)) {
name
full_name
gender
friend {
name
full_name
friend @cascade @filter(gt(age, 40)) {
name
full_name
dob
age
}
}
}
}
`
js := processQueryNoErr(t, query)
require.JSONEq(t, `
{
"data": {
"me": [
{
"name": "Michonne",
"full_name": "Michonne's large name for hashing",
"gender": "female",
"friend": [
{
"name": "Rick Grimes"
},
{
"name": "Glenn Rhee"
},
{
"name": "Daryl Dixon"
},
{
"name": "Andrea"
}
]
}
]
}
}`, js)
}

func TestCascadeSubQueryWithVars1(t *testing.T) {
query := `
{
him(func: uid(0x01)) {
L as friend {
B as friend @cascade {
name
}
}
}

me(func: uid(L, B)) {
name
}
}
`
js := processQueryNoErr(t, query)
require.JSONEq(t, `
{
"data": {
"him": [
{
"friend": [
{
"friend": [
{
"name": "Michonne"
}
]
},
{
"friend": [
{
"name": "Glenn Rhee"
}
]
}
]
}
],
"me": [
{
"name": "Michonne"
},
{
"name": "Rick Grimes"
},
{
"name": "Glenn Rhee"
},
{
"name": "Daryl Dixon"
},
{
"name": "Andrea"
}
]
}
}`, js)
}

func TestCascadeSubQueryWithVars2(t *testing.T) {
query := `
{
var(func: uid(0x01)) {
L as friend @cascade {
B as friend
}
}

me(func: uid(L, B)) {
name
}
}
`
js := processQueryNoErr(t, query)
require.JSONEq(t, `
{
"data": {
"me": [
{
"name": "Michonne"
},
{
"name": "Rick Grimes"
},
{
"name": "Glenn Rhee"
},
{
"name": "Andrea"
}
]
}
}`, js)
}

func TestCascadeSubQueryMultiUid(t *testing.T) {
query := `
{
me(func: uid(0x01, 0x02, 0x03)) {
name
full_name
gender
friend @cascade {
name
full_name
friend {
name
full_name
dob
age
}
}
}
}
`
js := processQueryNoErr(t, query)
// Friends of Michonne who don't have full_name predicate associated with them are filtered.
require.JSONEq(t, `
{
"data": {
"me": [
{
"name": "Michonne",
"full_name": "Michonne's large name for hashing",
"gender": "female"
},
{
"name": "King Lear"
},
{
"name": "Margaret"
}
]
}
}
`, js)
}