Skip to content

Commit 59f889b

Browse files
committed
graph: Add ability to order by fields other than timestamp for aggregate entities
1 parent 5a1bb5e commit 59f889b

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

graph/src/schema/api.rs

+25-7
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,12 @@ fn add_types_for_aggregation_types(
485485
input_schema: &InputSchema,
486486
) -> Result<(), APISchemaError> {
487487
for (name, agg_type) in input_schema.aggregation_types() {
488+
// Combine regular fields and aggregate fields for ordering
489+
let mut all_fields = agg_type.fields.to_vec();
490+
for agg in agg_type.aggregates.iter() {
491+
all_fields.push(agg.as_agg_field());
492+
}
493+
add_order_by_type(&mut api.document, name, &all_fields)?;
488494
add_aggregation_filter_type(api, name, agg_type)?;
489495
}
490496
Ok(())
@@ -678,13 +684,25 @@ impl FilterOps {
678684
s::Type::NamedType("OrderDirection".to_string()),
679685
),
680686
],
681-
FilterOps::Aggregation => vec![input_value(
682-
"interval",
683-
"",
684-
s::Type::NonNullType(Box::new(s::Type::NamedType(
685-
"Aggregation_interval".to_string(),
686-
))),
687-
)],
687+
FilterOps::Aggregation => vec![
688+
input_value(
689+
"interval",
690+
"",
691+
s::Type::NonNullType(Box::new(s::Type::NamedType(
692+
"Aggregation_interval".to_string(),
693+
))),
694+
),
695+
input_value(
696+
"orderBy",
697+
"",
698+
s::Type::NamedType(format!("{}_orderBy", type_name)),
699+
),
700+
input_value(
701+
"orderDirection",
702+
"",
703+
s::Type::NamedType("OrderDirection".to_string()),
704+
),
705+
],
688706
};
689707

690708
let mut args = vec![skip, first];

graph/src/schema/input/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ impl Aggregate {
824824

825825
/// The field needed for the finalised aggregation for hourly/daily
826826
/// values
827-
fn as_agg_field(&self) -> Field {
827+
pub fn as_agg_field(&self) -> Field {
828828
Field {
829829
name: self.name.clone(),
830830
field_type: self.field_type.clone(),

graphql/src/store/prefetch.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,8 @@ impl<'a> Loader<'a> {
713713
// that causes unnecessary work in the database
714714
query.order = EntityOrder::Unordered;
715715
}
716-
// Aggregations are always ordered by (timestamp, id)
717-
if child_type.is_aggregation() {
716+
// Apply default timestamp ordering for aggregations if no custom order is specified
717+
if child_type.is_aggregation() && matches!(query.order, EntityOrder::Default) {
718718
let ts = child_type.field(kw::TIMESTAMP).unwrap();
719719
query.order = EntityOrder::Descending(ts.name.to_string(), ts.value_type);
720720
}

0 commit comments

Comments
 (0)