|
5 | 5 | * Each block contains metadata about the block itself, references to related blocks, |
6 | 6 | * and cryptographic information needed for blockchain verification. |
7 | 7 | * |
8 | | - * The module also exports a PostGraphile plugin that extends the GraphQL schema |
9 | | - * with custom block queries for API consumers. |
10 | 8 | */ |
11 | 9 |
|
12 | 10 | import { Model, DataTypes } from 'sequelize'; |
13 | 11 | import { sequelize } from '../config/database'; |
14 | | -import { gql, makeExtendSchemaPlugin } from 'postgraphile'; |
15 | 12 |
|
16 | 13 | /** |
17 | 14 | * Interface defining the attributes of a Block. |
@@ -250,121 +247,4 @@ Block.init( |
250 | 247 | }, |
251 | 248 | ); |
252 | 249 |
|
253 | | -/** |
254 | | - * PostGraphile plugin that extends the GraphQL schema with custom Block queries. |
255 | | - * This allows API consumers to query blocks by height and perform search operations |
256 | | - * across blocks, transactions, addresses, and tokens. |
257 | | - * |
258 | | - * TODO: [OPTIMIZATION] Consider implementing caching mechanisms for frequently used queries |
259 | | - * to improve API response times. |
260 | | - */ |
261 | | -export const blockQueryPlugin = makeExtendSchemaPlugin(build => { |
262 | | - return { |
263 | | - typeDefs: gql` |
264 | | - extend type Query { |
265 | | - blockByHeight(height: Int!, chainId: Int!): Block |
266 | | - searchAll(searchTerm: String!, limit: Int!, heightFilter: Int): SearchAllResult |
267 | | - } |
268 | | -
|
269 | | - type SearchAllResult { |
270 | | - blocks: [Block] |
271 | | - transactions: [Transaction] |
272 | | - addresses: [Balance] |
273 | | - tokens: [Contract] |
274 | | - } |
275 | | - `, |
276 | | - resolvers: { |
277 | | - Query: { |
278 | | - /** |
279 | | - * Resolver to find a block by its height and chain ID. |
280 | | - * Uses a direct SQL query for optimal performance. |
281 | | - */ |
282 | | - blockByHeight: async (_query, args, context, resolveInfo) => { |
283 | | - const { height, chainId } = args; |
284 | | - const { rootPgPool } = context; |
285 | | - const { rows } = await rootPgPool.query( |
286 | | - `SELECT * FROM public."Blocks" WHERE height = $1 AND "chainId" = $2`, |
287 | | - [height, chainId], |
288 | | - ); |
289 | | - return rows[0]; |
290 | | - }, |
291 | | - |
292 | | - /** |
293 | | - * Resolver for the searchAll query that searches across multiple entities. |
294 | | - * Performs parallel queries for different entity types and combines the results. |
295 | | - * |
296 | | - * TODO: [OPTIMIZATION] This function performs multiple separate queries that could |
297 | | - * potentially be optimized or consolidated for better performance. |
298 | | - */ |
299 | | - searchAll: async (_query, args, context, resolveInfo) => { |
300 | | - const { searchTerm, limit, heightFilter } = args; |
301 | | - const { rootPgPool } = context; |
302 | | - |
303 | | - // Query to search for blocks by various hash fields |
304 | | - const blocksQuery = ` |
305 | | - SELECT * FROM public."Blocks" WHERE LOWER(hash) = LOWER($1) |
306 | | - UNION ALL |
307 | | - SELECT * FROM public."Blocks" WHERE LOWER(parent) = LOWER($1) |
308 | | - UNION ALL |
309 | | - SELECT * FROM public."Blocks" WHERE LOWER("payloadHash") = LOWER($1) |
310 | | - UNION ALL |
311 | | - SELECT * FROM public."Blocks" WHERE LOWER("transactionsHash") = LOWER($1) |
312 | | - UNION ALL |
313 | | - SELECT * FROM public."Blocks" WHERE LOWER("outputsHash") = LOWER($1) |
314 | | - UNION ALL |
315 | | - SELECT * FROM public."Blocks" WHERE height = $3 |
316 | | - LIMIT $2; |
317 | | - `; |
318 | | - |
319 | | - // Query to search for transactions by various identifiers |
320 | | - const transactionsQuery = ` |
321 | | - SELECT * FROM public."Transactions" WHERE LOWER(requestkey) = LOWER($1) |
322 | | - UNION ALL |
323 | | - SELECT * FROM public."Transactions" WHERE LOWER(txid) = LOWER($1) |
324 | | - UNION ALL |
325 | | - SELECT * FROM public."Transactions" WHERE LOWER(pactid) = LOWER($1) |
326 | | - UNION ALL |
327 | | - SELECT * FROM public."Transactions" WHERE LOWER(sender) = LOWER($1) |
328 | | - LIMIT $2; |
329 | | - `; |
330 | | - |
331 | | - // Query to search for accounts/addresses |
332 | | - const addressesQuery = ` |
333 | | - SELECT DISTINCT account, module, qualname, network, -1 as "chainId" |
334 | | - FROM public."Balances" |
335 | | - WHERE LOWER(account) = LOWER($1) |
336 | | - ORDER BY account ASC |
337 | | - LIMIT $2 |
338 | | - `; |
339 | | - |
340 | | - // Query to search for token contracts |
341 | | - const tokensQuery = ` |
342 | | - SELECT DISTINCT network, type, module, precision, -1 as "chainId" |
343 | | - FROM public."Contracts" |
344 | | - WHERE (type = 'fungible' OR type = 'poly-fungible') |
345 | | - AND LOWER(module) LIKE LOWER($1) |
346 | | - LIMIT $2 |
347 | | - `; |
348 | | - |
349 | | - // Execute all queries in parallel for better performance |
350 | | - const [blocks, transactions, addresses, tokens] = await Promise.all([ |
351 | | - rootPgPool.query(blocksQuery, [`${searchTerm}`, limit, heightFilter]), |
352 | | - rootPgPool.query(transactionsQuery, [`${searchTerm}`, limit]), |
353 | | - rootPgPool.query(addressesQuery, [`${searchTerm}`, limit]), |
354 | | - rootPgPool.query(tokensQuery, [`%${searchTerm}%`, limit]), |
355 | | - ]); |
356 | | - |
357 | | - // Combine and return the search results |
358 | | - return { |
359 | | - blocks: blocks.rows, |
360 | | - transactions: transactions.rows, |
361 | | - addresses: addresses.rows, |
362 | | - tokens: tokens.rows, |
363 | | - }; |
364 | | - }, |
365 | | - }, |
366 | | - }, |
367 | | - }; |
368 | | -}); |
369 | | - |
370 | 250 | export default Block; |
0 commit comments