From d6777a473dabd2c1959652126881daf34d852f98 Mon Sep 17 00:00:00 2001 From: JatinDevDG Date: Thu, 11 Mar 2021 10:50:35 +0530 Subject: [PATCH 1/2] added fix for duplicate xid error and tests --- graphql/resolve/add_mutation_test.yaml | 167 +++++++++++++++++++++++++ graphql/resolve/mutation_rewriter.go | 15 ++- graphql/resolve/schema.graphql | 12 ++ 3 files changed, 193 insertions(+), 1 deletion(-) diff --git a/graphql/resolve/add_mutation_test.yaml b/graphql/resolve/add_mutation_test.yaml index f4491455bfc..dd5299ccd36 100644 --- a/graphql/resolve/add_mutation_test.yaml +++ b/graphql/resolve/add_mutation_test.yaml @@ -3681,6 +3681,173 @@ "uid": "_:Book2" } +- + name: "Add mutation with multiple Xid fields shouldn't give error if xidName+xidVal is equal for two different xid fields in a type" + gqlmutation: | + mutation($input: [AddABCInput!]!) { + addABC(input: $input) { + aBC { + ab + abc + } + } + } + + gqlvariables: | + { + "input": [ + { + "ab": "cd", + "abc": "d" + } + ] + } + dgquery: |- + query { + ABC1(func: eq(ABC.ab, "cd")) @filter(type(ABC)) { + uid + } + ABC2(func: eq(ABC.abc, "d")) @filter(type(ABC)) { + uid + } + } + explanation: "We should generate different variables as ABC1 and ABC2 if xidName+xidValue is same as in above case + i.e. ab+cd and abc+d both equals to abcd" + dgmutations: + - setjson: | + { + "ABC.ab": "cd", + "ABC.abc": "d", + "dgraph.type": [ + "ABC" + ], + "uid":"_:ABC2" + } + +- + name: "Add mutation with multiple Xid fields shouldn't give error if xidName+xidVal is equal for two different xid fields in different objects" + gqlmutation: | + mutation($input: [AddABCInput!]!) { + addABC(input: $input) { + aBC { + ab + abc + } + } + } + + gqlvariables: | + { + "input": [ + { + "ab": "cd", + "abc": "de" + }, + { + "ab": "ef", + "abc": "d" + } + ] + } + dgquery: |- + query { + ABC1(func: eq(ABC.ab, "cd")) @filter(type(ABC)) { + uid + } + ABC2(func: eq(ABC.abc, "de")) @filter(type(ABC)) { + uid + } + ABC3(func: eq(ABC.ab, "ef")) @filter(type(ABC)) { + uid + } + ABC4(func: eq(ABC.abc, "d")) @filter(type(ABC)) { + uid + } + } + explanation: "We should generate different variables as ABC1 and ABC4 if xidName+xidValue is same in two different objects as in above case + i.e. ab+cd and abc+d both equals to abcd" + dgmutations: + - setjson: | + { + "ABC.ab": "cd", + "ABC.abc": "de", + "dgraph.type": [ + "ABC" + ], + "uid":"_:ABC2" + } + - setjson: | + { + "ABC.ab": "ef", + "ABC.abc": "d", + "dgraph.type": [ + "ABC" + ], + "uid":"_:ABC4" + } + +- + name: "Add mutation with multiple Xid fields shouldn't give error if typeName+xidName+xidVal is equal for two different xid fields in different types" + gqlmutation: | + mutation($input: [AddABCInput!]!) { + addABC(input: $input) { + aBC { + ab + abc + AB { + Cab + Cabc + } + } + } + } + + gqlvariables: | + { + "input": [ + { + "ab": "cd", + "abc": "de", + "AB": { + "Cab": "cde", + "Cabc":"d" + } + } + ] + } + dgquery: |- + query { + ABC1(func: eq(ABC.ab, "cd")) @filter(type(ABC)) { + uid + } + ABC2(func: eq(ABC.abc, "de")) @filter(type(ABC)) { + uid + } + AB3(func: eq(AB.Cab, "cde")) @filter(type(AB)) { + uid + } + AB4(func: eq(AB.Cabc, "d")) @filter(type(AB)) { + uid + } + } + explanation: "We should generate different variables as ABC1 and AB3, or ABC2 and AB4 if typename+xidName+xidValue is same in two different types as in above case + i.e. ABC+ab+cd and AB+Cabc+d both equals to ABCabcd" + dgmutations: + - setjson: | + { + "ABC.AB": { + "AB.Cab": "cde", + "AB.Cabc": "d", + "dgraph.type": ["AB"], + "uid": "_:AB4" + }, + "ABC.ab": "cd", + "ABC.abc": "de", + "dgraph.type": ["ABC"], + "uid": "_:ABC2" + } + + - name: "Add type with multiple Xid fields at deep level" gqlmutation: | diff --git a/graphql/resolve/mutation_rewriter.go b/graphql/resolve/mutation_rewriter.go index b4bbeb3d276..f4ec5e27929 100644 --- a/graphql/resolve/mutation_rewriter.go +++ b/graphql/resolve/mutation_rewriter.go @@ -139,7 +139,20 @@ func (v *VariableGenerator) Next(typ schema.Type, xidName, xidVal string, auth b if xidName == "" || xidVal == "" { key = typ.Name() } else { - key = typ.FieldOriginatedFrom(xidName) + xidName + xidVal + // We add "." between values while generating key to removes duplicate xidError from below type of cases + // mutation { + // addABC(input: [{ ab: "cd", abc: "d" }]) { + // aBC { + // ab + // abc + // } + // } + // } + // The two generated kes for this case will be + // ABC.ab.cd and ABC.abc.d + // It also ensures that xids from different types gets different variable names + // here we are using the assertion that field name or type name can't have "." in them + key = typ.FieldOriginatedFrom(xidName) + "." + xidName + "." + xidVal } if varName, ok := v.xidVarNameMap[key]; ok { return varName diff --git a/graphql/resolve/schema.graphql b/graphql/resolve/schema.graphql index 15ec8245419..840abe45213 100644 --- a/graphql/resolve/schema.graphql +++ b/graphql/resolve/schema.graphql @@ -426,3 +426,15 @@ type Bar { id: String! @id foo: Foo! } + +type ABC { + ab: String! @id + abc: String! @id + AB: AB +} + +type AB { + Cab: String! @id + Cabc: String! @id +} + From 7e451bd69e250d9e5c8ba947cb949a05e5d339a3 Mon Sep 17 00:00:00 2001 From: JatinDevDG Date: Thu, 11 Mar 2021 11:55:44 +0530 Subject: [PATCH 2/2] fixed comment. --- graphql/resolve/mutation_rewriter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphql/resolve/mutation_rewriter.go b/graphql/resolve/mutation_rewriter.go index f4ec5e27929..710a1285a79 100644 --- a/graphql/resolve/mutation_rewriter.go +++ b/graphql/resolve/mutation_rewriter.go @@ -148,7 +148,7 @@ func (v *VariableGenerator) Next(typ schema.Type, xidName, xidVal string, auth b // } // } // } - // The two generated kes for this case will be + // The two generated keys for this case will be // ABC.ab.cd and ABC.abc.d // It also ensures that xids from different types gets different variable names // here we are using the assertion that field name or type name can't have "." in them