Skip to content

Commit

Permalink
Fix nested queries
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie-wasp committed Jun 18, 2020
1 parent f08cb00 commit 1038599
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/schema/resolvers/ledger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getRepository, In } from "typeorm";
import { IApolloContext } from "../../graphql_server";
import { Ledger, LedgerHeader as LedgerHeaderModel, Operation, PaymentOperations, Transaction } from "../../model";
import { LedgerHeaderFactory } from "../../model/factories";
import { LedgerHeaderFactory, TransactionWithXDRFactory } from "../../model/factories";
import { LedgerHeader } from "../../orm/entities";
import { LEDGER_CREATED, pubsub } from "../../pubsub";
import {
Expand Down Expand Up @@ -30,7 +30,8 @@ export default {
header: ledgerHeaderResolver,
transactions: async (root: Ledger, args: any, ctx: IApolloContext) => {
return makeConnection<IStorageTransactionData, Transaction>(
await ctx.storage.transactions.forLedger(root.seq).all(args)
await ctx.storage.transactions.forLedger(root.seq).all(args),
r => TransactionWithXDRFactory.fromStorage(r)
);
},
operations: async (root: Ledger, args: any, ctx: IApolloContext) => {
Expand Down
8 changes: 3 additions & 5 deletions src/schema/resolvers/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { withFilter } from "graphql-subscriptions";
import { Asset } from "stellar-base";
import { IApolloContext } from "../../graphql_server";
import { Operation, OperationType, Transaction } from "../../model";
import { OperationFactory, TransactionWithXDRFactory } from "../../model/factories";
import { OperationFactory } from "../../model/factories";
import { NEW_OPERATION, pubsub } from "../../pubsub";
import { OperationData as StorageOperationData } from "../../storage/types";
import { makeConnection } from "./util";
Expand All @@ -17,8 +17,7 @@ export default {
return operation.tx;
}

const tx = await ctx.storage.transactions.get(operation.tx.id);
return TransactionWithXDRFactory.fromStorage(tx);
return ctx.storage.transactions.findById(operation.tx.id);
},
__resolveType(operation: Operation) {
switch (operation.type) {
Expand Down Expand Up @@ -83,8 +82,7 @@ export default {
SetOptionsSigner: { account: resolvers.account },
Query: {
operation: async (root: any, args: { id: string }, ctx: IApolloContext) => {
const doc = await ctx.storage.operations.get(args.id);
return OperationFactory.fromStorage(doc);
return ctx.storage.operations.findById(args.id);
},
operations: async (root: any, args: any, ctx: IApolloContext) => {
const { type, ...paging } = args;
Expand Down
6 changes: 3 additions & 3 deletions src/schema/resolvers/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { makeConnection } from "./util";

import { Operation, PaymentOperations, Transaction } from "../../model";
import { OperationFactory, TransactionWithXDRFactory } from "../../model/factories";
import { OperationsStorage } from "../../storage";
import {
ITransactionData as IStorageTransactionData,
OperationData as StorageOperationData
Expand All @@ -30,7 +31,7 @@ export default {
},
operations: async (root: Transaction, args: any, ctx: IApolloContext) => {
return makeConnection<StorageOperationData, Operation>(
await ctx.storage.operations.forTransaction(root.id).all(args),
await OperationsStorage.forTransaction(root.id, args),
r => OperationFactory.fromStorage(r)
);
},
Expand All @@ -46,8 +47,7 @@ export default {
},
Query: {
transaction: async (root: any, args: any, ctx: IApolloContext, info: any) => {
const tx = await ctx.storage.transactions.get(args.id);
return TransactionWithXDRFactory.fromStorage(tx);
return await ctx.storage.transactions.findById(args.id);
},
transactions: async (root: any, args: any, ctx: IApolloContext, info: any) => {
const records = await ctx.storage.transactions.all(args);
Expand Down
31 changes: 24 additions & 7 deletions src/storage/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ export abstract class BaseStorage {
};
}

public async get(id: string) {
return connection.get({ index: this.elasticIndexName, id }).then(({ body }: { body: any }) => {
return { ...body._source, id: body._id };
});
}

public async all(pagingParams?: PagingParams) {
if (pagingParams) {
this.paginate(pagingParams);
Expand All @@ -31,9 +25,28 @@ export abstract class BaseStorage {
return [];
}

// reset everything
this.searchParams = {
query: {
bool: {
must: []
}
}
};

return (pagingParams ? properlyOrdered(docs, pagingParams) : docs);
}

// Finds document by ElasticSearch id
public async findById(id: string) {
const { body: response } = await connection.get({
index: this.elasticIndexName,
id,
});

return this.convertRawDoc(response._source);
}

public async one() {
this.searchParams.size = 1;

Expand All @@ -56,10 +69,14 @@ export abstract class BaseStorage {
});

return response.hits.hits.map((h: any) => {
return { ...h._source, id: h._id };
return { ...h._source, id: this.hitID(h) };
});
}

protected hitID(hit: any): string {
return hit._id;
}

protected async aggregation(queries: any) {
const { body: response } = await connection.search(
{
Expand Down
6 changes: 4 additions & 2 deletions src/storage/operations.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { AccountID, Operation, OperationType } from "../model";
import { OperationFactory } from "../model/factories";
import { DataMapper } from "../model/factories/operation_data_mapper/storage";
import { PagingParams } from "../util/paging";
import { BaseStorage } from "./base";
import { OperationData as StorageOperationData } from "./types";

export class OperationsStorage extends BaseStorage {
public static async forTransaction(txId: string): Promise<Operation[]> {
return new OperationsStorage().addTerm({ tx_id: txId }).all();
public static async forTransaction(txId: string, paging?: PagingParams): Promise<StorageOperationData[]> {
return new OperationsStorage().addTerm({ tx_id: txId }).all(paging);
}

public filterTypes(types: OperationType[]) {
Expand Down
8 changes: 8 additions & 0 deletions src/storage/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ export class TransactionsStorage extends BaseStorage {
return this;
}

public async findById(id: string) {
return this.addTerm({ id }).one();
}

protected get elasticIndexName() {
return "tx";
}

protected hitID(hit: any): string {
return hit._source.id;
}

protected convertRawDoc(doc: any): Transaction {
return TransactionWithXDRFactory.fromStorage(doc);
}
Expand Down

0 comments on commit 1038599

Please sign in to comment.