Skip to content

fix(GraphQL): Link xids properly if there are duplicate xids within the same add request. #6265

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 4 commits into from
Aug 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
65 changes: 61 additions & 4 deletions graphql/resolve/add_mutation_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2216,8 +2216,8 @@

- name: "Deep mutation three level xid"
gqlmutation: |
mutation($auth: AddPost1Input!) {
addPost1(input: [$auth]) {
mutation($auth: [AddPost1Input!]!) {
addPost1(input: $auth) {
post1 {
id
comments {
Expand All @@ -2232,15 +2232,24 @@

gqlvariables: |
{
"auth": {
"auth": [{
"id": "post1",
"comments": [{
"id": "comment1",
"replies": [{
"id": "reply1"
}]
}]
}
},
{
"id": "post2",
"comments": [{
"id": "comment2",
"replies": [{
"id": "reply1"
}]
}]
}]
}
dgquery: |-
query {
Expand All @@ -2250,8 +2259,12 @@
Comment16 as Comment16(func: eq(Comment1.id, "reply1")) @filter(type(Comment1)) {
uid
}
Comment110 as Comment110(func: eq(Comment1.id, "comment2")) @filter(type(Comment1)) {
uid
}
}
dgmutations:
# Create comment1 if it doesn't exist.
- setjson: |
{
"Comment1.id": "comment1",
Expand All @@ -2261,6 +2274,7 @@
"uid": "_:Comment14"
}
cond: "@if(eq(len(Comment14), 0))"
# Create reply1 if it doesn't exist.
- setjson: |
{
"Comment1.id": "reply1",
Expand All @@ -2270,15 +2284,41 @@
"uid": "_:Comment16"
}
cond: "@if(eq(len(Comment16), 0) AND eq(len(Comment14), 0))"
# Link comment1 and reply1 if reply exists but comment doesn't.
- setjson: |
{"uid":"_:Comment14", "Comment1.replies": [{"uid": "uid(Comment16)"}]}
cond: "@if(eq(len(Comment16), 1) AND eq(len(Comment14), 0))"
# Link comment1 and reply1 if both don't exist.
- setjson: |
{"uid":"_:Comment14", "Comment1.replies": [{"uid": "_:Comment16"}]}
cond: "@if(eq(len(Comment16), 0) AND eq(len(Comment14), 0))"
# useless mutation which needs investigation
- setjson: |
{"uid":"uid(Comment16)"}
cond: "@if(eq(len(Comment16), 1) AND eq(len(Comment14), 0))"
# create comment2 if it doesn't exist
- setjson: |
{
"Comment1.id": "comment2",
"dgraph.type": [
"Comment1"
],
"uid": "_:Comment110"
}
cond: "@if(eq(len(Comment110), 0))"
# Link comment1 and reply1 if reply exists but comment doesn't.
- setjson: |
{"uid":"_:Comment110", "Comment1.replies": [{"uid": "uid(Comment16)"}]}
cond: "@if(eq(len(Comment16), 1) AND eq(len(Comment110), 0))"
# Link comment2 and reply1 if both don't exist.
- setjson: |
{"uid":"_:Comment110", "Comment1.replies": [{"uid": "_:Comment16"}]}
cond: "@if(eq(len(Comment16), 0) AND eq(len(Comment110), 0))"
# useless mutation which needs investigation

- setjson: |
{"uid":"uid(Comment16)"}
cond: "@if(eq(len(Comment16), 1) AND eq(len(Comment110), 0))"
dgquerysec: |-
query {
Post12 as Post12(func: eq(Post1.id, "post1")) @filter(type(Post1)) {
Expand All @@ -2287,6 +2327,12 @@
Comment14 as Comment14(func: eq(Comment1.id, "comment1")) @filter(type(Comment1)) {
uid
}
Post18 as Post18(func: eq(Post1.id, "post2")) @filter(type(Post1)) {
uid
}
Comment110 as Comment110(func: eq(Comment1.id, "comment2")) @filter(type(Comment1)) {
uid
}
}
dgmutationssec:
- setjson: |
Expand All @@ -2300,6 +2346,17 @@
"uid":"_:Post12"
}
cond: "@if(eq(len(Post12), 0) AND eq(len(Comment14), 1))"
- setjson: |
{
"Post1.comments":
[{
"uid":"uid(Comment110)"
}],
"Post1.id":"post2",
"dgraph.type":["Post1"],
"uid":"_:Post18"
}
cond: "@if(eq(len(Post18), 0) AND eq(len(Comment110), 1))"

-
name: "Add mutation error on @id field for empty value"
Expand Down
13 changes: 8 additions & 5 deletions graphql/resolve/mutation_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,15 @@ func rewriteObject(
newObj["uid"] = myUID
frag := newFragment(newObj)
frag.newNodes[variable] = typ

results := &mutationRes{secondPass: []*mutationFragment{frag}}
if xid != nil && !atTopLevel && !xidEncounteredFirstTime {
// If this is an xid that has been encountered before, e.g. think add mutations with
// multiple objects as input. In that case we don't need to add the fragment to create this
// object, so we clear it out. We do need other fragments for linking this node to its
// parent which are added later.
results.secondPass = results.secondPass[:0]
}

// if xidString != "", then we are adding with an xid. In which case, we have to ensure
// as part of the upsert that the xid doesn't already exist.
Expand Down Expand Up @@ -1211,11 +1219,6 @@ func rewriteObject(
results.secondPass = appendFragments(results.secondPass, []*mutationFragment{xidFrag})
}

// if !xidEncounteredFirstTime, we have already seen the relevant fragments.
if xid != nil && !atTopLevel && !xidEncounteredFirstTime {
results.firstPass = []*mutationFragment{}
}

return results
}

Expand Down