hasura / hasura/graphql-engine

Deeply Nested Aggregate query is not filtered by parent colum

Open
#6,753 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
32.1k
Forks
3k
PR merge metrics
PR metrics pending

Description

The query needs to count each occurance of a column value in a "history" table per each "definition" row. This requires looking up possible values and counting each of the possible "ExitReason Codes".

This query gets each **subtask**, but the **count** is the total count of each ExitReason, and not the ExitReason count for the give subtask:

```gql
query SubtaskExitReasonCount {
ivr_subtasks {
friendly_name
subtask_histories {
exitReasonByExitReason {
exit_reason
subtask_histories_aggregate {
aggregate { count }
}
}
}
}
}
```

The following does work as expected; however, it would require filtering by each `subtask_id` rather depending on the implied relationships.

```gql
query SubtaskExitReasonCountWithFilter {
ivr_subtasks {
friendly_name
subtask_histories {
exitReasonByExitReason {
exit_reason
subtask_histories_aggregate(where: {subtask_id: {_eq: 1}}) {
aggregate { count }
}
}
}
}
}
```

- How can the `subtask_histories_aggregate` be filtered/grouped by the `subtask_id`?
- Must this be done with a `VIEW` with an extra group by statement?
- Can a where clause reference a parent relationship property?

```gql
## Pseudo Code
subtask_histories_aggregate(where: {subtask_id: {_eq: "_root.id" }}) {
aggregate { count }
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the nested aggregate behavior with the two GraphQL queries shown in the issue, then trace how the relationship and aggregate query are generated. Done means the aggregate count is scoped to each parent subtask without requiring an explicit subtask_id filter, with regression coverage for the provided case.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, postgres
Domain
api, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.