From bf5b62d2df229c76c30357c1ea98170647c5aec2 Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Fri, 22 Jul 2022 14:11:50 +0200 Subject: [PATCH] graphql: unlock GRAPHQL_VALIDATION_CACHE quickly --- Cargo.lock | 3 +-- graphql/Cargo.toml | 2 +- graphql/src/execution/query.rs | 29 ++++++++++++++++++----------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 709559a2edb..767ee852ae5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1888,8 +1888,7 @@ dependencies = [ [[package]] name = "graphql-tools" version = "0.0.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d254a7bcdefe7c9f111ff1c149272f5db0da2dbd142783db45e402bb79731a0" +source = "git+https://github.com/dotansimha/graphql-tools-rs?branch=kamilkisiela-patch-1#0d5a2c6161420018eaf0279bc449ffaeadcd0443" dependencies = [ "graphql-parser", "serde", diff --git a/graphql/Cargo.toml b/graphql/Cargo.toml index 978625a6d1c..dae5553cd91 100644 --- a/graphql/Cargo.toml +++ b/graphql/Cargo.toml @@ -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 = "kamilkisiela-patch-1" } indexmap = "1.9" Inflector = "0.11.3" lazy_static = "1.2.0" diff --git a/graphql/src/execution/query.rs b/graphql/src/execution/query.rs index bdf0c9d3566..a753b86f98a 100644 --- a/graphql/src/execution/query.rs +++ b/graphql/src/execution/query.rs @@ -143,6 +143,23 @@ pub struct Query { pub query_id: String, } +fn validate_query(query: &GraphDataQuery, document: &s::Document) -> Vec { + 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` @@ -156,17 +173,7 @@ impl Query { max_complexity: Option, max_depth: u8, ) -> Result, Vec> { - 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 {