This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Draft WASM validation logic #269
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
mod selection_parser; | ||
mod spec; | ||
pub(crate) mod spec; | ||
mod url_path_template; | ||
|
||
pub use selection_parser::ApplyTo; | ||
pub use selection_parser::ApplyToError; | ||
pub use selection_parser::Selection; | ||
pub use url_path_template::URLPathTemplate; | ||
|
||
// For use with external WASM validation | ||
#[deprecated(note = "will be superseded by composition in Rust")] | ||
pub mod wasm_validators; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
mod directives; | ||
mod schema; | ||
pub(crate) mod schema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//! Validation helpers for WASM targets | ||
//! | ||
//! These utility methods are mostly used for allowing WASM contexts to validate | ||
//! connect-enabled schemas. Since this must pass the WASM FFI boundary, they can | ||
//! only operate on WASM primitives (for now) like strings. | ||
|
||
use crate::{ | ||
error::FederationError, | ||
schema::ValidFederationSchema, | ||
sources::connect::spec::schema::{ | ||
ConnectDirectiveArguments, SourceDirectiveArguments, CONNECT_DIRECTIVE_NAME_IN_SPEC, | ||
SOURCE_DIRECTIVE_NAME_IN_SPEC, | ||
}, | ||
}; | ||
|
||
#[derive(Default)] | ||
pub struct Location { | ||
pub line: usize, | ||
pub column: usize, | ||
} | ||
#[derive(Default)] | ||
pub struct LocationRange { | ||
pub start: Location, | ||
pub end: Location, | ||
} | ||
pub struct ValidationError { | ||
pub range: LocationRange, | ||
pub reason: String, | ||
pub error: FederationError, | ||
} | ||
|
||
/// Validate a subgraph schema with connect directives | ||
// Note: The error is boxed because it is large and clippy complains | ||
pub fn validate_connect_enabled_schema(subgraph_schema: &str) -> Result<(), Box<ValidationError>> { | ||
// Helper for mapping into the correct error type | ||
fn error_with_phony_loc(reason: &str, e: impl Into<FederationError>) -> Box<ValidationError> { | ||
Box::new(ValidationError { | ||
range: Default::default(), | ||
reason: reason.to_string(), | ||
error: e.into(), | ||
}) | ||
} | ||
|
||
// First we need to create an actul schema from the provided one | ||
let schema = apollo_compiler::Schema::parse(subgraph_schema, "") | ||
.map_err(|e| error_with_phony_loc("failed to parse schema", e))?; | ||
|
||
// Make sure that the parsed schema is actually valid | ||
let schema = schema | ||
.validate() | ||
.map_err(|e| error_with_phony_loc("failed to validate schema", e))?; | ||
let schema = ValidFederationSchema::new(schema) | ||
.map_err(|e| error_with_phony_loc("failed to parse as federation schema", e))?; | ||
|
||
// Process the @source directives | ||
let sources = schema | ||
.referencers() | ||
.get_directive(&SOURCE_DIRECTIVE_NAME_IN_SPEC) | ||
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. this will error if it can't find any sources — before we do anything, we should first check to see if the |
||
.map_err(|e| error_with_phony_loc("could not get @source directives from schema", e))?; | ||
|
||
// Extract the sources from the schema definition and map them to their `Source` equivalent | ||
let schema_directive_refs = sources.schema.as_ref().unwrap(); | ||
let _sources = schema_directive_refs | ||
.get(schema.schema()) | ||
.directives | ||
.iter() | ||
.filter(|directive| directive.name == SOURCE_DIRECTIVE_NAME_IN_SPEC) | ||
.map(SourceDirectiveArguments::try_from) | ||
.collect::<Result<Vec<_>, _>>() | ||
.map_err(|e| error_with_phony_loc("could not process @source in spec", e))?; | ||
|
||
// Now process the @connect directives | ||
let connects = schema | ||
.referencers() | ||
.get_directive(&CONNECT_DIRECTIVE_NAME_IN_SPEC) | ||
.map_err(|e| error_with_phony_loc("could not get @connect directives from schema", e))?; | ||
|
||
// Extract the connects from the schema definition and map them to their `Connect` equivalent | ||
// TODO: We can safely assume that a connect can only be on object fields, right? | ||
let _connects = connects | ||
.object_fields | ||
.iter() | ||
.flat_map(|field| field.get(schema.schema()).unwrap().directives.iter()) | ||
.map(ConnectDirectiveArguments::try_from) | ||
.collect::<Result<Vec<_>, _>>() | ||
.map_err(|e| error_with_phony_loc("could not process @connect in spec", e))?; | ||
|
||
Ok(()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
we can use parse_and_validate i think