Skip to content

Commit a8a21a3

Browse files
committed
core, graph : Refactor LinkResolver trait
1 parent 8f706cf commit a8a21a3

File tree

9 files changed

+33
-37
lines changed

9 files changed

+33
-37
lines changed

chain/substreams/src/data_source.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ mod test {
331331
blockchain::{DataSource as _, UnresolvedDataSource as _},
332332
components::link_resolver::LinkResolver,
333333
data::subgraph::LATEST_VERSION,
334-
prelude::{async_trait, serde_yaml, DeploymentHash, JsonValueStream, Link},
334+
prelude::{async_trait, serde_yaml, JsonValueStream, Link},
335335
slog::{o, Discard, Logger},
336336
substreams::{
337337
module::{
@@ -705,10 +705,7 @@ mod test {
705705
unimplemented!()
706706
}
707707

708-
fn for_deployment(
709-
&self,
710-
_deployment: DeploymentHash,
711-
) -> Result<Box<dyn LinkResolver>, Error> {
708+
fn for_manifest(&self, _manifest_path: &str) -> Result<Box<dyn LinkResolver>, Error> {
712709
unimplemented!()
713710
}
714711

core/src/subgraph/instance_manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl<S: SubgraphStore> SubgraphInstanceManager<S> {
289289
// Allow for infinite retries for subgraph definition files.
290290
let link_resolver = Arc::from(
291291
self.link_resolver
292-
.for_deployment(deployment.hash.clone())
292+
.for_manifest(&deployment.hash.to_string())
293293
.map_err(SubgraphRegistrarError::Unknown)?
294294
.with_retries(),
295295
);

core/src/subgraph/provider.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<I: SubgraphInstanceManager> SubgraphAssignmentProviderTrait for SubgraphAss
8888

8989
let link_resolver = self
9090
.link_resolver
91-
.for_deployment(loc.hash.clone())
91+
.for_manifest(&loc.hash.to_string())
9292
.map_err(SubgraphAssignmentProviderError::ResolveError)?;
9393

9494
let file_bytes = link_resolver

core/src/subgraph/registrar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ where
289289

290290
let resolver: Arc<dyn LinkResolver> = Arc::from(
291291
self.resolver
292-
.for_deployment(hash.clone())
292+
.for_manifest(&hash.to_string())
293293
.map_err(SubgraphRegistrarError::Unknown)?,
294294
);
295295

graph/src/components/link_resolver/file.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use async_trait::async_trait;
77
use slog::Logger;
88

99
use crate::data::subgraph::Link;
10-
use crate::prelude::{DeploymentHash, Error, JsonValueStream, LinkResolver as LinkResolverTrait};
10+
use crate::prelude::{Error, JsonValueStream, LinkResolver as LinkResolverTrait};
1111

1212
#[derive(Clone, Debug)]
1313
pub struct FileLinkResolver {
@@ -72,20 +72,19 @@ impl FileLinkResolver {
7272
/// It will set the base directory to the parent directory of the manifest path
7373
/// This is required because paths mentioned in the subgraph manifest are relative paths
7474
/// and we need a new resolver with the right base directory for the specific subgraph
75-
fn clone_for_deployment(&self, deployment: DeploymentHash) -> Result<Self, Error> {
75+
fn clone_for_manifest(&self, manifest_path_str: &str) -> Result<Self, Error> {
7676
let mut resolver = self.clone();
7777

78-
let deployment_str = deployment.to_string();
79-
8078
// Create a path to the manifest based on the current resolver's
8179
// base directory or default to using the deployment string as path
8280
// If the deployment string is an alias, use the aliased path
83-
let manifest_path = if let Some(aliased) = self.aliases.get(&deployment_str) {
81+
let manifest_path = if let Some(aliased) = self.aliases.get(&manifest_path_str.to_string())
82+
{
8483
aliased.clone()
8584
} else {
8685
match &resolver.base_dir {
87-
Some(dir) => dir.join(&deployment_str),
88-
None => PathBuf::from(deployment_str),
86+
Some(dir) => dir.join(&manifest_path_str),
87+
None => PathBuf::from(manifest_path_str),
8988
}
9089
};
9190

@@ -143,11 +142,8 @@ impl LinkResolverTrait for FileLinkResolver {
143142
}
144143
}
145144

146-
fn for_deployment(
147-
&self,
148-
deployment: DeploymentHash,
149-
) -> Result<Box<dyn LinkResolverTrait>, Error> {
150-
Ok(Box::new(self.clone_for_deployment(deployment)?))
145+
fn for_manifest(&self, manifest_path: &str) -> Result<Box<dyn LinkResolverTrait>, Error> {
146+
Ok(Box::new(self.clone_for_manifest(manifest_path)?))
151147
}
152148

153149
async fn get_block(&self, _logger: &Logger, _link: &Link) -> Result<Vec<u8>, Error> {
@@ -325,8 +321,7 @@ mod tests {
325321
assert_eq!(result2, test_content2);
326322

327323
// Test that the alias works in for_deployment as well
328-
let deployment = DeploymentHash::new("deployment-id").unwrap();
329-
let deployment_resolver = resolver.clone_for_deployment(deployment).unwrap();
324+
let deployment_resolver = resolver.clone_for_manifest("deployment-id").unwrap();
330325

331326
let expected_dir = test_file1_path.parent().unwrap();
332327
let deployment_base_dir = deployment_resolver.base_dir.clone().unwrap();

graph/src/components/link_resolver/ipfs.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ impl LinkResolverTrait for IpfsResolver {
7474
Box::new(s)
7575
}
7676

77-
fn for_deployment(
78-
&self,
79-
_deployment: DeploymentHash,
80-
) -> Result<Box<dyn LinkResolverTrait>, Error> {
77+
fn for_manifest(&self, _manifest_path: &str) -> Result<Box<dyn LinkResolverTrait>, Error> {
8178
Ok(Box::new(self.cheap_clone()))
8279
}
8380

graph/src/components/link_resolver/mod.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::time::Duration;
33
use slog::Logger;
44

55
use crate::data::subgraph::Link;
6-
use crate::prelude::{DeploymentHash, Error};
6+
use crate::prelude::Error;
77
use std::fmt::Debug;
88

99
mod arweave;
@@ -30,12 +30,19 @@ pub trait LinkResolver: Send + Sync + 'static + Debug {
3030
/// Fetches the IPLD block contents as bytes.
3131
async fn get_block(&self, logger: &Logger, link: &Link) -> Result<Vec<u8>, Error>;
3232

33-
/// Creates a new resolver that is scoped to a specific subgraph
34-
/// This is used by FileLinkResolver to create a new resolver for a specific subgraph
35-
/// For other resolvers, this method will simply return the current resolver
36-
/// This is required because paths mentioned in the subgraph manifest are relative paths
37-
/// and we need a new resolver with the right base directory for the specific subgraph
38-
fn for_deployment(&self, deployment: DeploymentHash) -> Result<Box<dyn LinkResolver>, Error>;
33+
/// Creates a new resolver scoped to a specific subgraph manifest.
34+
///
35+
/// For FileLinkResolver, this sets the base directory to the manifest's parent directory.
36+
/// Note the manifest here is the manifest in the build directory, not the manifest in the source directory
37+
/// to properly resolve relative paths referenced in the manifest (schema, mappings, etc.).
38+
/// For other resolvers (IPFS/Arweave), this simply returns a clone since they use
39+
/// absolute content identifiers.
40+
///
41+
/// The `manifest_path` parameter can be a filesystem path or an alias. Aliases are used
42+
/// in development environments (via `gnd --sources`) to map user-defined
43+
/// aliases to actual subgraph paths, enabling local development with file-based
44+
/// subgraphs that reference each other.
45+
fn for_manifest(&self, manifest_path: &str) -> Result<Box<dyn LinkResolver>, Error>;
3946

4047
/// Read the contents of `link` and deserialize them into a stream of JSON
4148
/// values. The values must each be on a single line; newlines are significant

graph/src/data_source/subgraph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl UnresolvedDataSource {
260260
logger: &Logger,
261261
) -> Result<Arc<SubgraphManifest<C>>, Error> {
262262
let resolver: Arc<dyn LinkResolver> =
263-
Arc::from(resolver.for_deployment(self.source.address.clone())?);
263+
Arc::from(resolver.for_manifest(&self.source.address.to_string())?);
264264
let source_raw = resolver
265265
.cat(logger, &self.source.address.to_ipfs_link())
266266
.await
@@ -284,7 +284,7 @@ impl UnresolvedDataSource {
284284
))?;
285285

286286
let resolver: Arc<dyn LinkResolver> =
287-
Arc::from(resolver.for_deployment(self.source.address.clone())?);
287+
Arc::from(resolver.for_manifest(&self.source.address.to_string())?);
288288
source_manifest
289289
.resolve(&resolver, logger, LATEST_VERSION.clone())
290290
.await

store/test-store/tests/chain/ethereum/manifest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ impl LinkResolverTrait for TextResolver {
9191
Box::new(self.clone())
9292
}
9393

94-
fn for_deployment(
94+
fn for_manifest(
9595
&self,
96-
_deployment: DeploymentHash,
96+
_manifest_path: &str,
9797
) -> Result<Box<dyn LinkResolverTrait>, anyhow::Error> {
9898
Ok(Box::new(self.clone()))
9999
}

0 commit comments

Comments
 (0)