diff --git a/graphql/e2e/common/common.go b/graphql/e2e/common/common.go index faec43afe16..f2ea9daa4fd 100644 --- a/graphql/e2e/common/common.go +++ b/graphql/e2e/common/common.go @@ -645,9 +645,11 @@ func RunAll(t *testing.T) { t.Run("query aggregate without filter", queryAggregateWithoutFilter) t.Run("query aggregate with filter", queryAggregateWithFilter) t.Run("query aggregate with alias", queryAggregateWithAlias) + t.Run("query aggregate with repeated fields", queryAggregateWithRepeatedFields) t.Run("query aggregate at child level", queryAggregateAtChildLevel) t.Run("query aggregate at child level with filter", queryAggregateAtChildLevelWithFilter) t.Run("query aggregate at child level with multiple alias", queryAggregateAtChildLevelWithMultipleAlias) + t.Run("query aggregate at child level with repeated fields", queryAggregateAtChildLevelWithRepeatedFields) t.Run("query aggregate and other fields at child level", queryAggregateAndOtherFieldsAtChildLevel) t.Run("query at child level with multiple alias on scalar field", queryChildLevelWithMultipleAliasOnScalarField) t.Run("checkUserPassword query", passwordTest) diff --git a/graphql/e2e/common/query.go b/graphql/e2e/common/query.go index ceb8b183777..5dde82004b8 100644 --- a/graphql/e2e/common/query.go +++ b/graphql/e2e/common/query.go @@ -2918,6 +2918,39 @@ func queryAggregateWithAlias(t *testing.T) { string(gqlResponse.Data)) } +func queryAggregateWithRepeatedFields(t *testing.T) { + queryPostParams := &GraphQLParams{ + Query: `query { + aggregatePost { + count + cnt2 : count + tmin : titleMin + tmin_again : titleMin + tmax: titleMax + navg : numLikesAvg + navg2 : numLikesAvg + } + }`, + } + + gqlResponse := queryPostParams.ExecuteAsPost(t, GraphqlURL) + RequireNoGQLErrors(t, gqlResponse) + testutil.CompareJSON(t, + `{ + "aggregatePost": + { + "count":4, + "cnt2":4, + "tmax": "Random post", + "tmin": "GraphQL doco", + "tmin_again": "GraphQL doco", + "navg": 66.25, + "navg2": 66.25 + } + }`, + string(gqlResponse.Data)) +} + func queryAggregateAtChildLevel(t *testing.T) { queryNumberOfStates := &GraphQLParams{ Query: `query @@ -3013,6 +3046,39 @@ func queryAggregateAtChildLevelWithMultipleAlias(t *testing.T) { string(gqlResponse.Data)) } +func queryAggregateAtChildLevelWithRepeatedFields(t *testing.T) { + queryNumberOfIndianStates := &GraphQLParams{ + Query: `query + { + queryCountry(filter: { name: { eq: "India" } }) { + name + ag1: statesAggregate(filter: {xcode: {in: ["ka", "mh"]}}) { + count + cnt2 : count + nameMax + nm : nameMax + } + } + }`, + } + gqlResponse := queryNumberOfIndianStates.ExecuteAsPost(t, GraphqlURL) + RequireNoGQLErrors(t, gqlResponse) + testutil.CompareJSON(t, + ` + { + "queryCountry": [{ + "name": "India", + "ag1": { + "count" : 2, + "cnt2" : 2, + "nameMax" : "Maharashtra", + "nm": "Maharashtra" + } + }] + }`, + string(gqlResponse.Data)) +} + func queryAggregateAndOtherFieldsAtChildLevel(t *testing.T) { queryNumberOfIndianStates := &GraphQLParams{ Query: `query diff --git a/graphql/resolve/query_rewriter.go b/graphql/resolve/query_rewriter.go index 02b2a637006..5c5238c30b3 100644 --- a/graphql/resolve/query_rewriter.go +++ b/graphql/resolve/query_rewriter.go @@ -190,8 +190,14 @@ func aggregateQuery(query schema.Query, authRw *authRewriter) []*gql.GraphQuery } // Add selection set to mainQuery and finalMainQuery. isAggregateFieldVisited := make(map[string]bool) + // isAggregateFunctionVisited stores if the aggregate function has been added or not. + isAggregateFunctionVisited := make(map[string]bool) for _, f := range query.SelectionSet() { fldName := f.Name() + if _, visited := isAggregateFunctionVisited[fldName]; visited { + continue + } + isAggregateFunctionVisited[fldName] = true if fldName == "count" { child := &gql.GraphQuery{ Var: "countVar", diff --git a/graphql/resolve/query_test.yaml b/graphql/resolve/query_test.yaml index 2f0a0dd549b..6969175909b 100644 --- a/graphql/resolve/query_test.yaml +++ b/graphql/resolve/query_test.yaml @@ -1416,7 +1416,9 @@ query { aggregateCountry(filter: { name: { regexp: "/.*ust.*/" }}) { count + cnt : count nameMin + nm : nameMin nameMax } } @@ -2938,7 +2940,9 @@ } statesAggregate(filter: { code: { eq: "state code" } }) { cnt : count + cnt2 : count nMin : nameMin + nameMin nMax : nameMax cMin : capitalMin }