Skip to content
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
19 changes: 19 additions & 0 deletions execution/engine/execution_engine_grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1432,4 +1432,23 @@ func TestGRPCSubgraphExecution(t *testing.T) {
require.Contains(t, response, `"teamsByProject":`)
require.Contains(t, response, `"favoriteCategories":`)
})

t.Run("should handle empty and nullable list items", func(t *testing.T) {
operation := graphql.Request{
OperationName: "EmptyAndNullableListItems",
Query: `query EmptyAndNullableListItems {
author {
id
authorGroups {
id
name
}
}
}`,
}

response, err := executeOperation(t, conn, operation, withGRPCMapping(mapping.DefaultGRPCMapping()))
require.NoError(t, err)
require.Equal(t, `{"data":{"author":{"id":"author-default","authorGroups":[[{"id":"group-auth-1","name":"Team Lead Alpha"},{"id":"group-auth-2","name":"Senior Dev Beta"}],[{"id":"group-auth-3","name":"Junior Dev Gamma"}],[],null]}}}`, response)
})
}
11 changes: 5 additions & 6 deletions v2/pkg/engine/datasource/grpc_datasource/grpc_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,12 @@ func (d *DataSource) traverseList(level int, arena *astjson.Arena, current *astj

msg := data.Get(fd).Message()
if !msg.IsValid() {
// If the message is not valid we can either return null if the list is nullable or an error if it is non nullable.
if md.LevelInfo[level].Optional {
return arena.NewNull(), nil
}

return arena.NewArray(), nil
return arena.NewArray(), fmt.Errorf("cannot add null item to response for non nullable list")
}

fd = msg.Descriptor().Fields().ByNumber(1)
Expand All @@ -328,11 +329,9 @@ func (d *DataSource) traverseList(level int, arena *astjson.Arena, current *astj

list := msg.Get(fd).List()
if !list.IsValid() {
if md.LevelInfo[level].Optional {
return arena.NewNull(), nil
}

return arena.NewNull(), fmt.Errorf("cannot add null item to response for non nullable list")
// If the list is not valid, we return an empty array here as the
// nullabilty is checked on the outer List wrapper type.
return arena.NewArray(), nil
}

for i := 0; i < list.Len(); i++ {
Expand Down
4 changes: 4 additions & 0 deletions v2/pkg/grpctest/mockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,10 @@ func (s *MockService) QueryAuthor(ctx context.Context, in *productv1.QueryAuthor
{Id: "group-auth-3", Name: "Junior Dev Gamma"},
},
}},
// empty list
{List: &productv1.ListOfUser_List{}},
// null item
nil,
},
},
},
Expand Down
Loading