From d7feabd79f50f76693322f6e6436126347dcea87 Mon Sep 17 00:00:00 2001 From: minhaj-shakeel Date: Mon, 21 Sep 2020 12:21:23 +0530 Subject: [PATCH] fix(GraphQL): fix for deletion on interfaces with no non Id field (#6387) (#6417) Fixes GRAPHQL-655. For this user schema (interface have no non-Id field): ``` interface A { name: String! @id } type B implements A { age: Int! } ``` the following delete mutation ``` mutation{ deleteA(filter:{name:{eq: "xyz"}}){ a{ name } } } ``` was resulting in the following error: ``` { "errors": [ { "message": "Internal Server Error - a panic was trapped. This indicates a bug in the GraphQL server. A stack trace was logged. Please let us know by filing an issue with the stack trace." } ] } ``` This PR fixes this bug and now deleting can be performed successfully. (cherry picked from commit 742259b4a6514b5ce50169d85da0feba6d69cb70) --- graphql/resolve/delete_mutation_test.yaml | 28 ++++++++++++++++++++++- graphql/resolve/schema.graphql | 4 ++++ graphql/schema/wrappers.go | 10 ++++---- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/graphql/resolve/delete_mutation_test.yaml b/graphql/resolve/delete_mutation_test.yaml index b2e970b9ad8..bd884725eec 100644 --- a/graphql/resolve/delete_mutation_test.yaml +++ b/graphql/resolve/delete_mutation_test.yaml @@ -265,4 +265,30 @@ dgquery: |- query { x as deleteX() - } \ No newline at end of file + } + +- + name: "Deleting an interface with just a field with @id directive" + gqlmutation: | + mutation{ + deleteA(filter:{name:{eq: "xyz"}}){ + a{ + name + } + } + } + dgquery: |- + query { + x as deleteA(func: type(A)) @filter(eq(A.name, "xyz")) { + uid + } + a(func: uid(x)) { + dgraph.type + name : A.name + dgraph.uid : uid + } + } + dgmutations: + - deletejson: | + [{ "uid": "uid(x)"}] + diff --git a/graphql/resolve/schema.graphql b/graphql/resolve/schema.graphql index eef8ed55b4e..ed3211e6e93 100644 --- a/graphql/resolve/schema.graphql +++ b/graphql/resolve/schema.graphql @@ -282,3 +282,7 @@ type ThingTwo implements Thing { prop: String @dgraph(pred: "prop") owner: String } + +interface A { + name: String! @id +} \ No newline at end of file diff --git a/graphql/schema/wrappers.go b/graphql/schema/wrappers.go index ffdd272acd5..9ab898b2122 100644 --- a/graphql/schema/wrappers.go +++ b/graphql/schema/wrappers.go @@ -516,12 +516,12 @@ func mutatedTypeMapping(s *schema, default: } // This is a convoluted way of getting the type for mutatedTypeName. We get the definition - // for UpdateTPayload and get the type from the first field. There is no direct way to get - // the type from the definition of an object. We use Update and not Add here because - // Interfaces only have Update. + // for AddTPayload and get the type from the first field. There is no direct way to get + // the type from the definition of an object. Interfaces can't have Add and if there is no non Id + // field then Update also will not be there, so we use Delete if there is no AddTPayload. var def *ast.Definition - if def = s.schema.Types["Update"+mutatedTypeName+"Payload"]; def == nil { - def = s.schema.Types["Add"+mutatedTypeName+"Payload"] + if def = s.schema.Types["Add"+mutatedTypeName+"Payload"]; def == nil { + def = s.schema.Types["Delete"+mutatedTypeName+"Payload"] } if def == nil {