Skip to content

Commit 7da9eb1

Browse files
committed
graphql: cache graphql validation
1 parent 8ea2995 commit 7da9eb1

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

graphql/src/execution/query.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use graph::data::graphql::DocumentExt as _;
22
use graph::data::value::Object;
33
use graphql_parser::Pos;
44
use graphql_tools::validation::rules::*;
5+
use graphql_tools::validation::utils::ValidationError;
56
use graphql_tools::validation::validate::{validate, ValidationPlan};
67
use lazy_static::lazy_static;
78
use std::collections::{BTreeMap, HashMap, HashSet};
89
use std::hash::{Hash, Hasher};
910
use std::iter::FromIterator;
10-
use std::sync::Arc;
11+
use std::sync::{Arc, Mutex, PoisonError};
1112
use std::time::Instant;
1213
use std::{collections::hash_map::DefaultHasher, convert::TryFrom};
1314

@@ -25,6 +26,11 @@ use crate::schema::ast::{self as sast};
2526
use crate::values::coercion;
2627
use crate::{execution::get_field, schema::api::ErrorPolicy};
2728

29+
lazy_static! {
30+
static ref GRAPHQL_VALIDATION_CACHE: Mutex<HashMap<u64, Vec<ValidationError>>> =
31+
Mutex::new(HashMap::<u64, Vec<ValidationError>>::new());
32+
}
33+
2834
lazy_static! {
2935
static ref GRAPHQL_VALIDATION_PLAN: ValidationPlan =
3036
ValidationPlan::from(if !ENV_VARS.graphql.enable_validations {
@@ -150,8 +156,17 @@ impl Query {
150156
max_complexity: Option<u64>,
151157
max_depth: u8,
152158
) -> Result<Arc<Self>, Vec<QueryExecutionError>> {
153-
let validation_errors =
154-
validate(schema.document(), &query.document, &GRAPHQL_VALIDATION_PLAN);
159+
GRAPHQL_VALIDATION_CACHE
160+
.lock()
161+
.unwrap_or_else(PoisonError::into_inner)
162+
.entry(query.shape_hash)
163+
.or_insert_with(|| {
164+
validate(
165+
&schema.document(),
166+
&query.document,
167+
&GRAPHQL_VALIDATION_PLAN,
168+
)
169+
});
155170

156171
if !validation_errors.is_empty() {
157172
if !ENV_VARS.graphql.silent_graphql_validations {
@@ -160,7 +175,7 @@ impl Query {
160175
.map(|e| {
161176
QueryExecutionError::ValidationError(
162177
e.locations.first().cloned(),
163-
e.message,
178+
e.message.clone(),
164179
)
165180
})
166181
.collect());

0 commit comments

Comments
 (0)