import stellar from "stellar-base" ; import { getRepository , In } from "typeorm" ; import { IApolloContext } from "../../../graphql_server" ; import { AssetID } from "../../../model" ; import { AssetFactory } from "../../../model/factories" ; import { Asset } from "../../../orm/entities" ; import { createBatchResolver , onlyFieldsRequested } from "../util" ; export const asset = createBatchResolver < any , Asset [ ] > ( async ( source , args , ctx , info ) => { const field = info . fieldName ; if ( onlyFieldsRequested ( info , "id" , "code" , "issuer" , "native" ) ) { return source . map ( ( obj ) => { if ( Array . isArray ( obj [ field ] ) ) { return obj [ field ] . map ( ( a ) => AssetFactory . fromId ( a . toString ( ) ) ) ; } return AssetFactory . fromId ( obj [ field ] . toString ( ) ) ; } ) ; } const ids = source . map ( ( s ) => s [ field ] . toString ( ) ) ; const assets = await getRepository ( Asset ) . find ( { id : In ( ids ) } ) ; return ids . map ( id => assets . find ( a => a . id === id ) || null ) ; } ) ;