-
Notifications
You must be signed in to change notification settings - Fork 23
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
feat(router-bridge): add validation function to export #408
Draft
lrlna
wants to merge
5
commits into
main
Choose a base branch
from
validation-for-fuzzing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1461ce5
feat(router-bridge): add validation function to export
lrlna a2bc045
validation.ts err response
lrlna 9490b58
it works
o0Ignition0o 3ebb0fe
run js validation from inside the deno runtime to avoid leaking memory
lrlna 4380f81
rm do_validate.js from package.json
lrlna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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,5 +1,3 @@ | ||
[workspace] | ||
members = [ | ||
"apollo-federation-types", | ||
"xtask" | ||
] | ||
members = ["apollo-federation-types", "xtask"] | ||
resolver = "2" |
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,3 @@ | ||
[workspace] | ||
members = ["harmonizer", "supergraph", "router-bridge"] | ||
resolver = "2" |
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,62 @@ | ||
import type { validate } from "."; | ||
import type { OperationResult } from "./types"; | ||
|
||
type JsError = { | ||
name: string; | ||
message: string; | ||
stack?: string; | ||
validationError?: boolean; | ||
}; | ||
|
||
/** | ||
* There are several global properties that we make available in our V8 runtime | ||
* and these are the types for those that we expect to use within this script. | ||
* They'll be stripped in the emitting of this file as JS, of course. | ||
*/ | ||
declare let bridge: { validate: typeof validate }; | ||
|
||
declare let done: (operationResult: OperationResult) => void; | ||
declare let schema: string; | ||
declare let query: string; | ||
|
||
const intoSerializableError = (error: Error): JsError => { | ||
const { | ||
name, | ||
message, | ||
stack, | ||
validationError = false, | ||
} = error as Error & { validationError?: boolean }; | ||
return { | ||
name, | ||
message, | ||
stack, | ||
validationError, | ||
}; | ||
}; | ||
|
||
if (!schema) { | ||
done({ | ||
Err: [{ message: "Error in JS-Rust-land: schema is empty." }], | ||
}); | ||
} | ||
|
||
if (!query) { | ||
done({ | ||
Err: [{ message: "Error in JS-Rust-land: query is empty." }], | ||
}); | ||
} | ||
|
||
try { | ||
const diagnostics = bridge | ||
.validate(schema, query) | ||
.map((e) => intoSerializableError(e)); | ||
if (diagnostics.length > 0) { | ||
done({ Err: diagnostics }); | ||
} else { | ||
done({ Ok: "successfully validated" }); | ||
} | ||
} catch (e) { | ||
done({ | ||
Err: [{ message: `An unknown error occured: ${e}` }], | ||
}); | ||
} |
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,3 +1,4 @@ | ||
export { apiSchema } from "./api_schema"; | ||
export { introspect, batchIntrospect } from "./introspection"; | ||
export { BridgeQueryPlanner } from "./plan"; | ||
export { validate } from "./validate"; |
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,16 @@ | ||
import { | ||
buildSchema, | ||
GraphQLError, | ||
parse, | ||
Source, | ||
validate as validateGraphQL, | ||
} from "graphql"; | ||
|
||
export function validate( | ||
schema: string, | ||
query: string | ||
): ReadonlyArray<GraphQLError> { | ||
let ts = buildSchema(schema); | ||
let op = parse(new Source(query, "op.graphql")); | ||
return validateGraphQL(ts, op); | ||
} |
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
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 |
---|---|---|
|
@@ -10,4 +10,5 @@ pub mod error; | |
pub mod introspect; | ||
mod js; | ||
pub mod planner; | ||
pub mod validate; | ||
mod worker; |
9 changes: 9 additions & 0 deletions
9
federation-2/router-bridge/src/snapshots/router_bridge__validate__tests__it_works.snap
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,9 @@ | ||
--- | ||
source: router-bridge/src/validate.rs | ||
expression: validated.errors | ||
--- | ||
[ | ||
{ | ||
"message": "Cannot query field \"me\" on type \"Query\"." | ||
} | ||
] |
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,72 @@ | ||
/*! | ||
# Run introspection against a GraphQL schema and obtain the result | ||
*/ | ||
|
||
use crate::error::Error; | ||
use crate::js::Js; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fmt::Display; | ||
use thiserror::Error; | ||
|
||
/// An error which occurred during JavaScript validation. | ||
/// | ||
/// The shape of this error is meant to mimick that of the error created within | ||
/// JavaScript, which is a [`GraphQLError`] from the [`graphql-js`] library. | ||
/// | ||
/// [`graphql-js']: https://npm.im/graphql | ||
/// [`GraphQLError`]: https://github.com/graphql/graphql-js/blob/3869211/src/error/GraphQLError.js#L18-L75 | ||
#[derive(Debug, Error, Serialize, Deserialize, PartialEq, Eq, Clone)] | ||
pub struct ValidationError { | ||
/// A human-readable description of the error that prevented introspection. | ||
pub message: Option<String>, | ||
} | ||
|
||
impl Display for ValidationError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.write_str(self.message.as_deref().unwrap_or("UNKNOWN")) | ||
} | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] | ||
pub struct ValidationResponse { | ||
/// The introspection response if batch_introspect succeeded | ||
#[serde(default)] | ||
#[serde(rename = "Ok")] | ||
data: Option<serde_json::Value>, | ||
/// The errors raised on this specific query if any | ||
#[serde(default)] | ||
#[serde(rename = "Err")] | ||
errors: Option<Vec<ValidationError>>, | ||
} | ||
|
||
pub fn validate(schema: &str, query: &str) -> Result<ValidationResponse, Error> { | ||
Js::new("validate".to_string()) | ||
.with_parameter("schema", schema)? | ||
.with_parameter("query", query)? | ||
.execute::<ValidationResponse>("validate", include_str!("../bundled/do_validate.js")) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::validate::validate; | ||
|
||
#[test] | ||
fn it_works() { | ||
let schema = r#" | ||
type Query { | ||
hello: String | ||
} | ||
"#; | ||
|
||
let query = r#" | ||
{ | ||
me | ||
} | ||
"#; | ||
|
||
let validated = validate(schema, query).unwrap(); | ||
dbg!(&validated); | ||
assert_eq!(validated.errors.clone().unwrap().len(), 1); | ||
insta::assert_json_snapshot!(validated.errors); | ||
} | ||
} |
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
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.
I think these need to be pub so apollo-rs fuzzing can read them