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 Query): Remove extra fields when querying interfaces #6596

Merged
merged 3 commits into from
Sep 30, 2020
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
41 changes: 41 additions & 0 deletions graphql/e2e/common/fragment.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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