import { SavedObject , SavedObjectsClientContract } from 'src/core/server' ; const MAX_BULK_GET_SIZE = 10000 ; interface ObjectsToCollect { id : string ; type : string ; } export async function collectReferencesDeep ( savedObjectClient , objects ) { let result = [ ] ; const queue = [ ... objects ] ; while ( queue . length !== 0 ) { const itemsToGet = queue . splice ( 0 , MAX_BULK_GET_SIZE ) ; const { saved_objects : savedObjects } = await savedObjectClient . bulkGet ( itemsToGet ) ; result = result . concat ( savedObjects ) ; for ( const { references = [ ] } of savedObjects ) { for ( const reference of references ) { const isDuplicate = queue . concat ( result ) . some ( ( obj ) => obj . type === reference . type && obj . id === reference . id ) ; if ( isDuplicate ) { continue ; } queue . push ( { type : reference . type , id : reference . id } ) ; } } } return result ; }