diff --git a/server/src-lib/Hasura/RQL/DML/Select/Internal.hs b/server/src-lib/Hasura/RQL/DML/Select/Internal.hs index 038f5b47760f5..8ac8da95f138d 100644 --- a/server/src-lib/Hasura/RQL/DML/Select/Internal.hs +++ b/server/src-lib/Hasura/RQL/DML/Select/Internal.hs @@ -159,8 +159,8 @@ data BaseNode txtToAlias :: Text -> S.Alias txtToAlias = S.Alias . Iden -aggFldToExp :: Iden -> AggFlds -> S.SQLExp -aggFldToExp pfx aggFlds = jsonRow +aggFldToExp :: AggFlds -> S.SQLExp +aggFldToExp aggFlds = jsonRow where jsonRow = S.applyJsonBuildObj (concatMap aggToFlds aggFlds) withAls fldName sqlExp = [S.SELit fldName, sqlExp] @@ -177,7 +177,7 @@ aggFldToExp pfx aggFlds = jsonRow colFldsToExtr op (t, PCFCol col) = [ S.SELit t - , S.SEFnApp op [S.SEIden $ mkBaseTableColAls pfx col] Nothing + , S.SEFnApp op [S.SEIden $ toIden col] Nothing ] colFldsToExtr _ (t, PCFExp e) = [ S.SELit t , S.SELit e] @@ -397,8 +397,7 @@ aggSelToAggNode pfx als aggSel = mkBaseNode pfx (FieldName t) selFld tabFrm tabPerm tabArgs selFldToExtr (t, fld) = (:) (S.SELit t) $ pure $ case fld of - TAFAgg flds -> - aggFldToExp pfx flds + TAFAgg flds -> aggFldToExp flds TAFNodes _ -> let jsonAgg = S.SEFnApp "json_agg" [S.SEIden $ Iden t] ordBy in S.SEFnApp "coalesce" [jsonAgg, S.SELit "[]"] Nothing @@ -454,7 +453,7 @@ mkBaseNode pfx fldAls annSelFlds tableFrom tablePerm tableArgs = mkColExp (PCFCol c) = let qualCol = S.mkQIdenExp (mkBaseTableAls pfx) (toIden c) - colAls = mkBaseTableColAls pfx c + colAls = toIden c in Just (S.Alias colAls, qualCol) mkColExp _ = Nothing diff --git a/server/tests-py/queries/graphql_query/aggregations/article_deeply_nested_aggregate.yaml b/server/tests-py/queries/graphql_query/aggregations/article_deeply_nested_aggregate.yaml new file mode 100644 index 0000000000000..76c8ba51fd477 --- /dev/null +++ b/server/tests-py/queries/graphql_query/aggregations/article_deeply_nested_aggregate.yaml @@ -0,0 +1,222 @@ +description: Deeply nested query with aggregate select +url: /v1alpha1/graphql +status: 200 +response: + data: + article: + - id: 1 + title: Article 1 + content: Sample article content 1 + author: + id: 1 + name: Author 1 + articles: + - id: 1 + title: Article 1 + content: Sample article content 1 + author: + articles_aggregate: + aggregate: + count: 2 + avg: + author_id: 1 + nodes: + - id: 1 + title: Article 1 + content: Sample article content 1 + author: + id: 1 + name: Author 1 + articles_aggregate: + aggregate: + count: 2 + max: + author_id: 1 + - id: 2 + title: Article 2 + content: Sample article content 2 + author: + id: 1 + name: Author 1 + articles_aggregate: + aggregate: + count: 2 + max: + author_id: 1 + - id: 2 + title: Article 2 + content: Sample article content 2 + author: + articles_aggregate: + aggregate: + count: 2 + avg: + author_id: 1 + nodes: + - id: 1 + title: Article 1 + content: Sample article content 1 + author: + id: 1 + name: Author 1 + articles_aggregate: + aggregate: + count: 2 + max: + author_id: 1 + - id: 2 + title: Article 2 + content: Sample article content 2 + author: + id: 1 + name: Author 1 + articles_aggregate: + aggregate: + count: 2 + max: + author_id: 1 + - id: 2 + title: Article 2 + content: Sample article content 2 + author: + id: 1 + name: Author 1 + articles: + - id: 1 + title: Article 1 + content: Sample article content 1 + author: + articles_aggregate: + aggregate: + count: 2 + avg: + author_id: 1 + nodes: + - id: 1 + title: Article 1 + content: Sample article content 1 + author: + id: 1 + name: Author 1 + articles_aggregate: + aggregate: + count: 2 + max: + author_id: 1 + - id: 2 + title: Article 2 + content: Sample article content 2 + author: + id: 1 + name: Author 1 + articles_aggregate: + aggregate: + count: 2 + max: + author_id: 1 + - id: 2 + title: Article 2 + content: Sample article content 2 + author: + articles_aggregate: + aggregate: + count: 2 + avg: + author_id: 1 + nodes: + - id: 1 + title: Article 1 + content: Sample article content 1 + author: + id: 1 + name: Author 1 + articles_aggregate: + aggregate: + count: 2 + max: + author_id: 1 + - id: 2 + title: Article 2 + content: Sample article content 2 + author: + id: 1 + name: Author 1 + articles_aggregate: + aggregate: + count: 2 + max: + author_id: 1 + - id: 3 + title: Article 3 + content: Sample article content 3 + author: + id: 2 + name: Author 2 + articles: + - id: 3 + title: Article 3 + content: Sample article content 3 + author: + articles_aggregate: + aggregate: + count: 1 + avg: + author_id: 2 + nodes: + - id: 3 + title: Article 3 + content: Sample article content 3 + author: + id: 2 + name: Author 2 + articles_aggregate: + aggregate: + count: 1 + max: + author_id: 2 + +query: + query: | + query { + article{ + id + title + content + author{ + id + name + articles{ + id + title + content + author{ + articles_aggregate{ + aggregate{ + count + avg{ + author_id + } + } + nodes{ + id + title + content + author{ + id + name + articles_aggregate{ + aggregate{ + count + max{ + author_id + } + } + } + } + } + } + } + } + } + } + } diff --git a/server/tests-py/test_graphql_queries.py b/server/tests-py/test_graphql_queries.py index df9dba2be8739..0f1ad537fc406 100644 --- a/server/tests-py/test_graphql_queries.py +++ b/server/tests-py/test_graphql_queries.py @@ -60,6 +60,9 @@ def test_author_agg_with_articles(self, hge_ctx): def test_author_agg_with_articles_where(self, hge_ctx): check_query_f(hge_ctx, self.dir() + '/author_agg_with_articles_where.yaml') + def test_article_deeply_nested_aggregate(self, hge_ctx): + check_query_f(hge_ctx, self.dir() + '/article_deeply_nested_aggregate.yaml') + @classmethod def dir(cls): return 'queries/graphql_query/aggregations'