import { getRepository , In } from "typeorm" ; import { IApolloContext } from "../../graphql_server" ; import { Ledger , LedgerHeader as LedgerHeaderModel , Operation , PaymentOperations , Transaction } from "../../model" ; import { LedgerHeaderFactory , TransactionWithXDRFactory } from "../../model/factories" ; import { LedgerHeader } from "../../orm/entities" ; import { LEDGER_CREATED , pubsub } from "../../pubsub" ; import { ITransactionData as IStorageTransactionData , OperationData as StorageOperationData } from "../../storage/types" ; import { createBatchResolver , makeConnection } from "./util" ; const ledgerHeaderResolver = createBatchResolver < Ledger , LedgerHeader > ( async ( ledgers ) => { const seqNumsWithoutHeaders = ledgers . filter ( l => l . header === undefined ) . map ( l => l . seq ) ; let fetchedHeaders = [ ] ; if ( seqNumsWithoutHeaders . length !== 0 ) { fetchedHeaders = ( await getRepository ( LedgerHeader ) . find ( { where : { seq : In ( seqNumsWithoutHeaders ) } } ) ) . map ( h => LedgerHeaderFactory . fromXDR ( h . data ) ) ; } return ledgers . map ( l => { return l . header || fetchedHeaders . find ( h => h . ledgerSeq === l . seq ) || null ; } ) ; } ) ; export default { Ledger : { header : ledgerHeaderResolver , transactions : async ( root , args , ctx ) => { return makeConnection < IStorageTransactionData , Transaction > ( await ctx . storage . transactions . forLedger ( root . seq ) . all ( args ) , r => TransactionWithXDRFactory . fromStorage ( r ) ) ; } , operations : async ( root , args , ctx ) => { return makeConnection < StorageOperationData , Operation > ( await ctx . storage . operations . forLedger ( root . seq ) . all ( args ) ) ; } , payments : async ( root , args , ctx ) => { return makeConnection < StorageOperationData , Operation > ( await ctx . storage . operations . forLedger ( root . seq ) . filterTypes ( PaymentOperations ) . all ( args ) ) ; } } , Query : { ledger ( root , args , ctx , info ) { return new Ledger ( args . seq ) ; } , ledgers ( root , args , ctx , info ) { return args . seq . map ( ( seq ) => new Ledger ( seq ) ) ; } } , Subscription : { ledgerCreated : { resolve ( payload , args , ctx , info ) { return payload ; } , subscribe ( ) { return pubsub . asyncIterator ( LEDGER_CREATED ) ; } } } } ;