Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derived fields getter #4434

Merged
merged 22 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions graph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ web3 = { git = "https://github.com/graphprotocol/rust-web3", branch = "graph-pat
serde_plain = "1.0.1"

[dev-dependencies]
test-store = { path = "../store/test-store" }
graph-store-postgres = { path = "../store/postgres" }
graph-chain-ethereum = { path = "../chain/ethereum" }
clap = { version = "3.2.23", features = ["derive", "env"] }
maplit = "1.0.2"
hex-literal = "0.3"

[build-dependencies]
tonic-build = { workspace = true }
24 changes: 24 additions & 0 deletions graph/src/components/store/entity_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::components::store::{self as s, Entity, EntityKey, EntityOp, EntityOpe
use crate::prelude::{Schema, ENV_VARS};
use crate::util::lfu_cache::LfuCache;

use super::{DerivedEntityQuery, EntityType, LoadRelatedRequest};

/// A cache for entities from the store that provides the basic functionality
/// needed for the store interactions in the host exports. This struct tracks
/// how entities are modified, and caches all entities looked up from the
Expand Down Expand Up @@ -113,6 +115,27 @@ impl EntityCache {
Ok(entity)
}

pub fn load_related(
&mut self,
eref: &LoadRelatedRequest,
) -> Result<Vec<Entity>, anyhow::Error> {
let (base_type, field) = self.schema.get_field_related(eref)?;

let query = DerivedEntityQuery {
entity_type: EntityType::new(base_type.to_string()),
entity_field: field.name.clone().into(),
value: eref.entity_id.clone(),
causality_region: eref.causality_region,
};

let entities = self.store.get_derived(&query)?;
entities.iter().for_each(|(key, e)| {
self.current.insert(key.clone(), Some(e.clone()));
});
let entities: Vec<Entity> = entities.values().cloned().collect();
Ok(entities)
}

pub fn remove(&mut self, key: EntityKey) {
self.entity_op(key, EntityOp::Remove);
}
Expand Down Expand Up @@ -151,6 +174,7 @@ impl EntityCache {
}
}

// check the validate for derived fields
let is_valid = entity.validate(&self.schema, &key).is_ok();

self.entity_op(key.clone(), EntityOp::Update(entity));
Expand Down
50 changes: 50 additions & 0 deletions graph/src/components/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,40 @@ pub struct EntityKey {
pub causality_region: CausalityRegion,
}

#[derive(Debug, Clone)]
pub struct LoadRelatedRequest {
/// Name of the entity type.
pub entity_type: EntityType,
/// ID of the individual entity.
pub entity_id: Word,
/// Field the shall be loaded
pub entity_field: Word,

/// This is the causality region of the data source that created the entity.
///
/// In the case of an entity lookup, this is the causality region of the data source that is
/// doing the lookup. So if the entity exists but was created on a different causality region,
/// the lookup will return empty.
pub causality_region: CausalityRegion,
}

#[derive(Debug)]
pub struct DerivedEntityQuery {
/// Name of the entity to search
pub entity_type: EntityType,
/// The field to check
pub entity_field: Word,
/// The value to compare against
pub value: Word,

/// This is the causality region of the data source that created the entity.
///
/// In the case of an entity lookup, this is the causality region of the data source that is
/// doing the lookup. So if the entity exists but was created on a different causality region,
/// the lookup will return empty.
pub causality_region: CausalityRegion,
}
gusinacio marked this conversation as resolved.
Show resolved Hide resolved

impl EntityKey {
// For use in tests only
#[cfg(debug_assertions)]
Expand All @@ -148,6 +182,15 @@ impl EntityKey {
causality_region: CausalityRegion::ONCHAIN,
}
}

pub fn from(id: &String, load_related_request: &LoadRelatedRequest) -> Self {
let clone = load_related_request.clone();
Self {
entity_id: id.clone().into(),
entity_type: clone.entity_type,
causality_region: clone.causality_region,
}
}
}

#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -1127,6 +1170,13 @@ impl ReadStore for EmptyStore {
Ok(BTreeMap::new())
}

fn get_derived(
&self,
_query: &DerivedEntityQuery,
) -> Result<BTreeMap<EntityKey, Entity>, StoreError> {
Ok(BTreeMap::new())
}

fn input_schema(&self) -> Arc<Schema> {
self.schema.cheap_clone()
}
Expand Down
13 changes: 13 additions & 0 deletions graph/src/components/store/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ pub trait ReadStore: Send + Sync + 'static {
keys: BTreeSet<EntityKey>,
) -> Result<BTreeMap<EntityKey, Entity>, StoreError>;

/// Reverse lookup
fn get_derived(
&self,
query_derived: &DerivedEntityQuery,
) -> Result<BTreeMap<EntityKey, Entity>, StoreError>;

fn input_schema(&self) -> Arc<Schema>;
}

Expand All @@ -202,6 +208,13 @@ impl<T: ?Sized + ReadStore> ReadStore for Arc<T> {
(**self).get_many(keys)
}

fn get_derived(
&self,
entity_derived: &DerivedEntityQuery,
) -> Result<BTreeMap<EntityKey, Entity>, StoreError> {
(**self).get_derived(entity_derived)
}

fn input_schema(&self) -> Arc<Schema> {
(**self).input_schema()
}
Expand Down
75 changes: 74 additions & 1 deletion graph/src/data/schema.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cheap_clone::CheapClone;
use crate::components::store::{EntityKey, EntityType};
use crate::components::store::{EntityKey, EntityType, LoadRelatedRequest};
use crate::data::graphql::ext::{DirectiveExt, DirectiveFinder, DocumentExt, TypeExt, ValueExt};
use crate::data::graphql::ObjectTypeExt;
use crate::data::store::{self, ValueType};
Expand Down Expand Up @@ -539,6 +539,79 @@ impl Schema {
}
}

/// Returns the field that has the relationship with the key requested
/// This works as a reverse search for the Field related to the query
///
/// example:
///
/// type Account @entity {
/// wallets: [Wallet!]! @derivedFrom(field: "account")
/// }
/// type Wallet {
/// account: Account!
/// balance: Int!
/// }
///
/// When asked to load the related entities from "Account" in the field "wallets"
/// This function will return the type "Wallet" with the field "account"
pub fn get_field_related(&self, key: &LoadRelatedRequest) -> Result<(&str, &Field), Error> {
let field = self
.document
.get_object_type_definition(key.entity_type.as_str())
.ok_or_else(|| {
anyhow!(
"Entity {}[{}]: unknown entity type `{}`",
key.entity_type,
key.entity_id,
key.entity_type,
)
})?
.field(&key.entity_field)
.ok_or_else(|| {
anyhow!(
"Entity {}[{}]: unknown field `{}`",
key.entity_type,
key.entity_id,
key.entity_field,
)
})?;
if field.is_derived() {
let derived_from = field.find_directive("derivedFrom").unwrap();
let base_type = field.field_type.get_base_type();
let field_name = derived_from.argument("field").unwrap();

let field = self
.document
.get_object_type_definition(base_type)
.ok_or_else(|| {
anyhow!(
"Entity {}[{}]: unknown entity type `{}`",
key.entity_type,
key.entity_id,
key.entity_type,
)
})?
.field(field_name.as_str().unwrap())
.ok_or_else(|| {
anyhow!(
"Entity {}[{}]: unknown field `{}`",
key.entity_type,
key.entity_id,
key.entity_field,
)
})?;

Ok((base_type, field))
} else {
Err(anyhow!(
"Entity {}[{}]: field `{}` is not derived",
key.entity_type,
key.entity_id,
key.entity_field,
))
}
}
gusinacio marked this conversation as resolved.
Show resolved Hide resolved

pub fn is_immutable(&self, entity_type: &EntityType) -> bool {
self.immutable_types.contains(entity_type)
}
Expand Down
10 changes: 9 additions & 1 deletion graph/src/runtime/gas/size_of.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Various implementations of GasSizeOf;

use crate::{
components::store::{EntityKey, EntityType},
components::store::{EntityKey, EntityType, LoadRelatedRequest},
data::store::{scalar::Bytes, Value},
prelude::{BigDecimal, BigInt},
};
Expand Down Expand Up @@ -168,6 +168,14 @@ impl GasSizeOf for EntityKey {
}
}

impl GasSizeOf for LoadRelatedRequest {
fn gas_size_of(&self) -> Gas {
self.entity_type.gas_size_of()
+ self.entity_id.gas_size_of()
+ self.entity_field.gas_size_of()
}
}

impl GasSizeOf for EntityType {
fn gas_size_of(&self) -> Gas {
self.as_str().gas_size_of()
Expand Down
1 change: 1 addition & 0 deletions graph/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ pub enum IndexForAscTypeId {
Log = 1001,
ArrayH256 = 1002,
ArrayLog = 1003,
ArrayTypedMapStringStoreValue = 1004,
// Continue to add more Ethereum type IDs here.
// e.g.:
// NextEthereumType = 1004,
Expand Down
Loading