@@ -2,12 +2,13 @@ use graph::data::graphql::DocumentExt as _;
2
2
use graph:: data:: value:: Object ;
3
3
use graphql_parser:: Pos ;
4
4
use graphql_tools:: validation:: rules:: * ;
5
+ use graphql_tools:: validation:: utils:: ValidationError ;
5
6
use graphql_tools:: validation:: validate:: { validate, ValidationPlan } ;
6
7
use lazy_static:: lazy_static;
7
8
use std:: collections:: { BTreeMap , HashMap , HashSet } ;
8
9
use std:: hash:: { Hash , Hasher } ;
9
10
use std:: iter:: FromIterator ;
10
- use std:: sync:: Arc ;
11
+ use std:: sync:: { Arc , Mutex , PoisonError } ;
11
12
use std:: time:: Instant ;
12
13
use std:: { collections:: hash_map:: DefaultHasher , convert:: TryFrom } ;
13
14
@@ -23,6 +24,11 @@ use crate::schema::ast::{self as sast};
23
24
use crate :: values:: coercion;
24
25
use crate :: { execution:: get_field, schema:: api:: ErrorPolicy } ;
25
26
27
+ lazy_static ! {
28
+ static ref GRAPHQL_VALIDATION_CACHE : Mutex <HashMap <u64 , Vec <ValidationError >>> =
29
+ Mutex :: new( HashMap :: <u64 , Vec <ValidationError >>:: new( ) ) ;
30
+ }
31
+
26
32
lazy_static ! {
27
33
static ref GRAPHQL_VALIDATION_PLAN : ValidationPlan = ValidationPlan :: from(
28
34
if std:: env:: var( "ENABLE_GRAPHQL_VALIDATIONS" ) . ok( ) . is_none( ) {
@@ -145,17 +151,25 @@ impl Query {
145
151
max_complexity : Option < u64 > ,
146
152
max_depth : u8 ,
147
153
) -> Result < Arc < Self > , Vec < QueryExecutionError > > {
148
- let validation_errors = validate (
149
- & schema. document ( ) ,
150
- & query. document ,
151
- & GRAPHQL_VALIDATION_PLAN ,
152
- ) ;
154
+ let mut validation_cache = GRAPHQL_VALIDATION_CACHE
155
+ . lock ( )
156
+ . unwrap_or_else ( PoisonError :: into_inner) ;
157
+ let validation_errors = validation_cache. entry ( query. shape_hash ) . or_insert_with ( || {
158
+ validate (
159
+ & schema. document ( ) ,
160
+ & query. document ,
161
+ & GRAPHQL_VALIDATION_PLAN ,
162
+ )
163
+ } ) ;
153
164
154
165
if validation_errors. len ( ) > 0 {
155
166
return Err ( validation_errors
156
167
. into_iter ( )
157
168
. map ( |e| {
158
- QueryExecutionError :: ValidationError ( e. locations . first ( ) . cloned ( ) , e. message )
169
+ QueryExecutionError :: ValidationError (
170
+ e. locations . first ( ) . cloned ( ) ,
171
+ e. message . clone ( ) ,
172
+ )
159
173
} )
160
174
. collect ( ) ) ;
161
175
}
0 commit comments