Skip to content

Commit fce0517

Browse files
committed
Refactor EntityOrderByChild*
1 parent 23886da commit fce0517

File tree

4 files changed

+71
-69
lines changed

4 files changed

+71
-69
lines changed

graph/src/components/store/mod.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -246,26 +246,22 @@ impl EntityFilter {
246246
}
247247
}
248248

249+
/// Holds the information needed to query a store.
249250
#[derive(Clone, Debug, PartialEq)]
250-
pub struct EntityOrderByChildObject {
251-
pub entity_type: EntityType,
252-
pub attribute: Attribute,
253-
pub join_attribute: Attribute,
254-
pub derived: bool,
255-
}
256-
257-
#[derive(Clone, Debug, PartialEq)]
258-
pub struct EntityOrderByChildInterface {
259-
pub entity_types: Vec<EntityType>,
260-
pub attribute: Attribute,
251+
pub struct EntityOrderByChildInfo {
252+
/// The attribute of the child entity that is used to order the results.
253+
pub sort_by_attribute: Attribute,
254+
/// The attribute that is used to join to the parent and child entity.
261255
pub join_attribute: Attribute,
256+
/// If true, the child entity is derived from the parent entity.
262257
pub derived: bool,
263258
}
264259

260+
/// Holds the information needed to order the results of a query based on nested entities.
265261
#[derive(Clone, Debug, PartialEq)]
266-
pub enum EntityOrderChild {
267-
Object(EntityOrderByChildObject),
268-
Interface(EntityOrderByChildInterface),
262+
pub enum EntityOrderByChild {
263+
Object(EntityOrderByChildInfo, EntityType),
264+
Interface(EntityOrderByChildInfo, Vec<EntityType>),
269265
}
270266

271267
/// The order in which entities should be restored from a store.
@@ -276,9 +272,9 @@ pub enum EntityOrder {
276272
/// Order descending by the given attribute. Use `id` as a tie-breaker
277273
Descending(String, ValueType),
278274
/// Order ascending by the given attribute of a child entity. Use `id` as a tie-breaker
279-
ChildAscending(EntityOrderChild),
275+
ChildAscending(EntityOrderByChild),
280276
/// Order descending by the given attribute of a child entity. Use `id` as a tie-breaker
281-
ChildDescending(EntityOrderChild),
277+
ChildDescending(EntityOrderByChild),
282278
/// Order by the `id` of the entities
283279
Default,
284280
/// Do not order at all. This speeds up queries where we know that

graph/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ pub mod prelude {
122122
pub use crate::components::store::{
123123
AttributeNames, BlockNumber, CachedEthereumCall, ChainStore, Child, ChildMultiplicity,
124124
EntityCache, EntityChange, EntityChangeOperation, EntityCollection, EntityFilter,
125-
EntityLink, EntityModification, EntityOperation, EntityOrder, EntityOrderByChildInterface,
126-
EntityOrderByChildObject, EntityOrderChild, EntityQuery, EntityRange, EntityWindow,
127-
EthereumCallCache, ParentLink, PartialBlockPtr, PoolWaitStats, QueryStore,
128-
QueryStoreManager, StoreError, StoreEvent, StoreEventStream, StoreEventStreamBox,
129-
SubgraphStore, UnfailOutcome, WindowAttribute, BLOCK_NUMBER_MAX,
125+
EntityLink, EntityModification, EntityOperation, EntityOrder, EntityOrderByChild,
126+
EntityOrderByChildInfo, EntityQuery, EntityRange, EntityWindow, EthereumCallCache,
127+
ParentLink, PartialBlockPtr, PoolWaitStats, QueryStore, QueryStoreManager, StoreError,
128+
StoreEvent, StoreEventStream, StoreEventStreamBox, SubgraphStore, UnfailOutcome,
129+
WindowAttribute, BLOCK_NUMBER_MAX,
130130
};
131131
pub use crate::components::subgraph::{
132132
BlockState, DataSourceTemplateInfo, HostMetrics, RuntimeHost, RuntimeHostBuilder,

graphql/src/store/query.rs

+33-27
Original file line numberDiff line numberDiff line change
@@ -63,40 +63,46 @@ pub(crate) fn build_query<'a>(
6363
EntityOrder::Descending(attr, value_type)
6464
}
6565
(Some((attr, _, Some(child))), OrderDirection::Ascending) => match child {
66-
OrderByChild::Object(child) => {
67-
EntityOrder::ChildAscending(EntityOrderChild::Object(EntityOrderByChildObject {
68-
entity_type: child.entity_type,
69-
attribute: attr,
66+
OrderByChild::Object(child) => EntityOrder::ChildAscending(EntityOrderByChild::Object(
67+
EntityOrderByChildInfo {
68+
sort_by_attribute: attr,
7069
join_attribute: child.join_attribute,
7170
derived: child.derived,
72-
}))
71+
},
72+
child.entity_type,
73+
)),
74+
OrderByChild::Interface(child) => {
75+
EntityOrder::ChildAscending(EntityOrderByChild::Interface(
76+
EntityOrderByChildInfo {
77+
sort_by_attribute: attr,
78+
join_attribute: child.join_attribute,
79+
derived: child.derived,
80+
},
81+
child.entity_types,
82+
))
7383
}
74-
OrderByChild::Interface(child) => EntityOrder::ChildAscending(
75-
EntityOrderChild::Interface(EntityOrderByChildInterface {
76-
entity_types: child.entity_types,
77-
attribute: attr,
78-
join_attribute: child.join_attribute,
79-
derived: child.derived,
80-
}),
81-
),
8284
},
8385
(Some((attr, _, Some(child))), OrderDirection::Descending) => match child {
8486
OrderByChild::Object(child) => {
85-
EntityOrder::ChildDescending(EntityOrderChild::Object(EntityOrderByChildObject {
86-
entity_type: child.entity_type,
87-
attribute: attr,
88-
join_attribute: child.join_attribute,
89-
derived: child.derived,
90-
}))
87+
EntityOrder::ChildDescending(EntityOrderByChild::Object(
88+
EntityOrderByChildInfo {
89+
sort_by_attribute: attr,
90+
join_attribute: child.join_attribute,
91+
derived: child.derived,
92+
},
93+
child.entity_type,
94+
))
95+
}
96+
OrderByChild::Interface(child) => {
97+
EntityOrder::ChildDescending(EntityOrderByChild::Interface(
98+
EntityOrderByChildInfo {
99+
sort_by_attribute: attr,
100+
join_attribute: child.join_attribute,
101+
derived: child.derived,
102+
},
103+
child.entity_types,
104+
))
91105
}
92-
OrderByChild::Interface(child) => EntityOrder::ChildDescending(
93-
EntityOrderChild::Interface(EntityOrderByChildInterface {
94-
entity_types: child.entity_types,
95-
attribute: attr,
96-
join_attribute: child.join_attribute,
97-
derived: child.derived,
98-
}),
99-
),
100106
},
101107
(None, _) => EntityOrder::Default,
102108
};

store/postgres/src/relational_queries.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use graph::components::store::EntityKey;
1616
use graph::data::value::Word;
1717
use graph::prelude::{
1818
anyhow, r, serde_json, Attribute, BlockNumber, ChildMultiplicity, Entity, EntityCollection,
19-
EntityFilter, EntityLink, EntityOrder, EntityOrderByChildInterface, EntityOrderChild,
20-
EntityRange, EntityWindow, ParentLink, QueryExecutionError, StoreError, Value, ENV_VARS,
19+
EntityFilter, EntityLink, EntityOrder, EntityOrderByChild, EntityOrderByChildInfo, EntityRange,
20+
EntityWindow, ParentLink, QueryExecutionError, StoreError, Value, ENV_VARS,
2121
};
2222
use graph::{
2323
components::store::{AttributeNames, EntityType},
@@ -2784,20 +2784,20 @@ impl<'a> SortKey<'a> {
27842784
}
27852785

27862786
fn with_child_interface_key<'a>(
2787-
children: EntityOrderByChildInterface,
2787+
child: EntityOrderByChildInfo,
2788+
entity_types: Vec<EntityType>,
27882789
direction: &'static str,
27892790
parent_table: &'a Table,
27902791
layout: &'a Layout,
27912792
) -> Result<SortKey<'a>, QueryExecutionError> {
27922793
let mut i = 0;
27932794

27942795
Ok(SortKey::ChildKey(ChildKey::Many(
2795-
children
2796-
.entity_types
2796+
entity_types
27972797
.iter()
27982798
.map(|entity_type| {
27992799
let child_table = layout.table_for_entity(entity_type)?;
2800-
let sort_by_column = child_table.column_for_field(&children.attribute)?;
2800+
let sort_by_column = child_table.column_for_field(&child.sort_by_attribute)?;
28012801
if sort_by_column.is_fulltext() {
28022802
Err(QueryExecutionError::NotSupported(
28032803
"Sorting by fulltext fields".to_string(),
@@ -2807,26 +2807,26 @@ impl<'a> SortKey<'a> {
28072807
"Sorting by id".to_string(),
28082808
))
28092809
} else {
2810-
let (parent_column, child_column) = match children.derived {
2810+
let (parent_column, child_column) = match child.derived {
28112811
true => (
28122812
parent_table.primary_key(),
28132813
child_table
2814-
.column_for_field(&children.join_attribute)
2814+
.column_for_field(&child.join_attribute)
28152815
.map_err(|_| {
28162816
graph::constraint_violation!(
28172817
"Column for a join attribute `{}` of `{}` table not found",
2818-
children.join_attribute,
2818+
child.join_attribute,
28192819
child_table.name.as_str()
28202820
)
28212821
})?,
28222822
),
28232823
false => (
28242824
parent_table
2825-
.column_for_field(&children.join_attribute)
2825+
.column_for_field(&child.join_attribute)
28262826
.map_err(|_| {
28272827
graph::constraint_violation!(
28282828
"Column for a join attribute `{}` of `{}` table not found",
2829-
children.join_attribute,
2829+
child.join_attribute,
28302830
parent_table.name.as_str()
28312831
)
28322832
})?,
@@ -2871,31 +2871,31 @@ impl<'a> SortKey<'a> {
28712871
EntityOrder::Default => Ok(SortKey::IdAsc(br_column)),
28722872
EntityOrder::Unordered => Ok(SortKey::None),
28732873
EntityOrder::ChildAscending(kind) => match kind {
2874-
EntityOrderChild::Object(child) => with_child_object_key(
2874+
EntityOrderByChild::Object(child, entity_type) => with_child_object_key(
28752875
table,
2876-
layout.table_for_entity(&child.entity_type)?,
2876+
layout.table_for_entity(&entity_type)?,
28772877
child.join_attribute,
28782878
child.derived,
2879-
child.attribute,
2879+
child.sort_by_attribute,
28802880
br_column,
28812881
ASC,
28822882
),
2883-
EntityOrderChild::Interface(children) => {
2884-
with_child_interface_key(children, ASC, table, layout)
2883+
EntityOrderByChild::Interface(child, entity_types) => {
2884+
with_child_interface_key(child, entity_types, ASC, table, layout)
28852885
}
28862886
},
28872887
EntityOrder::ChildDescending(kind) => match kind {
2888-
EntityOrderChild::Object(child) => with_child_object_key(
2888+
EntityOrderByChild::Object(child, entity_type) => with_child_object_key(
28892889
table,
2890-
layout.table_for_entity(&child.entity_type)?,
2890+
layout.table_for_entity(&entity_type)?,
28912891
child.join_attribute,
28922892
child.derived,
2893-
child.attribute,
2893+
child.sort_by_attribute,
28942894
br_column,
28952895
DESC,
28962896
),
2897-
EntityOrderChild::Interface(children) => {
2898-
with_child_interface_key(children, DESC, table, layout)
2897+
EntityOrderByChild::Interface(child, entity_types) => {
2898+
with_child_interface_key(child, entity_types, DESC, table, layout)
28992899
}
29002900
},
29012901
}

0 commit comments

Comments
 (0)