@@ -21,6 +21,11 @@ use graph::{
2121 data:: query:: { QueryResults , QueryTarget } ,
2222 prelude:: QueryStore ,
2323} ;
24+ use graphql_tools:: validation:: rules:: {
25+ FragmentsOnCompositeTypes , KnownFragmentNamesRule , LeafFieldSelections , LoneAnonymousOperation ,
26+ NoUnusedFragments , OverlappingFieldsCanBeMerged ,
27+ } ;
28+ use graphql_tools:: validation:: validate:: ValidationPlan ;
2429
2530use lazy_static:: lazy_static;
2631
@@ -77,6 +82,7 @@ pub struct GraphQlRunner<S, SM> {
7782 subscription_manager : Arc < SM > ,
7883 load_manager : Arc < LoadManager > ,
7984 result_size : Arc < ResultSizeMetrics > ,
85+ pub graphql_validation_plan : Arc < ValidationPlan > ,
8086}
8187
8288lazy_static ! {
@@ -86,6 +92,10 @@ lazy_static! {
8692 u64 :: from_str( & s)
8793 . unwrap_or_else( |_| panic!( "failed to parse env var GRAPH_GRAPHQL_QUERY_TIMEOUT" ) )
8894 ) ) ;
95+ static ref DISABLE_GRAPHQL_VALIDATIONS : bool = std:: env:: var( "DISABLE_GRAPHQL_VALIDATIONS" )
96+ . unwrap_or_else( |_| "false" . into( ) )
97+ . parse:: <bool >( )
98+ . unwrap_or_else( |_| false ) ;
8999 static ref GRAPHQL_MAX_COMPLEXITY : Option <u64 > = env:: var( "GRAPH_GRAPHQL_MAX_COMPLEXITY" )
90100 . ok( )
91101 . map( |s| u64 :: from_str( & s)
@@ -135,12 +145,24 @@ where
135145 ) -> Self {
136146 let logger = logger. new ( o ! ( "component" => "GraphQlRunner" ) ) ;
137147 let result_size = Arc :: new ( ResultSizeMetrics :: new ( registry) ) ;
148+ let mut graphql_validation_plan = ValidationPlan { rules : Vec :: new ( ) } ;
149+
150+ if !( * DISABLE_GRAPHQL_VALIDATIONS ) {
151+ graphql_validation_plan. add_rule ( Box :: new ( LoneAnonymousOperation { } ) ) ;
152+ graphql_validation_plan. add_rule ( Box :: new ( FragmentsOnCompositeTypes { } ) ) ;
153+ graphql_validation_plan. add_rule ( Box :: new ( OverlappingFieldsCanBeMerged { } ) ) ;
154+ graphql_validation_plan. add_rule ( Box :: new ( KnownFragmentNamesRule { } ) ) ;
155+ graphql_validation_plan. add_rule ( Box :: new ( NoUnusedFragments { } ) ) ;
156+ graphql_validation_plan. add_rule ( Box :: new ( LeafFieldSelections { } ) ) ;
157+ }
158+
138159 GraphQlRunner {
139160 logger,
140161 store,
141162 subscription_manager,
142163 load_manager,
143164 result_size,
165+ graphql_validation_plan : Arc :: new ( graphql_validation_plan) ,
144166 }
145167 }
146168
@@ -212,6 +234,7 @@ where
212234 schema,
213235 network,
214236 query,
237+ self . graphql_validation_plan . clone ( ) ,
215238 max_complexity,
216239 max_depth,
217240 ) ?;
@@ -317,6 +340,7 @@ where
317340 schema,
318341 Some ( network. clone ( ) ) ,
319342 subscription. query ,
343+ self . graphql_validation_plan . clone ( ) ,
320344 * GRAPHQL_MAX_COMPLEXITY ,
321345 * GRAPHQL_MAX_DEPTH ,
322346 ) ?;
0 commit comments