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
  • Loading branch information
all-seeing-code committed Sep 30, 2020
1 parent 1d34ade commit cec5567
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
41 changes: 41 additions & 0 deletions graphql/e2e/common/fragment.go
Expand Up @@ -161,6 +161,27 @@ 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
}
}
queryThing {
__typename
... on ThingOne {
Expand Down Expand Up @@ -269,6 +290,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"
}
],
"queryThing":[
{
"__typename": "ThingOne",
Expand Down
9 changes: 9 additions & 0 deletions graphql/resolve/resolver.go
Expand Up @@ -1268,6 +1268,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{})
for _, f := range fields {
Expand All @@ -1285,6 +1289,9 @@ func completeObject(
if len(dgraphTypes) > 0 {
includeField = f.IncludeInterfaceField(dgraphTypes)
}
if _, ok := seenField[f.ResponseName()]; ok {
includeField = false
}
if !includeField {
continue
}
Expand All @@ -1294,6 +1301,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 cec5567

Please sign in to comment.