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

Fix(GraphQL): Fix Upsert with Multiple IDs #7472

Merged
merged 1 commit into from
Feb 23, 2021
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
52 changes: 51 additions & 1 deletion graphql/resolve/add_mutation_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@
cond: "@if(gt(len(State3), 0))"

-
name: "Multiple Upsert Mutation with multiple xids where both existence queries result exist"
name: "Upsert Mutation with multiple xids where both existence queries result exist"
gqlmutation: |
mutation addBook($input: [AddBookInput!]!) {
addBook(input: $input, upsert: true) {
Expand Down Expand Up @@ -962,6 +962,56 @@
}
cond: "@if(gt(len(Book2), 0))"

-
name: "Upsert Mutation with multiple xids where only one of existence queries result exist"
explanation: "Book1 does not exist but Book2 exists. As Book2 exists, this is an upsert.
Even though, Book1 does not exist, the mutation should not update ISBN as it is also an XID."
gqlmutation: |
mutation addBook($input: [AddBookInput!]!) {
addBook(input: $input, upsert: true) {
book {
title
ISBN
}
}
}
gqlvariables: |
{ "input":
[
{
"title": "Sapiens",
"ISBN": "NSW",
"publisher": "penguin"
}
]
}
dgquery: |-
query {
Book1(func: eq(Book.ISBN, "NSW")) @filter(type(Book)) {
uid
}
Book2(func: eq(Book.title, "Sapiens")) @filter(type(Book)) {
uid
}
}
qnametouid: |-
{
"Book2": "0x11"
}
dgquerysec: |-
query {
Book2 as addBook(func: uid(0x11)) @filter(type(Book)) {
uid
}
}
dgmutations:
- setjson: |
{
"uid" : "uid(Book2)",
"Book.publisher": "penguin"
}
cond: "@if(gt(len(Book2), 0))"

-
name: "Multiple Upsert Mutation 2"
explanation: "The first state exists and is updated. Second is created. Country
Expand Down
10 changes: 8 additions & 2 deletions graphql/resolve/mutation_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1413,8 +1413,6 @@ func rewriteObject(
// updating this node.
upsertVar = variable
srcUID = fmt.Sprintf("uid(%s)", variable)
// To ensure that xid is not added to the output json
delete(obj, xid.Name())
} else {
// We return an error as we are at top level of non-upsert mutation and the XID exists.
// We need to conceal the error because we might be leaking information to the user if it
Expand Down Expand Up @@ -1501,6 +1499,14 @@ func rewriteObject(
return nil, upsertVar, retErrors
}
}
} else {
// In case this is known to be an Upsert. We delete all entries of XIDs
// from obj. This is done to prevent any XID entries in the json which is returned
// by rewriteObject and ensure that no XID value gets rewritten due to upsert.
for _, xid := range xids {
// To ensure that xid is not added to the output json in case of upsert
delete(obj, xid.Name())
}
}
}

Expand Down