Skip to content

Commit 846eed5

Browse files
gusinaciolutter
authored andcommitted
graph,runtime,store: fix requested changes
1 parent 7e46209 commit 846eed5

File tree

7 files changed

+56
-20
lines changed

7 files changed

+56
-20
lines changed

graph/src/components/store/entity_cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ impl EntityCache {
119119
&mut self,
120120
eref: &LoadRelatedRequest,
121121
) -> Result<Vec<Entity>, anyhow::Error> {
122-
let (base_type, field) = self.schema.get_type_for_field(eref)?;
122+
let (base_type, field) = self.schema.get_field_related(eref)?;
123123

124124
let query = DerivedEntityQuery {
125125
entity_type: EntityType::new(base_type.to_string()),
126-
entity_field: field.into(),
126+
entity_field: field.name.clone().into(),
127127
value: eref.entity_id.clone(),
128128
causality_region: eref.causality_region,
129129
};

graph/src/data/schema.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,22 @@ impl Schema {
539539
}
540540
}
541541

542-
pub fn get_type_for_field(&self, key: &LoadRelatedRequest) -> Result<(&str, &str), Error> {
542+
/// Returns the field that has the relationship with the key requested
543+
/// This works as a reverse search for the Field related to the query
544+
///
545+
/// example:
546+
///
547+
/// type Account @entity {
548+
/// wallets: [Wallet!]! @derivedFrom("account")
549+
/// }
550+
/// type Wallet {
551+
/// account: Account!
552+
/// balance: Int!
553+
/// }
554+
///
555+
/// When asked to load the related entities from "Account" in the field "wallets"
556+
/// This function will return the type "Wallet" with the field "account"
557+
pub fn get_field_related(&self, key: &LoadRelatedRequest) -> Result<(&str, &Field), Error> {
543558
let field = self
544559
.document
545560
.get_object_type_definition(key.entity_type.as_str())
@@ -561,11 +576,32 @@ impl Schema {
561576
)
562577
})?;
563578
if field.is_derived() {
564-
let derived_from = field.find_directive("derivedFrom").unwrap();
579+
let derived_from = field.find_directive("derivedfrom").unwrap();
565580
let base_type = field.field_type.get_base_type();
566-
let field = derived_from.argument("field").unwrap();
581+
let field_name = derived_from.argument("field").unwrap();
582+
583+
let field = self
584+
.document
585+
.get_object_type_definition(base_type)
586+
.ok_or_else(|| {
587+
anyhow!(
588+
"Entity {}[{}]: unknown entity type `{}`",
589+
key.entity_type,
590+
key.entity_id,
591+
key.entity_type,
592+
)
593+
})?
594+
.field(field_name.as_str().unwrap())
595+
.ok_or_else(|| {
596+
anyhow!(
597+
"Entity {}[{}]: unknown field `{}`",
598+
key.entity_type,
599+
key.entity_id,
600+
key.entity_field,
601+
)
602+
})?;
567603

568-
Ok((base_type, field.as_str().unwrap()))
604+
Ok((base_type, field))
569605
} else {
570606
Err(anyhow!(
571607
"Entity {}[{}]: field `{}` is not derived",

runtime/wasm/src/module/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ impl<C: Blockchain> WasmInstance<C> {
530530
link!(
531531
"store.loadRelated",
532532
store_load_related,
533-
"host_export_store_get_derived",
533+
"host_export_store_load_related",
534534
entity,
535535
id,
536536
field
@@ -1086,10 +1086,8 @@ impl<C: Blockchain> WasmInstanceContext<C> {
10861086
gas,
10871087
)?;
10881088

1089-
let entities: Vec<Vec<(String, Value)>> = entities
1090-
.iter()
1091-
.map(|entity| entity.clone().sorted())
1092-
.collect();
1089+
let entities: Vec<Vec<(String, Value)>> =
1090+
entities.into_iter().map(|entity| entity.sorted()).collect();
10931091
let ret = asc_new(self, &entities, gas)?;
10941092
Ok(ret)
10951093
}

store/postgres/src/deployment_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ impl DeploymentStore {
11241124
site: Arc<Site>,
11251125
derived_query: &DerivedEntityQuery,
11261126
block: BlockNumber,
1127-
excluded_keys: &Option<Vec<EntityKey>>,
1127+
excluded_keys: &Vec<EntityKey>,
11281128
) -> Result<BTreeMap<EntityKey, Entity>, StoreError> {
11291129
let conn = self.get_conn()?;
11301130
let layout = self.layout(&conn, site)?;

store/postgres/src/relational.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ impl Layout {
563563
conn: &PgConnection,
564564
derived_query: &DerivedEntityQuery,
565565
block: BlockNumber,
566-
excluded_keys: &Option<Vec<EntityKey>>,
566+
excluded_keys: &Vec<EntityKey>,
567567
) -> Result<BTreeMap<EntityKey, Entity>, StoreError> {
568568
let table = self.table_for_entity(&derived_query.entity_type)?;
569569
let query = FindDerivedQuery::new(table, derived_query, block, excluded_keys);

store/postgres/src/relational_queries.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,7 +1675,7 @@ pub struct FindDerivedQuery<'a> {
16751675
table: &'a Table,
16761676
derived_query: &'a DerivedEntityQuery,
16771677
block: BlockNumber,
1678-
excluded_keys: &'a Option<Vec<EntityKey>>,
1678+
excluded_keys: &'a Vec<EntityKey>,
16791679
}
16801680

16811681
impl<'a> QueryFragment<Pg> for FindDerivedQuery<'a> {
@@ -1699,11 +1699,11 @@ impl<'a> QueryFragment<Pg> for FindDerivedQuery<'a> {
16991699
out.push_sql(self.table.qualified_name.as_str());
17001700
out.push_sql(" e\n where ");
17011701

1702-
if let Some(keys) = self.excluded_keys.as_ref().filter(|keys| keys.len() > 0) {
1702+
if self.excluded_keys.len() > 0 {
17031703
let primary_key = self.table.primary_key();
17041704
out.push_identifier(primary_key.name.as_str())?;
17051705
out.push_sql(" not in (");
1706-
for (i, value) in keys.iter().enumerate() {
1706+
for (i, value) in self.excluded_keys.iter().enumerate() {
17071707
if i > 0 {
17081708
out.push_sql(", ");
17091709
}

store/postgres/src/writable.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl SyncStore {
254254
&self,
255255
key: &DerivedEntityQuery,
256256
block: BlockNumber,
257-
excluded_keys: Option<Vec<EntityKey>>,
257+
excluded_keys: Vec<EntityKey>,
258258
) -> Result<BTreeMap<EntityKey, Entity>, StoreError> {
259259
self.retry("get_derived", || {
260260
self.writable
@@ -776,7 +776,9 @@ impl Queue {
776776
for emod in mods {
777777
let key = emod.entity_ref();
778778
// we select just the entities that match the query
779-
if derived_query.entity_type == key.entity_type {
779+
if derived_query.entity_type == key.entity_type
780+
&& derived_query.value == key.entity_id
781+
{
780782
map.insert(key.clone(), emod.entity().cloned());
781783
}
782784
}
@@ -793,7 +795,7 @@ impl Queue {
793795
// We filter to exclude the entities ids that we already have from the queue
794796
let mut items_from_database =
795797
self.store
796-
.get_derived(derived_query, tracker.query_block(), Some(excluded_keys))?;
798+
.get_derived(derived_query, tracker.query_block(), excluded_keys)?;
797799

798800
// Extend the store results with the entities from the queue.
799801
// This overwrites any entitiy from the database with the same key from queue
@@ -969,7 +971,7 @@ impl Writer {
969971
key: &DerivedEntityQuery,
970972
) -> Result<BTreeMap<EntityKey, Entity>, StoreError> {
971973
match self {
972-
Writer::Sync(store) => store.get_derived(key, BLOCK_NUMBER_MAX, None),
974+
Writer::Sync(store) => store.get_derived(key, BLOCK_NUMBER_MAX, vec![]),
973975
Writer::Async(queue) => queue.get_derived(key),
974976
}
975977
}

0 commit comments

Comments
 (0)