Skip to content

Commit

Permalink
graphql: unlock GRAPHQL_VALIDATION_CACHE quickly
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela committed Jul 22, 2022
1 parent 7da9eb1 commit 31a6405
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion graphql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
crossbeam = "0.8"
graph = { path = "../graph" }
graphql-parser = "0.4.0"
graphql-tools = "0.0.19"
graphql-tools = { git = "https://github.com/dotansimha/graphql-tools-rs", branch = "clone-validation-error" }
indexmap = "1.9"
Inflector = "0.11.3"
lazy_static = "1.2.0"
Expand Down
24 changes: 13 additions & 11 deletions graphql/src/execution/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ pub struct Query {
pub query_id: String,
}

fn validate_query(query: &GraphDataQuery, document: &s::Document) -> Vec<ValidationError> {
let mut cache = GRAPHQL_VALIDATION_CACHE
.lock()
.unwrap_or_else(PoisonError::into_inner);

let errors = cache
.entry(query.shape_hash)
.or_insert_with(|| validate(&document, &query.document, &GRAPHQL_VALIDATION_PLAN));

return errors.clone();
}

impl Query {
/// Process the raw GraphQL query `query` and prepare for executing it.
/// The returned `Query` has already been validated and, if `max_complexity`
Expand All @@ -156,17 +168,7 @@ impl Query {
max_complexity: Option<u64>,
max_depth: u8,
) -> Result<Arc<Self>, Vec<QueryExecutionError>> {
GRAPHQL_VALIDATION_CACHE
.lock()
.unwrap_or_else(PoisonError::into_inner)
.entry(query.shape_hash)
.or_insert_with(|| {
validate(
&schema.document(),
&query.document,
&GRAPHQL_VALIDATION_PLAN,
)
});
let validation_errors = validate_query(&query, &schema.document());

if !validation_errors.is_empty() {
if !ENV_VARS.graphql.silent_graphql_validations {
Expand Down

0 comments on commit 31a6405

Please sign in to comment.