Skip to content

Commit

Permalink
Fix repeatitive fields in aggregate query
Browse files Browse the repository at this point in the history
  • Loading branch information
vmrajas committed Dec 10, 2020
1 parent 9963636 commit 70c443f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
66 changes: 66 additions & 0 deletions graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions graphql/resolve/query_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions graphql/resolve/query_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,9 @@
query {
aggregateCountry(filter: { name: { regexp: "/.*ust.*/" }}) {
count
cnt : count
nameMin
nm : nameMin
nameMax
}
}
Expand Down Expand Up @@ -2938,7 +2940,9 @@
}
statesAggregate(filter: { code: { eq: "state code" } }) {
cnt : count
cnt2 : count
nMin : nameMin
nameMin
nMax : nameMax
cMin : capitalMin
}
Expand Down

0 comments on commit 70c443f

Please sign in to comment.