@@ -7,10 +7,7 @@ use std::sync::Arc;
7
7
use std:: time:: Instant ;
8
8
use std:: { collections:: hash_map:: DefaultHasher , convert:: TryFrom } ;
9
9
10
- use graph:: data:: graphql:: {
11
- ext:: { DocumentExt , TypeExt } ,
12
- ObjectOrInterface ,
13
- } ;
10
+ use graph:: data:: graphql:: { ext:: TypeExt , ObjectOrInterface } ;
14
11
use graph:: data:: query:: { Query as GraphDataQuery , QueryVariables } ;
15
12
use graph:: data:: schema:: ApiSchema ;
16
13
use graph:: prelude:: {
@@ -21,10 +18,7 @@ use crate::execution::ast as a;
21
18
use crate :: query:: { ast as qast, ext:: BlockConstraint } ;
22
19
use crate :: schema:: ast as sast;
23
20
use crate :: values:: coercion;
24
- use crate :: {
25
- execution:: { get_field, get_named_type, object_or_interface} ,
26
- schema:: api:: ErrorPolicy ,
27
- } ;
21
+ use crate :: { execution:: get_field, schema:: api:: ErrorPolicy } ;
28
22
29
23
#[ derive( Clone , Debug ) ]
30
24
pub enum ComplexityError {
@@ -157,8 +151,8 @@ impl Query {
157
151
158
152
let start = Instant :: now ( ) ;
159
153
let root_type = match kind {
160
- Kind :: Query => schema. document ( ) . get_root_query_type ( ) . unwrap ( ) ,
161
- Kind :: Subscription => schema. document ( ) . get_root_subscription_type ( ) . unwrap ( ) ,
154
+ Kind :: Query => schema. query_type . as_ref ( ) ,
155
+ Kind :: Subscription => schema. subscription_type . as_ref ( ) . unwrap ( ) ,
162
156
} ;
163
157
// Use an intermediate struct so we can modify the query before
164
158
// enclosing it in an Arc
@@ -316,7 +310,7 @@ pub fn coerce_variables(
316
310
. flatten ( )
317
311
{
318
312
// Skip variable if it has an invalid type
319
- if !sast :: is_input_type ( schema. document ( ) , & variable_def. var_type ) {
313
+ if !schema. is_input_type ( & variable_def. var_type ) {
320
314
errors. push ( QueryExecutionError :: InvalidVariableTypeError (
321
315
variable_def. position ,
322
316
variable_def. name . to_owned ( ) ,
@@ -371,7 +365,7 @@ fn coerce_variable(
371
365
) -> Result < r:: Value , Vec < QueryExecutionError > > {
372
366
use crate :: values:: coercion:: coerce_value;
373
367
374
- let resolver = |name : & str | schema. document ( ) . get_named_type ( name) ;
368
+ let resolver = |name : & str | schema. get_named_type ( name) ;
375
369
376
370
coerce_value ( value, & variable_def. var_type , & resolver) . map_err ( |value| {
377
371
vec ! [ QueryExecutionError :: InvalidArgumentError (
@@ -430,7 +424,6 @@ impl<'s> RawQuery<'s> {
430
424
. items
431
425
. iter ( )
432
426
. try_fold ( 0 , |total_complexity, selection| {
433
- let schema = self . schema . document ( ) ;
434
427
match selection {
435
428
q:: Selection :: Field ( field) => {
436
429
// Empty selection sets are the base case.
@@ -454,7 +447,8 @@ impl<'s> RawQuery<'s> {
454
447
. ok_or ( Invalid ) ?;
455
448
456
449
let field_complexity = self . complexity_inner (
457
- & get_named_type ( schema, s_field. field_type . get_base_type ( ) )
450
+ self . schema
451
+ . get_named_type ( s_field. field_type . get_base_type ( ) )
458
452
. ok_or ( Invalid ) ?,
459
453
& field. selection_set ,
460
454
max_depth,
@@ -483,7 +477,7 @@ impl<'s> RawQuery<'s> {
483
477
q:: Selection :: FragmentSpread ( fragment) => {
484
478
let def = self . fragments . get ( & fragment. fragment_name ) . unwrap ( ) ;
485
479
let q:: TypeCondition :: On ( type_name) = & def. type_condition ;
486
- let ty = get_named_type ( schema , & type_name) . ok_or ( Invalid ) ?;
480
+ let ty = self . schema . get_named_type ( & type_name) . ok_or ( Invalid ) ?;
487
481
488
482
// Copy `visited_fragments` on write.
489
483
let mut visited_fragments = visited_fragments. clone ( ) ;
@@ -501,12 +495,12 @@ impl<'s> RawQuery<'s> {
501
495
q:: Selection :: InlineFragment ( fragment) => {
502
496
let ty = match & fragment. type_condition {
503
497
Some ( q:: TypeCondition :: On ( type_name) ) => {
504
- get_named_type ( schema , & type_name) . ok_or ( Invalid ) ?
498
+ self . schema . get_named_type ( type_name) . ok_or ( Invalid ) ?
505
499
}
506
- _ => ty. clone ( ) ,
500
+ _ => ty,
507
501
} ;
508
502
self . complexity_inner (
509
- & ty,
503
+ ty,
510
504
& fragment. selection_set ,
511
505
max_depth,
512
506
depth + 1 ,
@@ -523,7 +517,7 @@ impl<'s> RawQuery<'s> {
523
517
/// If the query is invalid, returns `Ok(0)` so that execution proceeds and
524
518
/// gives a proper error.
525
519
fn complexity ( & self , max_depth : u8 ) -> Result < u64 , QueryExecutionError > {
526
- let root_type = sast :: get_root_query_type_def ( self . schema . document ( ) ) . unwrap ( ) ;
520
+ let root_type = self . schema . get_root_query_type_def ( ) . unwrap ( ) ;
527
521
528
522
match self . complexity_inner (
529
523
root_type,
@@ -545,7 +539,7 @@ impl<'s> RawQuery<'s> {
545
539
}
546
540
547
541
fn validate_fields ( & self ) -> Result < ( ) , Vec < QueryExecutionError > > {
548
- let root_type = self . schema . document ( ) . get_root_query_type ( ) . unwrap ( ) ;
542
+ let root_type = self . schema . query_type . as_ref ( ) ;
549
543
550
544
let errors =
551
545
self . validate_fields_inner ( & "Query" . to_owned ( ) , root_type. into ( ) , & self . selection_set ) ;
@@ -563,8 +557,6 @@ impl<'s> RawQuery<'s> {
563
557
ty : ObjectOrInterface < ' _ > ,
564
558
selection_set : & q:: SelectionSet ,
565
559
) -> Vec < QueryExecutionError > {
566
- let schema = self . schema . document ( ) ;
567
-
568
560
selection_set
569
561
. items
570
562
. iter ( )
@@ -573,9 +565,9 @@ impl<'s> RawQuery<'s> {
573
565
q:: Selection :: Field ( field) => match get_field ( ty, & field. name ) {
574
566
Some ( s_field) => {
575
567
let base_type = s_field. field_type . get_base_type ( ) ;
576
- if get_named_type ( schema , base_type) . is_none ( ) {
568
+ if self . schema . get_named_type ( base_type) . is_none ( ) {
577
569
errors. push ( QueryExecutionError :: NamedTypeError ( base_type. into ( ) ) ) ;
578
- } else if let Some ( ty) = object_or_interface ( schema , base_type) {
570
+ } else if let Some ( ty) = self . schema . object_or_interface ( base_type) {
579
571
errors. extend ( self . validate_fields_inner (
580
572
base_type,
581
573
ty,
@@ -593,7 +585,7 @@ impl<'s> RawQuery<'s> {
593
585
match self . fragments . get ( & fragment. fragment_name ) {
594
586
Some ( frag) => {
595
587
let q:: TypeCondition :: On ( type_name) = & frag. type_condition ;
596
- match object_or_interface ( schema , type_name) {
588
+ match self . schema . object_or_interface ( type_name) {
597
589
Some ( ty) => errors. extend ( self . validate_fields_inner (
598
590
type_name,
599
591
ty,
@@ -611,7 +603,7 @@ impl<'s> RawQuery<'s> {
611
603
}
612
604
q:: Selection :: InlineFragment ( fragment) => match & fragment. type_condition {
613
605
Some ( q:: TypeCondition :: On ( type_name) ) => {
614
- match object_or_interface ( schema , type_name) {
606
+ match self . schema . object_or_interface ( type_name) {
615
607
Some ( ty) => errors. extend ( self . validate_fields_inner (
616
608
type_name,
617
609
ty,
@@ -745,7 +737,7 @@ impl Transform {
745
737
) -> Result < ( ) , Vec < QueryExecutionError > > {
746
738
let mut errors = vec ! [ ] ;
747
739
748
- let resolver = |name : & str | self . schema . document ( ) . get_named_type ( name) ;
740
+ let resolver = |name : & str | self . schema . get_named_type ( name) ;
749
741
750
742
for argument_def in sast:: get_argument_definitions ( ty, field_name)
751
743
. into_iter ( )
@@ -812,7 +804,7 @@ impl Transform {
812
804
let field_type = parent_type. field ( & name) . expect ( "field names are valid" ) ;
813
805
let ty = field_type. field_type . get_base_type ( ) ;
814
806
let type_set = a:: ObjectTypeSet :: from_name ( & self . schema , ty) ?;
815
- let ty = self . schema . document ( ) . object_or_interface ( ty) . unwrap ( ) ;
807
+ let ty = self . schema . object_or_interface ( ty) . unwrap ( ) ;
816
808
self . expand_selection_set ( selection_set, & type_set, ty) ?
817
809
} ;
818
810
@@ -914,7 +906,6 @@ impl Transform {
914
906
let ty = match frag_cond {
915
907
Some ( q:: TypeCondition :: On ( name) ) => self
916
908
. schema
917
- . document ( )
918
909
. object_or_interface ( name)
919
910
. expect ( "type names on fragment spreads are valid" ) ,
920
911
None => ty,
0 commit comments