@@ -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
@@ -25,6 +26,11 @@ use crate::schema::ast::{self as sast};
25
26
use crate :: values:: coercion;
26
27
use crate :: { execution:: get_field, schema:: api:: ErrorPolicy } ;
27
28
29
+ lazy_static ! {
30
+ static ref GRAPHQL_VALIDATION_CACHE : Mutex <HashMap <u64 , Vec <ValidationError >>> =
31
+ Mutex :: new( HashMap :: <u64 , Vec <ValidationError >>:: new( ) ) ;
32
+ }
33
+
28
34
lazy_static ! {
29
35
static ref GRAPHQL_VALIDATION_PLAN : ValidationPlan =
30
36
ValidationPlan :: from( if !ENV_VARS . graphql. enable_validations {
@@ -150,8 +156,17 @@ impl Query {
150
156
max_complexity : Option < u64 > ,
151
157
max_depth : u8 ,
152
158
) -> 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
+ } ) ;
155
170
156
171
if !validation_errors. is_empty ( ) {
157
172
if !ENV_VARS . graphql . silent_graphql_validations {
@@ -160,7 +175,7 @@ impl Query {
160
175
. map ( |e| {
161
176
QueryExecutionError :: ValidationError (
162
177
e. locations . first ( ) . cloned ( ) ,
163
- e. message ,
178
+ e. message . clone ( ) ,
164
179
)
165
180
} )
166
181
. collect ( ) ) ;
0 commit comments