Skip to content

Commit

Permalink
feat: improve search (#117)
Browse files Browse the repository at this point in the history
closes FE-12
  • Loading branch information
matt-user authored Nov 15, 2023
1 parent 5fba76f commit 69c735a
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
122 changes: 122 additions & 0 deletions packages/graphql/src/domains/Search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { gql } from 'graphql-request';

import { parseAddressParam } from '../utils/address';
import { Domain } from '../utils/domain';

type Args = {
query: string;
};

export class SearchDomain extends Domain<any, Args> {
static createResolvers() {
const domain = new SearchDomain();
return {
...domain.createResolver('search', 'getSearch'),
};
}

async getSearch() {
const { query } = this.args;
const parsedQuery = parseAddressParam(query);
// TODO use last 5 once reverse pagination is supported
const gqlQuery = gql`
query fuelCoreQuery(
$owner: Address!
$contractId: ContractId!
$blockId: BlockId!
$transactionId: TransactionId!
) {
transactionsByOwner(owner: $owner, first: 5) {
nodes {
id
}
}
contract(id: $contractId) {
id
}
block(id: $blockId) {
id
header {
height
}
}
transaction(id: $transactionId) {
id
}
}
`;

/** @todo: Get types from the query directly instead of creating custom types */
type Result = {
transactionsByOwner: {
nodes: {
id: string;
}[];
};
contract: {
id: string;
};
block: {
id: string;
header: {
height: string;
};
};
transaction: {
id: string;
};
};

const data = await this.query<Result>(gqlQuery, {
owner: parsedQuery,
contractId: parsedQuery,
blockId: parsedQuery,
transactionId: parsedQuery,
});
const result: {
account: null | {
address: string;
transactions: { id: string }[];
};
contract: null | {
id: string;
};
block: null | {
id: string;
height: string;
};
transaction: null | {
id: string;
};
} = {
account: null,
contract: null,
block: null,
transaction: null,
};

if (data.transactionsByOwner.nodes.length) {
result.account = {
address: parsedQuery,
transactions: data.transactionsByOwner.nodes.map((node) => {
return { id: node.id };
}),
};
}

if (data.contract) {
result.contract = { id: data.contract.id };
}

if (data.block) {
result.block = { id: data.block.id, height: data.block.header.height };
}

if (data.transaction) {
result.transaction = { id: data.transaction.id };
}

return result;
}
}
20 changes: 20 additions & 0 deletions packages/graphql/src/queries/search-query.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
query searchQuery($search: String!) {
search(query: $search) {
account {
address
transactions {
id
}
}
contract {
id
}
block {
id
height
}
transaction {
id
}
}
}
32 changes: 32 additions & 0 deletions packages/graphql/src/services/extends.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,35 @@ extend type Block {
totalGasUsed: U64
producer: Address
}

# --------------------------------
# Query
# --------------------------------
type SearchAccount {
address: Address
transactions: [SearchTransaction]
}

type SearchContract {
id: ContractId
}

type SearchBlock {
id: BlockId
height: String
}

type SearchTransaction {
id: TransactionId
}

type SearchResult {
account: SearchAccount
contract: SearchContract
block: SearchBlock
transaction: SearchTransaction
}

extend type Query {
search(query: String!): SearchResult
}
2 changes: 2 additions & 0 deletions packages/graphql/src/services/extends.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BalanceDomain } from '../domains/Balance';
import { BlockDomain } from '../domains/Block';
import { SearchDomain } from '../domains/Search';
import { TransactionDomain } from '../domains/Transaction';
import { TransactionConnectionDomain } from '../domains/TransactionConnection';

Expand All @@ -11,4 +12,5 @@ export const extendsResolvers = {
Transaction: TransactionDomain.createResolvers(),
TransactionConnection: TransactionConnectionDomain.createResolvers(),
Block: BlockDomain.createResolvers(),
Query: SearchDomain.createResolvers(),
};

0 comments on commit 69c735a

Please sign in to comment.