@@ -43,6 +43,7 @@ interface BulkOperationStatus {
4343export async function runBulkQuery (
4444 query : string ,
4545 session : AdminSession ,
46+ onProgress ?: ( status : string , objectCount : string ) => void ,
4647) : Promise < string > {
4748 const startResult = await adminRequest < any > ( BULK_QUERY_MUTATION , session , { query} )
4849
@@ -51,16 +52,34 @@ export async function runBulkQuery(
5152 throw new Error ( `bulk operation failed: ${ errors } ` )
5253 }
5354
55+ console . log ( 'bulk operation started' , startResult . bulkOperationRunQuery . bulkOperation . id )
56+
57+ let lastStatus = ''
58+
5459 while ( true ) {
55- await sleep ( 1000 )
60+ await sleep ( 1 )
61+
62+ console . log ( 'polling bulk operation status' )
5663
5764 const statusResult = await adminRequest < BulkOperationStatus > ( BULK_OPERATION_STATUS_QUERY , session )
5865 const operation = statusResult . currentBulkOperation
5966
67+ if ( operation . status !== lastStatus && onProgress ) {
68+ onProgress ( operation . status , operation . objectCount )
69+ lastStatus = operation . status
70+ }
71+
72+ if ( operation . status === 'RUNNING' && onProgress ) {
73+ onProgress ( operation . status , operation . objectCount )
74+ }
75+
6076 if ( operation . status === 'COMPLETED' ) {
6177 if ( ! operation . url ) {
6278 throw new Error ( 'bulk operation completed but no results url' )
6379 }
80+ if ( onProgress ) {
81+ onProgress ( 'DOWNLOADING' , operation . objectCount )
82+ }
6483 const response = await fetch ( operation . url )
6584 return await response . text ( )
6685 }
0 commit comments