Skip to content

Commit

Permalink
fix(GraphQL Query): Remove extra fields when querying interfaces (#6596
Browse files Browse the repository at this point in the history
…) (#6647)

(cherry picked from commit cec5567)
This PR fixes: GRAPHQL-638
  • Loading branch information
all-seeing-code authored Oct 5, 2020
1 parent 48f80d6 commit f079c7f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
42 changes: 42 additions & 0 deletions graphql/e2e/common/fragment.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,32 @@ func fragmentInQueryOnInterface(t *testing.T) {
id
}
}
qcRep1: queryCharacter {
name
... on Human {
name
totalCredits
}
... on Droid {
name
primaryFunction
}
}
qcRep2: queryCharacter {
... on Human {
totalCredits
}
name
... on Droid {
primaryFunction
name
}
}
qc2: queryCharacter {
... on Human {
name
n: name
}
}
queryThing {
Expand Down Expand Up @@ -274,6 +296,26 @@ func fragmentInQueryOnInterface(t *testing.T) {
"id": "%s"
}
],
"qcRep1": [
{
"name": "Han",
"totalCredits": 10
},
{
"name": "R2-D2",
"primaryFunction": "Robot"
}
],
"qcRep2": [
{
"totalCredits": 10,
"name": "Han"
},
{
"name": "R2-D2",
"primaryFunction": "Robot"
}
],
"qc2":[
{
"name": "Han",
Expand Down
9 changes: 9 additions & 0 deletions graphql/resolve/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,10 @@ func completeObject(
var buf bytes.Buffer
comma := ""

// Below map keeps track of fields which have been seen as part of
// interface to avoid double entry in the resulting response
seenField := make(map[string]bool)

x.Check2(buf.WriteRune('{'))

dgraphTypes, ok := res["dgraph.type"].([]interface{})
Expand All @@ -1283,6 +1287,9 @@ func completeObject(
if len(dgraphTypes) > 0 {
includeField = f.IncludeInterfaceField(dgraphTypes)
}
if _, ok := seenField[f.ResponseName()]; ok {
includeField = false
}
if !includeField {
continue
}
Expand All @@ -1292,6 +1299,8 @@ func completeObject(
x.Check2(buf.WriteString(f.ResponseName()))
x.Check2(buf.WriteString(`": `))

seenField[f.ResponseName()] = true

val := res[f.DgraphAlias()]
if f.Name() == schema.Typename {
// From GraphQL spec:
Expand Down

0 comments on commit f079c7f

Please sign in to comment.