From aa4ece96ea48e85968a17ca9621195402ee463ef Mon Sep 17 00:00:00 2001 From: Pawan Rawal Date: Tue, 28 Apr 2020 17:45:07 +0530 Subject: [PATCH] Fix response for partial admin queries. (#5317) Handle `[]map[string]interface{}` separately from `[]interface{}` in `completObject` function. This fixes the bug where admin queries returned full info even when asked for partial information. --- graphql/e2e/common/admin.go | 29 +++++++++++++++++++++++++++++ graphql/e2e/common/common.go | 1 + graphql/resolve/resolver.go | 8 ++++++++ 3 files changed, 38 insertions(+) diff --git a/graphql/e2e/common/admin.go b/graphql/e2e/common/admin.go index da72091f102..0cba13594d7 100644 --- a/graphql/e2e/common/admin.go +++ b/graphql/e2e/common/admin.go @@ -29,6 +29,7 @@ import ( "github.com/dgraph-io/dgo/v200" "github.com/dgraph-io/dgo/v200/protos/api" "github.com/dgraph-io/dgraph/protos/pb" + "github.com/dgraph-io/dgraph/testutil" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/stretchr/testify/require" @@ -429,6 +430,34 @@ func health(t *testing.T) { } } +func partialHealth(t *testing.T) { + queryParams := &GraphQLParams{ + Query: `query { + health { + instance + status + group + } + }`, + } + gqlResponse := queryParams.ExecuteAsPost(t, graphqlAdminTestAdminURL) + RequireNoGQLErrors(t, gqlResponse) + testutil.CompareJSON(t, `{ + "health": [ + { + "instance": "zero", + "status": "healthy", + "group": "0" + }, + { + "instance": "alpha", + "status": "healthy", + "group": "1" + } + ] + }`, string(gqlResponse.Data)) +} + // The GraphQL /admin state result should be the same as /state func adminState(t *testing.T) { queryParams := &GraphQLParams{ diff --git a/graphql/e2e/common/common.go b/graphql/e2e/common/common.go index b4164599919..008e3e7a27a 100644 --- a/graphql/e2e/common/common.go +++ b/graphql/e2e/common/common.go @@ -213,6 +213,7 @@ func RunAll(t *testing.T) { // admin tests t.Run("admin", admin) t.Run("health", health) + t.Run("partial health", partialHealth) t.Run("state", adminState) t.Run("propagate client remote ip", clientInfoLogin) diff --git a/graphql/resolve/resolver.go b/graphql/resolve/resolver.go index 911730c9fd4..8cbb5d49a5e 100644 --- a/graphql/resolve/resolver.go +++ b/graphql/resolve/resolver.go @@ -1178,6 +1178,14 @@ func completeValue( return completeObject(path, field.SelectionSet(), val) case []interface{}: return completeList(path, field, val) + case []map[string]interface{}: + // This case is different from the []interface{} case above and is true for admin queries + // where we built the val ourselves. + listVal := make([]interface{}, 0, len(val)) + for _, v := range val { + listVal = append(listVal, v) + } + return completeList(path, field, listVal) default: if val == nil { if field.Type().ListType() != nil {