From a0ab03a31edc9808e8f4e03e1bf9f95bffffc104 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Wed, 7 Sep 2022 08:38:41 +0530 Subject: [PATCH] MM-46869: Fix channel props not appearing in GraphQL (#20944) The props map was being null, but the schema did not allow nulls. Therefore, the whole response was being set to null. There was no error which created more confusion. To avoid making the property nillable, which would mean sending pointers to maps, we just create empty maps if they are nil. https://mattermost.atlassian.net/browse/MM-46869 ```release-note NONE ``` --- api4/resolver_channel.go | 8 +++++++- api4/resolver_channel_test.go | 3 +++ model/utils.go | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/api4/resolver_channel.go b/api4/resolver_channel.go index 3cf4d28893..4e9a59da38 100644 --- a/api4/resolver_channel.go +++ b/api4/resolver_channel.go @@ -87,12 +87,18 @@ func postProcessChannels(c *web.Context, channels []*model.Channel) ([]*channel, // This approach becomes effectively similar to a dataloader if the displayName computation // were to be done at the field level per channel. - // Get DM/GM channelIDs + // Get DM/GM channelIDs and set empty maps as well. var channelIDs []string for _, ch := range channels { if ch.IsGroupOrDirect() { channelIDs = append(channelIDs, ch.Id) } + + // This is needed to avoid sending null, which + // does not match with the schema since props is not nullable. + // And making it nullable would mean taking pointer of a map, + // which is not very idiomatic. + ch.MakeNonNil() } var nameFormat string diff --git a/api4/resolver_channel_test.go b/api4/resolver_channel_test.go index 627706bd48..36c7989ec1 100644 --- a/api4/resolver_channel_test.go +++ b/api4/resolver_channel_test.go @@ -43,6 +43,7 @@ func TestGraphQLChannels(t *testing.T) { TotalMsgCountRoot float64 `json:"totalMsgCountRoot"` LastRootPostAt float64 `json:"lastRootPostAt"` Cursor string `json:"cursor"` + Props map[string]any `json:"props"` Team struct { ID string `json:"id"` DisplayName string `json:"displayName"` @@ -75,6 +76,7 @@ func TestGraphQLChannels(t *testing.T) { totalMsgCountRoot lastRootPostAt cursor + props } } `, @@ -97,6 +99,7 @@ func TestGraphQLChannels(t *testing.T) { assert.NotEmpty(t, ch.PrettyDisplayName) assert.NotEmpty(t, ch.CreateAt) assert.NotEmpty(t, ch.UpdateAt) + assert.NotNil(t, ch.Props) if ch.Type == model.ChannelTypeOpen { numPublic++ } else if ch.Type == model.ChannelTypePrivate { diff --git a/model/utils.go b/model/utils.go index 0b89e03606..75e34c7d29 100644 --- a/model/utils.go +++ b/model/utils.go @@ -190,6 +190,10 @@ func (StringInterface) ImplementsGraphQLType(name string) bool { return name == "StringInterface" } +func (si StringInterface) MarshalJSON() ([]byte, error) { + return json.Marshal((map[string]any)(si)) +} + func (si *StringInterface) UnmarshalGraphQL(input any) error { json, ok := input.(map[string]any) if !ok {