How do I pass parameter to view? #9903
-
I have a view that does custom aggregation on a table. However, I need to do the aggregation on a particular date range. How do I pass the start and end date parameter dynamically when doing graphql query? For example:
And then from graphql query I should be able to do something like.
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
It depends how tied you are to the view. Views don't take parameters; you'll want a function for that. If you don't really need the view, you could write the query directly; it'd look something like this: query {
events_aggregate(
where: {
_and: [
{start_at: {_gte: "2022-01-01"}},
{start_at: {_lt: "2022-02-01"}}
]
}
) {
aggregate {
count
}
}
} This uses the comparison operators If you really this to be encoded in the database itself, I recommend using a function, and then calling that function from Hasura, or using a native query. |
Beta Was this translation helpful? Give feedback.
-
Understood, yes I do need the view since the query is complex that cannot be done with direct query (above example was a simplified). I'll look at the docs and figure out alternative. At least I know view cannot take parameters. |
Beta Was this translation helpful? Give feedback.
It depends how tied you are to the view. Views don't take parameters; you'll want a function for that.
If you don't really need the view, you could write the query directly; it'd look something like this:
This uses the comparison operators
_gte
for>=
and_lt
for<
.If you really this to be encoded in the database itself, I recommend using a function, and then calling that function from Hasura, or using a native query.