Skip to content

Commit

Permalink
Fix(GraphQL): Fix GraphQL encoding in case of empty list (#7726) (#7730)
Browse files Browse the repository at this point in the history
* Fix output node rewriting in case of null list

* Fix lint

(cherry picked from commit 9e8ab99)
  • Loading branch information
vmrajas authored Apr 16, 2021
1 parent 828e98a commit 445f7bf
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 3 deletions.
3 changes: 2 additions & 1 deletion graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@
"type": "uid",
"list": true
},
{
"predicate": "Person1.closeFriends",
"type": "uid",
"list": true
},
{
"predicate": "Person1.name",
"type": "string"
Expand Down Expand Up @@ -1061,6 +1066,9 @@
},
{
"name": "Person1.friends"
},
{
"name": "Person1.closeFriends"
}
],
"name": "Person1"
Expand Down
1 change: 1 addition & 0 deletions graphql/e2e/normal/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,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
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@
"type": "uid",
"list": true
},
{
"predicate": "Person1.closeFriends",
"type": "uid",
"list": true
},
{
"predicate": "Person1.name",
"type": "string"
Expand Down Expand Up @@ -1117,6 +1122,9 @@
},
{
"name": "Person1.friends"
},
{
"name": "Person1.closeFriends"
}
],
"name": "Person1"
Expand Down
11 changes: 10 additions & 1 deletion query/outputnode_graphql.go
Original file line number Diff line number Diff line change
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

0 comments on commit 445f7bf

Please sign in to comment.