Skip to content

Commit 7707967

Browse files
authored
Remove typename from POI (#3739)
* tests: Fix typo in Runner::data_source_revert * tests: Add missing newline at end of package.json * tests: Add __typename scenario to Runner The test scenario added here shows a bug in the EntityCache where the __typename field wouldn't be cleared when it should. This was affecting the POI and mapping code determinism, it will be fixed in the next commit :) * store: Remove __typename from Entities after merge After an entity merge, if the Entity Cache was cleared, the __typename field would come up again and polute mappings and by consequence, the POI. This commit fixes the issue/bug alongside with it's respective test. * tests: Format Cargo.toml * tests: Remove leftover data-source-context in package.json * tests: Add --mutex file to yarn install for Runner tests * store: Remove extra __typename from unit test * store: Move typename logic omission to FromEntityData * tests: Add Mutex to `Stores` to fix test flakiness * tests: Move Mutex from `Stores` struct to `stores` fn * store: Remove __typename from unit tests
1 parent 62ccd69 commit 7707967

File tree

15 files changed

+203
-19
lines changed

15 files changed

+203
-19
lines changed

store/postgres/src/relational.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ impl Layout {
483483
FindQuery::new(table.as_ref(), id, block)
484484
.get_result::<EntityData>(conn)
485485
.optional()?
486-
.map(|entity_data| entity_data.deserialize_with_layout(self, None))
486+
.map(|entity_data| entity_data.deserialize_with_layout(self, None, true))
487487
.transpose()
488488
}
489489

@@ -509,10 +509,13 @@ impl Layout {
509509
};
510510
let mut entities_for_type: BTreeMap<EntityType, Vec<Entity>> = BTreeMap::new();
511511
for data in query.load::<EntityData>(conn)? {
512+
let entity_type = data.entity_type();
513+
let entity_data: Entity = data.deserialize_with_layout(self, None, true)?;
514+
512515
entities_for_type
513-
.entry(data.entity_type())
516+
.entry(entity_type)
514517
.or_default()
515-
.push(data.deserialize_with_layout(self, None)?);
518+
.push(entity_data);
516519
}
517520
Ok(entities_for_type)
518521
}
@@ -541,7 +544,7 @@ impl Layout {
541544

542545
for entity_data in inserts_or_updates.into_iter() {
543546
let entity_type = entity_data.entity_type();
544-
let mut data: Entity = entity_data.deserialize_with_layout(self, None)?;
547+
let mut data: Entity = entity_data.deserialize_with_layout(self, None, false)?;
545548
let entity_id = data.id().expect("Invalid ID for entity.");
546549
processed_entities.insert((entity_type.clone(), entity_id.clone()));
547550

@@ -709,7 +712,7 @@ impl Layout {
709712
.into_iter()
710713
.map(|entity_data| {
711714
entity_data
712-
.deserialize_with_layout(self, parent_type.as_ref())
715+
.deserialize_with_layout(self, parent_type.as_ref(), false)
713716
.map_err(|e| e.into())
714717
})
715718
.collect()

store/postgres/src/relational_queries.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl ForeignKeyClauses for Column {
233233
}
234234
}
235235

236-
pub trait FromEntityData: std::fmt::Debug {
236+
pub trait FromEntityData: std::fmt::Debug + std::default::Default {
237237
type Value: FromColumnValue;
238238

239239
fn new_entity(typename: String) -> Self;
@@ -479,14 +479,19 @@ impl EntityData {
479479
self,
480480
layout: &Layout,
481481
parent_type: Option<&ColumnType>,
482+
remove_typename: bool,
482483
) -> Result<T, StoreError> {
483484
let entity_type = EntityType::new(self.entity);
484485
let table = layout.table_for_entity(&entity_type)?;
485486

486487
use serde_json::Value as j;
487488
match self.data {
488489
j::Object(map) => {
489-
let mut out = T::new_entity(entity_type.into_string());
490+
let mut out = if !remove_typename {
491+
T::new_entity(entity_type.into_string())
492+
} else {
493+
T::default()
494+
};
490495
for (key, json) in map {
491496
// Simply ignore keys that do not have an underlying table
492497
// column; those will be things like the block_range that

store/postgres/tests/relational.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,11 @@ lazy_static! {
182182
bigInt: big_int.clone(),
183183
bigIntArray: vec![big_int.clone(), (big_int + 1.into()).clone()],
184184
color: "yellow",
185-
__typename: "Scalar",
186185
}
187186
};
188187
static ref EMPTY_NULLABLESTRINGS_ENTITY: Entity = {
189188
entity! {
190189
id: "one",
191-
__typename: "NullableStrings"
192190
}
193191
};
194192
static ref SCALAR: EntityType = EntityType::from("Scalar");

store/postgres/tests/relational_bytes.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ lazy_static! {
6767
static ref BEEF_ENTITY: Entity = entity! {
6868
id: scalar::Bytes::from_str("deadbeef").unwrap(),
6969
name: "Beef",
70-
__typename: "Thing"
7170
};
7271
static ref NAMESPACE: Namespace = Namespace::new("sgd0815".to_string()).unwrap();
7372
static ref THING: EntityType = EntityType::from("Thing");

store/postgres/tests/store.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ fn get_entity_1() {
349349

350350
let mut expected_entity = Entity::new();
351351

352-
expected_entity.insert("__typename".to_owned(), USER.into());
353352
expected_entity.insert("id".to_owned(), "1".into());
354353
expected_entity.insert("name".to_owned(), "Johnton".into());
355354
expected_entity.insert(
@@ -380,7 +379,6 @@ fn get_entity_3() {
380379

381380
let mut expected_entity = Entity::new();
382381

383-
expected_entity.insert("__typename".to_owned(), USER.into());
384382
expected_entity.insert("id".to_owned(), "3".into());
385383
expected_entity.insert("name".to_owned(), "Shaqueeena".into());
386384
expected_entity.insert(
@@ -473,7 +471,6 @@ fn update_existing() {
473471
_ => unreachable!(),
474472
};
475473

476-
new_data.insert("__typename".to_owned(), USER.into());
477474
new_data.insert("bin_name".to_owned(), Value::Bytes(bin_name));
478475
assert_eq!(writable.get(&entity_key).unwrap(), Some(new_data));
479476
})

tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ port_check = "0.1.5"
88
anyhow = "1.0"
99
futures = { version = "0.3", features = ["compat"] }
1010
graph = { path = "../graph" }
11-
tokio = {version = "1.16.1", features = ["rt", "macros", "process"]}
11+
tokio = { version = "1.16.1", features = ["rt", "macros", "process"] }
1212
graph-chain-ethereum = { path = "../chain/ethereum" }
1313
async-stream = "0.3.3"
1414
graph-node = { path = "../node" }

tests/integration-tests/data-source-revert/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
"@graphprotocol/graph-cli": "https://github.com/graphprotocol/graph-cli#main",
1111
"@graphprotocol/graph-ts": "https://github.com/graphprotocol/graph-ts#main"
1212
}
13-
}
13+
}

tests/integration-tests/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"private": true,
33
"workspaces": [
44
"api-version-v0-0-4",
5-
"data-source-context",
65
"data-source-revert",
76
"fatal-error",
87
"ganache-reverts",
@@ -11,6 +10,7 @@
1110
"overloaded-contract-functions",
1211
"poi-for-failed-subgraph",
1312
"remove-then-update",
13+
"typename",
1414
"value-roundtrip"
1515
]
1616
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[
2+
{
3+
"inputs": [],
4+
"stateMutability": "nonpayable",
5+
"type": "constructor"
6+
},
7+
{
8+
"anonymous": false,
9+
"inputs": [
10+
{
11+
"indexed": false,
12+
"internalType": "uint16",
13+
"name": "x",
14+
"type": "uint16"
15+
}
16+
],
17+
"name": "Trigger",
18+
"type": "event"
19+
},
20+
{
21+
"inputs": [
22+
{
23+
"internalType": "uint16",
24+
"name": "x",
25+
"type": "uint16"
26+
}
27+
],
28+
"name": "emitTrigger",
29+
"outputs": [],
30+
"stateMutability": "nonpayable",
31+
"type": "function"
32+
}
33+
]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "typename",
3+
"version": "0.1.0",
4+
"scripts": {
5+
"codegen": "graph codegen",
6+
"create:test": "graph create test/typename --node $GRAPH_NODE_ADMIN_URI",
7+
"deploy:test": "graph deploy test/typename --version-label v0.0.1 --ipfs $IPFS_URI --node $GRAPH_NODE_ADMIN_URI"
8+
},
9+
"devDependencies": {
10+
"@graphprotocol/graph-cli": "https://github.com/graphprotocol/graph-cli#main",
11+
"@graphprotocol/graph-ts": "https://github.com/graphprotocol/graph-ts#main"
12+
}
13+
}

0 commit comments

Comments
 (0)