@@ -550,6 +550,28 @@ WOQLClient.prototype.info = function () {
550550 return this . dispatch ( CONST . GET , url ) . then ( ( response ) => response ) ;
551551} ;
552552
553+ // Get Resource objects from WOQL query
554+ function getResourceObjects ( queryObject , result_array ) {
555+ if ( queryObject instanceof Array ) {
556+ for ( let i = 0 ; i < queryObject . length ; i += 1 ) {
557+ getResourceObjects ( queryObject [ i ] , result_array ) ;
558+ }
559+ } else {
560+ const keys = Object . keys ( queryObject ) ;
561+
562+ for ( let i = 0 ; i < keys . length ; i += 1 ) {
563+ if ( keys [ i ] === 'resource' ) {
564+ if ( queryObject [ keys [ i ] ] [ '@type' ] && queryObject [ keys [ i ] ] [ '@type' ] === 'QueryResource' ) {
565+ result_array . push ( queryObject [ keys [ i ] ] ) ;
566+ }
567+ }
568+ if ( queryObject [ keys [ i ] ] instanceof Object || queryObject [ keys [ i ] ] instanceof Array ) {
569+ getResourceObjects ( queryObject [ keys [ i ] ] , result_array ) ;
570+ }
571+ }
572+ }
573+ }
574+
553575/**
554576 * Executes a WOQL query on the specified database and returns the results
555577 * @param {WOQLQuery } woql - an instance of the WOQLQuery class
@@ -572,13 +594,19 @@ WOQLClient.prototype.query = function (woql, commitMsg, allWitnesses, lastDataVe
572594 doql . query = woql . json ( ) ;
573595
574596 let postBody ;
597+ const resourceObjects = [ ] ;
598+ getResourceObjects ( doql . query , resourceObjects ) ;
575599
576- if ( doql . query . resource && doql . query . resource [ '@type' ] === 'QueryResource' && doql . query . resource . source . post ) {
577- const fileName = doql . query . resource . source . post . split ( '/' ) . pop ( ) ;
578-
600+ if ( resourceObjects . length > 0 ) {
579601 const formData = new FormData ( ) ;
580- formData . append ( 'file' , fs . createReadStream ( doql . query . resource . source . post ) ) ;
581- doql . query . resource . source . post = fileName ;
602+
603+ resourceObjects . forEach ( ( resourceObject ) => {
604+ const fileName = resourceObject . source . post . split ( '/' ) . pop ( ) ;
605+
606+ formData . append ( 'file' , fs . createReadStream ( resourceObject . source . post ) ) ;
607+ resourceObject . source . post = fileName ;
608+ } ) ;
609+
582610 formData . append ( 'payload' , Buffer . from ( JSON . stringify ( doql ) ) , { filename : 'body.json' , contentType : 'application/json' } ) ;
583611 this . customHeaders ( formData . getHeaders ( ) ) ;
584612
0 commit comments