module author : Austin Ruby
 *  function changelog changelog changelog changelog import * as vscode from 'vscode' ; const fetch = require ( 'node-fetch' ) ; const fs = require ( 'fs' ) ; const extractQueries = require ( './extractQueries' ) ; function readFileSendReqAndWriteResponse ( filePath , channel , portNumber , rootPath , url ) { const copy = fs . readFileSync ( filePath ) . toString ( ) ; const validatedURL = url || `http://localhost:${ portNumber }/graphql` ; if ( filePath . includes ( rootPath ) && ! copy . includes ( 'function graphQuill' ) ) { const newFile = `function graphQuill() {}\n\n${ copy }` ; fs . writeFileSync ( filePath , newFile ) ; } fs . readFile ( filePath , ( err , data ) => { if ( err ) { return null ; } const result = extractQueries ( data . toString ( ) ) ; setTimeout ( async ( ) => { const queriesWithoutQuotes = result . filter ( ( e ) => ( typeof e === 'string' && e . length ) ) . map ( ( query ) => ( typeof query === 'string' && { query : query . slice ( 1 , query . length - 1 ) , response : '' } ) ) ; const finalReqResObj = await Promise . all ( queriesWithoutQuotes . map ( ( reqResObj , index ) => { if ( reqResObj ) { const newReqResObj = { ... reqResObj } ; const { query } = newReqResObj ; return fetch ( validatedURL , { method : 'POST' , headers : { 'Content-Type' : 'application/json' } , body : JSON . stringify ( { query } ) , } ) . then ( ( response ) => response . json ( ) ) . then ( ( parsedResponse ) => { newReqResObj . response = parsedResponse . data || parsedResponse . errors ; return newReqResObj ; } ) . catch ( ( error ) => { channel . append ( `ERROR!!!\n${ JSON . stringify ( error , null , 2 ) }` ) ; } ) ; } return reqResObj ; } ) ) ; channel . clear ( ) ; channel . append ( 'GraphQuill results:' ) ; channel . show ( true ) ; finalReqResObj . forEach ( ( pair ) => { channel . append ( `\nQuery:${ pair . query }\nResponse:\n${ JSON . stringify ( pair . response , null , 2 ) }\n` ) ; } ) ; } , 1 ) ; } ) ; } module . exports = readFileSendReqAndWriteResponse ;