Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(GraphQL): Add support for repeatitive fields in aggregate query #7094

Merged
merged 2 commits into from
Dec 10, 2020
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
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
68 changes: 68 additions & 0 deletions graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2918,6 +2918,41 @@ 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
tmax_again : titleMax
navg : numLikesAvg
navg2 : numLikesAvg
}
}`,
}

gqlResponse := queryPostParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)
testutil.CompareJSON(t,
`{
"aggregatePost":
{
"count":4,
"cnt2":4,
"tmax": "Random post",
"tmax_again": "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 +3048,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
7 changes: 7 additions & 0 deletions graphql/resolve/query_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,15 @@ 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 for a field has been added or not.
// So the map entries would contain keys as nameMin, ageMin, nameName, etc.
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