-
Notifications
You must be signed in to change notification settings - Fork 982
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
Add new API Version to validate when setting fields not defined in the schema #4894
Changes from 6 commits
02c4328
ce27a4d
648dedd
7e132f0
af14d42
2289fea
d25c639
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -379,6 +379,20 @@ impl InputSchema { | |
.map(|fields| fields.contains(&field)) | ||
.unwrap_or(false) | ||
} | ||
|
||
pub fn has_field_with_name(&self, entity_type: &EntityType, field: &str) -> bool { | ||
let field = self.inner.pool.lookup(field); | ||
|
||
match field { | ||
Some(field) => self | ||
.inner | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can just call |
||
.field_names | ||
.get(entity_type) | ||
.map(|fields| fields.contains(&field)) | ||
.unwrap_or(false), | ||
None => false, | ||
} | ||
} | ||
} | ||
|
||
/// Create a new pool that contains the names of all the types defined | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ use std::ops::Deref; | |
use std::str::FromStr; | ||
use std::time::{Duration, Instant}; | ||
|
||
use graph::data::subgraph::API_VERSION_0_0_8; | ||
use graph::data::value::Word; | ||
|
||
use never::Never; | ||
|
@@ -199,9 +200,57 @@ impl<C: Blockchain> HostExports<C> { | |
} | ||
} | ||
|
||
// From apiVersion 0.0.8 onwards, we check that the entity data | ||
// does not contain fields that are not in the schema and fail | ||
if self.api_version >= API_VERSION_0_0_8 { | ||
// Quick check for any invalid field | ||
let has_invalid_fields = data.iter().any(|(field_name, _)| { | ||
!state | ||
.entity_cache | ||
.schema | ||
.has_field_with_name(&key.entity_type, &field_name) | ||
}); | ||
|
||
// If an invalid field exists, find all and return an error | ||
if has_invalid_fields { | ||
let invalid_fields: Vec<Word> = data | ||
.iter() | ||
.filter_map(|(field_name, _)| { | ||
if !state | ||
.entity_cache | ||
.schema | ||
.has_field_with_name(&key.entity_type, &field_name) | ||
{ | ||
Some(field_name.clone()) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect(); | ||
|
||
return Err(HostExportError::Deterministic(anyhow!( | ||
"Attempted to set undefined fields [{}] for the entity type `{}`. Make sure those fields are defined in the schema.", | ||
invalid_fields | ||
.iter() | ||
.map(|f| f.as_str()) | ||
.collect::<Vec<_>>() | ||
.join(", "), | ||
key.entity_type | ||
))); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could the block starting at line 203 be put into a |
||
|
||
// Filter out fields that are not in the schema | ||
let filtered_entity_data = data.into_iter().filter(|(field_name, _)| { | ||
state | ||
.entity_cache | ||
.schema | ||
.has_field_with_name(&key.entity_type, field_name) | ||
}); | ||
|
||
let entity = state | ||
.entity_cache | ||
.make_entity(data.into_iter().map(|(key, value)| (key, value))) | ||
.make_entity(filtered_entity_data.map(|(key, value)| (key, value))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That |
||
.map_err(|e| HostExportError::Deterministic(anyhow!(e)))?; | ||
|
||
let poi_section = stopwatch.start_section("host_export_store_set__proof_of_indexing"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[ | ||
{ | ||
"anonymous": false, | ||
"inputs": [ | ||
{ | ||
"indexed": false, | ||
"internalType": "string", | ||
"name": "testCommand", | ||
"type": "string" | ||
} | ||
], | ||
"name": "TestEvent", | ||
"type": "event" | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"apiVersion": "0.0.7" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"apiVersion": "0.0.8" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "api-version", | ||
"version": "0.1.0", | ||
"scripts": { | ||
"build-contracts": "../../common/build-contracts.sh", | ||
"codegen": "graph codegen --skip-migrations", | ||
"test": "yarn build-contracts && truffle test --compile-none --network test", | ||
"create:test": "graph create test/api-version --node $GRAPH_NODE_ADMIN_URI", | ||
"prepare:0-0-7": "mustache data.0.0.7.json subgraph.template.yaml > subgraph.yaml", | ||
"prepare:0-0-8": "mustache data.0.0.8.json subgraph.template.yaml > subgraph.yaml", | ||
"deploy:test-0-0-7": "yarn prepare:0-0-7 && graph deploy test/api-version-0-0-7 --version-label 0.0.7 --ipfs $IPFS_URI --node $GRAPH_NODE_ADMIN_URI", | ||
"deploy:test-0-0-8": "yarn prepare:0-0-8 && graph deploy test/api-version-0-0-8 --version-label 0.0.8 --ipfs $IPFS_URI --node $GRAPH_NODE_ADMIN_URI" | ||
}, | ||
"devDependencies": { | ||
"@graphprotocol/graph-cli": "0.53.0", | ||
"@graphprotocol/graph-ts": "0.31.0", | ||
"solc": "^0.8.2" | ||
}, | ||
"dependencies": { | ||
"@truffle/contract": "^4.3", | ||
"@truffle/hdwallet-provider": "^1.2", | ||
"apollo-fetch": "^0.7.0", | ||
"babel-polyfill": "^6.26.0", | ||
"babel-register": "^6.26.0", | ||
"gluegun": "^4.6.1", | ||
"mustache": "^4.2.0", | ||
"truffle": "^5.2" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
type TestResult @entity { | ||
id: ID! | ||
message: String! | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Entity, Value, store } from "@graphprotocol/graph-ts"; | ||
import { TestEvent } from "../generated/Contract/Contract"; | ||
import { TestResult } from "../generated/schema"; | ||
|
||
export function handleTestEvent(event: TestEvent): void { | ||
let testResult = new TestResult(event.params.testCommand); | ||
testResult.message = event.params.testCommand; | ||
let testResultEntity = testResult as Entity; | ||
testResultEntity.set( | ||
"invalid_field", | ||
Value.fromString("This is an invalid field"), | ||
); | ||
store.set("TestResult", testResult.id, testResult); | ||
testResult.save(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
specVersion: 0.0.4 | ||
schema: | ||
file: ./schema.graphql | ||
dataSources: | ||
- kind: ethereum/contract | ||
name: Contract | ||
network: test | ||
source: | ||
address: "0x0000000000000000000000000000000000000000" | ||
abi: Contract | ||
mapping: | ||
kind: ethereum/events | ||
apiVersion: {{apiVersion}} | ||
language: wasm/assemblyscript | ||
abis: | ||
- name: Contract | ||
file: ./abis/Contract.abi | ||
entities: | ||
- Call | ||
eventHandlers: | ||
- event: TestEvent(string) | ||
handler: handleTestEvent | ||
file: ./src/mapping.ts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
small typo: s/doesnt/don't/