Skip to content

Commit a702b32

Browse files
committed
graph: do not allow mutable entities in entity handlers of composed subgraphs
1 parent f898def commit a702b32

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

graph/src/data_source/subgraph.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,16 @@ impl UnresolvedDataSource {
239239
None => {
240240
return Err(anyhow!("Entity {} not found in source manifest", entity));
241241
}
242-
Some(TypeKind::Object) => {}
242+
Some(TypeKind::Object) => {
243+
// Check if the entity is immutable
244+
let entity_type = source_manifest.schema.entity_type(entity)?;
245+
if !entity_type.is_immutable() {
246+
return Err(anyhow!(
247+
"Entity {} is not immutable and cannot be used as a mapping entity",
248+
entity
249+
));
250+
}
251+
}
243252
}
244253
}
245254
Ok(())

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const SOURCE_SUBGRAPH_SCHEMA: &str = "
5050
type TestEntity @entity { id: ID! }
5151
type User @entity { id: ID! }
5252
type Profile @entity { id: ID! }
53+
type ImmutableUser @entity(immutable: true) { id: ID!, name: String! }
5354
5455
type TokenData @entity(timeseries: true) {
5556
id: Int8!
@@ -1857,3 +1858,72 @@ specVersion: 1.3.0
18571858
}
18581859
})
18591860
}
1861+
1862+
#[tokio::test]
1863+
async fn subgraph_ds_manifest_mutable_entities_should_fail() {
1864+
let yaml = "
1865+
schema:
1866+
file:
1867+
/: /ipfs/Qmschema
1868+
dataSources:
1869+
- name: SubgraphSource
1870+
kind: subgraph
1871+
entities:
1872+
- Gravatar
1873+
network: mainnet
1874+
source:
1875+
address: 'QmSource'
1876+
startBlock: 9562480
1877+
mapping:
1878+
apiVersion: 0.0.6
1879+
language: wasm/assemblyscript
1880+
entities:
1881+
- TestEntity
1882+
file:
1883+
/: /ipfs/Qmmapping
1884+
handlers:
1885+
- handler: handleEntity
1886+
entity: User # This is a mutable entity and should fail
1887+
specVersion: 1.3.0
1888+
";
1889+
1890+
let result = try_resolve_manifest(yaml, SPEC_VERSION_1_3_0).await;
1891+
assert!(result.is_err());
1892+
let err = result.unwrap_err();
1893+
assert!(err
1894+
.to_string()
1895+
.contains("Entity User is not immutable and cannot be used as a mapping entity"));
1896+
}
1897+
1898+
#[tokio::test]
1899+
async fn subgraph_ds_manifest_immutable_entities_should_succeed() {
1900+
let yaml = "
1901+
schema:
1902+
file:
1903+
/: /ipfs/Qmschema
1904+
dataSources:
1905+
- name: SubgraphSource
1906+
kind: subgraph
1907+
entities:
1908+
- Gravatar
1909+
network: mainnet
1910+
source:
1911+
address: 'QmSource'
1912+
startBlock: 9562480
1913+
mapping:
1914+
apiVersion: 0.0.6
1915+
language: wasm/assemblyscript
1916+
entities:
1917+
- TestEntity
1918+
file:
1919+
/: /ipfs/Qmmapping
1920+
handlers:
1921+
- handler: handleEntity
1922+
entity: ImmutableUser # This is an immutable entity and should succeed
1923+
specVersion: 1.3.0
1924+
";
1925+
1926+
let result = try_resolve_manifest(yaml, SPEC_VERSION_1_3_0).await;
1927+
1928+
assert!(result.is_ok());
1929+
}

0 commit comments

Comments
 (0)