@@ -6,7 +6,7 @@ mod tests;
6
6
7
7
use crate :: query:: UsedTypes ;
8
8
use crate :: type_qualifiers:: GraphqlTypeQualifier ;
9
- use std:: collections:: HashMap ;
9
+ use std:: collections:: { HashMap , HashSet } ;
10
10
11
11
pub ( crate ) const DEFAULT_SCALARS : & [ & str ] = & [ "ID" , "String" , "Int" , "Float" , "Boolean" ] ;
12
12
@@ -400,7 +400,13 @@ impl StoredInputType {
400
400
}
401
401
}
402
402
403
- fn contains_type_without_indirection ( & self , input_id : InputId , schema : & Schema ) -> bool {
403
+ fn contains_type_without_indirection (
404
+ & self ,
405
+ input_id : InputId ,
406
+ schema : & Schema ,
407
+ visited_types : & mut HashSet < String > ,
408
+ ) -> bool {
409
+ visited_types. insert ( self . name . clone ( ) ) ;
404
410
// The input type is recursive if any of its members contains it, without indirection
405
411
self . fields . iter ( ) . any ( |( _name, field_type) | {
406
412
// the field is indirected, so no boxing is needed
@@ -417,8 +423,13 @@ impl StoredInputType {
417
423
418
424
let input = schema. get_input ( field_input_id) ;
419
425
426
+ // no need to visit type twice (prevents infinite recursion)
427
+ if visited_types. contains ( & input. name ) {
428
+ return true ;
429
+ }
430
+
420
431
// we check if the other input contains this one (without indirection)
421
- input. contains_type_without_indirection ( input_id, schema)
432
+ input. contains_type_without_indirection ( input_id, schema, visited_types )
422
433
} else {
423
434
// the field is not referring to an input type
424
435
false
@@ -429,9 +440,9 @@ impl StoredInputType {
429
440
430
441
pub ( crate ) fn input_is_recursive_without_indirection ( input_id : InputId , schema : & Schema ) -> bool {
431
442
let input = schema. get_input ( input_id) ;
432
- input. contains_type_without_indirection ( input_id, schema)
443
+ let mut visited_types = HashSet :: < String > :: new ( ) ;
444
+ input. contains_type_without_indirection ( input_id, schema, & mut visited_types)
433
445
}
434
-
435
446
impl std:: convert:: From < graphql_parser:: schema:: Document > for Schema {
436
447
fn from ( ast : graphql_parser:: schema:: Document ) -> Schema {
437
448
graphql_parser_conversion:: build_schema ( ast)
0 commit comments