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): Fix GraphQL encoding in case of empty list #7726

Merged
merged 2 commits into from Apr 15, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion graphql/e2e/common/common.go
Expand Up @@ -862,7 +862,8 @@ func RunAll(t *testing.T) {
t.Run("add multiple mutations", testMultipleMutations)
t.Run("deep XID mutations", deepXIDMutations)
t.Run("three level xid", testThreeLevelXID)
t.Run("nested add mutation with @hasInverse", nestedAddMutationWithHasInverse)
t.Run("nested add mutation with multiple linked lists and @hasInverse",
nestedAddMutationWithMultipleLinkedListsAndHasInverse)
t.Run("add mutation with @hasInverse overrides correctly", addMutationWithHasInverseOverridesCorrectly)
t.Run("error in multiple mutations", addMultipleMutationWithOneError)
t.Run("dgraph directive with reverse edge adds data correctly",
Expand Down
6 changes: 5 additions & 1 deletion graphql/e2e/common/mutation.go
Expand Up @@ -4143,14 +4143,17 @@ func intWithList(t *testing.T) {

}

func nestedAddMutationWithHasInverse(t *testing.T) {
func nestedAddMutationWithMultipleLinkedListsAndHasInverse(t *testing.T) {
params := &GraphQLParams{
Query: `mutation addPerson1($input: [AddPerson1Input!]!) {
addPerson1(input: $input) {
person1 {
name
friends {
name
closeFriends {
name
}
friends {
name
}
Expand Down Expand Up @@ -4186,6 +4189,7 @@ func nestedAddMutationWithHasInverse(t *testing.T) {
{
"friends": [
{
"closeFriends": [],
"friends": [
{
"name": "Or"
Expand Down
1 change: 1 addition & 0 deletions graphql/e2e/directives/schema.graphql
Expand Up @@ -207,6 +207,7 @@ type post1{
type Person1 {
id: ID!
name: String!
closeFriends: [Person1] @hasInverse(field: closeFriends)
friends: [Person1] @hasInverse(field: friends)
}

Expand Down
8 changes: 8 additions & 0 deletions graphql/e2e/directives/schema_response.json
Expand Up @@ -350,6 +350,11 @@
"type": "uid",
"list": true
},
{
"predicate": "Person1.closeFriends",
"type": "uid",
"list": true
},
{
"predicate": "Person1.name",
"type": "string"
Expand Down Expand Up @@ -1086,6 +1091,9 @@
},
{
"name": "Person1.friends"
},
{
"name": "Person1.closeFriends"
}
],
"name": "Person1"
Expand Down
1 change: 1 addition & 0 deletions graphql/e2e/normal/schema.graphql
Expand Up @@ -220,6 +220,7 @@ type post1{
type Person1 {
id: ID!
name: String!
closeFriends: [Person1] @hasInverse(field: closeFriends)
friends: [Person1] @hasInverse(field: friends)
}

Expand Down
8 changes: 8 additions & 0 deletions graphql/e2e/normal/schema_response.json
Expand Up @@ -432,6 +432,11 @@
"type": "uid",
"list": true
},
{
"predicate": "Person1.closeFriends",
"type": "uid",
"list": true
},
{
"predicate": "Person1.name",
"type": "string"
Expand Down Expand Up @@ -1119,6 +1124,9 @@
},
{
"name": "Person1.friends"
},
{
"name": "Person1.closeFriends"
}
],
"name": "Person1"
Expand Down
11 changes: 10 additions & 1 deletion query/outputnode_graphql.go
Expand Up @@ -444,7 +444,16 @@ func (genc *graphQLEncoder) encode(encInp encodeInput) bool {
}

// Step-3: Update counters and Write closing ] for JSON arrays
if !curSelectionIsDgList || next == nil || genc.getAttr(cur) != genc.getAttr(next) {
// We perform this step in any of the 4 conditions is satisfied.
// 1. The current selection is not a Dgraph List (It's of custom type or a single JSON object)
// 2. We are at the end of json encoding process and there is no fastjson node ahead (next == nil)
// 3. We are at the end of list writing and the type of next fastJSON node is not equal to
// type of curr fastJSON node.
// 4. The current selection set which we are encoding is not equal to the type of
// current fastJSON node.
if !curSelectionIsDgList || next == nil ||
genc.getAttr(cur) != genc.getAttr(next) ||
curSelection.DgraphAlias() != genc.attrForID(genc.getAttr(cur)) {
if curSelectionIsDgList && !nullWritten {
x.Check2(genc.buf.WriteRune(']'))
}
Expand Down