Skip to content

graphql: Introduce @cascade in GraphQL #5511

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

Merged
merged 5 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
introduce @cascade in GraphQL
  • Loading branch information
abhimanyusinghgaur committed May 23, 2020
commit 2115d19bef86b6a7139f7e3ac5f2281527a50591
1 change: 1 addition & 0 deletions graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func RunAll(t *testing.T) {
t.Run("query post with author", queryPostWithAuthor)
t.Run("queries have extensions", queriesHaveExtensions)
t.Run("alias works for queries", queryWithAlias)
t.Run("cascade directive", queryWithCascade)

// mutation tests
t.Run("add mutation", addMutation)
Expand Down
11 changes: 5 additions & 6 deletions graphql/e2e/common/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -1514,13 +1514,12 @@ func deleteCountry(
deleteGqlType(t, "Country", filter, expectedNumUids, expectedErrors)
}

func deleteAuthor(
func deleteAuthors(
t *testing.T,
authorID string,
expectedNumUids int,
authorIDs []string,
expectedErrors x.GqlErrorList) {
filter := map[string]interface{}{"id": []string{authorID}}
deleteGqlType(t, "Author", filter, expectedNumUids, expectedErrors)
filter := map[string]interface{}{"id": authorIDs}
deleteGqlType(t, "Author", filter, len(authorIDs), expectedErrors)
}

func deletePost(
Expand Down Expand Up @@ -1911,7 +1910,7 @@ func cleanUp(t *testing.T, countries []*country, authors []*author, posts []*pos
}

for _, author := range authors {
deleteAuthor(t, author.ID, 1, nil)
deleteAuthors(t, []string{author.ID}, nil)
}

for _, country := range countries {
Expand Down
157 changes: 157 additions & 0 deletions graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -1464,3 +1464,160 @@ func queryWithAlias(t *testing.T) {
"postAuthor": { "theName": "Ann Author" }}]}`,
string(gqlResponse.Data))
}

func queryWithCascade(t *testing.T) {
// for testing @cascade with get by ID and filter queries, also for testing @cascade on field
authors := addMultipleAuthorFromRef(t, []*author{
{
Name: "George",
Reputation: 4.5,
Posts: []*post{{Title: "A show about nothing", Text: "Got ya!", Tags: []string{}}},
}, {
Name: "Jerry",
Reputation: 4.6,
Posts: []*post{{Title: "Outside", Tags: []string{}}},
}, {
Name: "Kramer",
Posts: []*post{{Title: "Ha! Cosmo Kramer", Text: "Giddy up!", Tags: []string{}}},
},
}, postExecutor)
authorIds := []string{authors[0].ID, authors[1].ID, authors[2].ID}
postIds := []string{authors[0].Posts[0].PostID, authors[1].Posts[0].PostID,
authors[2].Posts[0].PostID}
getAuthorByIdQuery := `query ($id: ID!) {
getAuthor(id: $id) @cascade {
reputation
posts {
text
}
}
}`

// for testing @cascade with get by XID queries
states := []*state{
{Name: "California", Code: "CA", Capital: "Sacramento"},
{Name: "Texas", Code: "TX"},
}
addStateParams := GraphQLParams{
Query: `mutation ($input: [AddStateInput!]!) {
addState(input: $input) {
numUids
}
}`,
Variables: map[string]interface{}{"input": states},
}
resp := addStateParams.ExecuteAsPost(t, graphqlURL)
RequireNoGQLErrors(t, resp)
testutil.CompareJSON(t, `{"addState":{"numUids":2}}`, string(resp.Data))
getStateByXidQuery := `query ($xid: String!) {
getState(xcode: $xid) @cascade {
xcode
capital
}
}`

tcases := []struct {
name string
query string
variables map[string]interface{}
respData string
}{
{
name: "@cascade on get by ID query returns null",
query: getAuthorByIdQuery,
variables: map[string]interface{}{"id": authors[1].ID},
respData: `{"getAuthor": null}`,
}, {
name: "@cascade on get by ID query returns author",
query: getAuthorByIdQuery,
variables: map[string]interface{}{"id": authors[0].ID},
respData: `{
"getAuthor": {
"reputation": 4.5,
"posts": [{
"text": "Got ya!"
}]
}
}`,
}, {
name: "@cascade on get by XID query returns null",
query: getStateByXidQuery,
variables: map[string]interface{}{"xid": states[1].Code},
respData: `{"getState": null}`,
}, {
name: "@cascade on get by XID query returns state",
query: getStateByXidQuery,
variables: map[string]interface{}{"xid": states[0].Code},
respData: `{
"getState": {
"xcode": "CA",
"capital": "Sacramento"
}
}`,
}, {
name: "@cascade on filter query",
query: `query ($ids: [ID!]) {
queryAuthor(filter: {id: $ids}) @cascade {
reputation
posts {
text
}
}
}`,
variables: map[string]interface{}{"ids": authorIds},
respData: `{
"queryAuthor": [{
"reputation": 4.5,
"posts": [{
"text": "Got ya!"
}]
}]
}`,
}, {
name: "@cascade on query field",
query: `query ($ids: [ID!]) {
queryAuthor(filter: {id: $ids}) {
reputation
posts @cascade {
text
}
}
}`,
variables: map[string]interface{}{"ids": authorIds},
respData: `{
"queryAuthor": [{
"reputation": 4.5,
"posts": [{
"text": "Got ya!"
}]
},{
"reputation": 4.6,
"posts": []
},{
"reputation": null,
"posts": [{
"text": "Giddy up!"
}]
}]
}`,
},
}

for _, tcase := range tcases {
t.Run(tcase.name, func(t *testing.T) {
params := &GraphQLParams{
Query: tcase.query,
Variables: tcase.variables,
}
resp := params.ExecuteAsPost(t, graphqlURL)
RequireNoGQLErrors(t, resp)
testutil.CompareJSON(t, tcase.respData, string(resp.Data))
})
}

// cleanup
deleteAuthors(t, authorIds, nil)
deleteGqlType(t, "Post", map[string]interface{}{"postID": postIds}, len(postIds), nil)
deleteState(t, getXidFilter("xcode", []string{states[0].Code, states[1].Code}), len(states),
nil)
}
87 changes: 87 additions & 0 deletions graphql/resolve/mutation_query_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,93 @@ ADD_UPDATE_MUTATION:
}
}

-
name: "cascade directive on mutation payload"
gqlquery: |
mutation {
ADD_UPDATE_MUTATION @cascade {
post {
title
text
author {
name
dob
}
}
}
}
dgquery: |-
query {
post(func: uid(0x4)) @cascade {
title : Post.title
text : Post.text
author : Post.author {
name : Author.name
dob : Author.dob
dgraph.uid : uid
}
dgraph.uid : uid
}
}

-
name: "cascade directive on mutation query field"
gqlquery: |
mutation {
ADD_UPDATE_MUTATION {
post @cascade {
title
text
author {
name
dob
}
}
}
}
dgquery: |-
query {
post(func: uid(0x4)) @cascade {
title : Post.title
text : Post.text
author : Post.author {
name : Author.name
dob : Author.dob
dgraph.uid : uid
}
dgraph.uid : uid
}
}

-
name: "cascade directive inside mutation query"
gqlquery: |
mutation {
ADD_UPDATE_MUTATION {
post {
title
text
author @cascade {
name
dob
}
}
}
}
dgquery: |-
query {
post(func: uid(0x4)) {
title : Post.title
text : Post.text
author : Post.author @cascade {
name : Author.name
dob : Author.dob
dgraph.uid : uid
}
dgraph.uid : uid
}
}

UPDATE_MUTATION:
-
name: "filter update result"
Expand Down
8 changes: 8 additions & 0 deletions graphql/resolve/query_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ func rewriteAsQueryByIds(field schema.Field, uids []uint64, authRw *authRewriter
addArgumentsToField(dgQuery, field)
selectionAuth := addSelectionSetFrom(dgQuery, field, authRw)
addUID(dgQuery)
addCascadeDirective(dgQuery, field)

if rbac == schema.Uncertain {
dgQuery = authRw.addAuthQueries(field.Type(), dgQuery)
Expand Down Expand Up @@ -293,6 +294,7 @@ func rewriteAsGet(
selectionAuth := addSelectionSetFrom(dgQuery, field, auth)
addUID(dgQuery)
addTypeFilter(dgQuery, field.Type())
addCascadeDirective(dgQuery, field)

if rbac == schema.Uncertain {
dgQuery = auth.addAuthQueries(field.Type(), dgQuery)
Expand Down Expand Up @@ -339,6 +341,7 @@ func rewriteAsQuery(field schema.Field, authRw *authRewriter) *gql.GraphQuery {
addArgumentsToField(dgQuery, field)
selectionAuth := addSelectionSetFrom(dgQuery, field, authRw)
addUID(dgQuery)
addCascadeDirective(dgQuery, field)

if rbac == schema.Uncertain {
dgQuery = authRw.addAuthQueries(field.Type(), dgQuery)
Expand Down Expand Up @@ -613,6 +616,7 @@ func addSelectionSetFrom(
addFilter(child, f.Type(), filter)
addOrder(child, f)
addPagination(child, f)
addCascadeDirective(child, f)
rbac := auth.evaluateStaticRules(f.Type())

selectionAuth := addSelectionSetFrom(child, f, auth)
Expand Down Expand Up @@ -703,6 +707,10 @@ func addPagination(q *gql.GraphQuery, field schema.Field) {
}
}

func addCascadeDirective(q *gql.GraphQuery, field schema.Field) {
q.Cascade = field.Cascade()
}

func convertIDs(idsSlice []interface{}) []uint64 {
ids := make([]uint64, 0, len(idsSlice))
for _, id := range idsSlice {
Expand Down
Loading