UNPKG

@ensnode/ponder-subgraph

Version:

A Hono middleware for generating Subgraph-compatible GraphQL schema.

94 lines (88 loc) 3.31 kB
import * as hono_types from 'hono/types'; import { NodePgDatabase } from 'drizzle-orm/node-postgres'; import { PgTable, TableConfig } from 'drizzle-orm/pg-core'; type Schema = { [name: string]: unknown; }; type Drizzle<TSchema extends Schema = Schema> = NodePgDatabase<TSchema>; interface SubgraphMetaBlockInfo { /** Block number */ number: number; /** Block unix timestamp */ timestamp: number; /** Block hash */ hash: `0x${string}` | null; /** Block parent hash */ parentHash: `0x${string}` | null; } /** * The metadata provider interface used to fetch data from the application layer. */ interface _SubgraphMeta { /** * Unique ID to be used as a deployment ID in `_meta.deployment`. */ deployment: string; /** * Get last indexed block status * @returns The last indexed block status */ block: SubgraphMetaBlockInfo; /** * Get the indexing errors status * @returns The indexing errors status */ hasIndexingErrors: boolean; } type SubgraphMeta = null | _SubgraphMeta; /** * Hono Variables type for Subgraph _meta Support */ type SubgraphMetaVariables = { _meta: SubgraphMeta | undefined; }; /** biome-ignore-all lint/suspicious/noExplicitAny: allow */ /** biome-ignore-all lint/style/noNonNullAssertion: allow */ /** * This is a graphql schema generated from a drizzle (sql) schema, initially based on ponder's. * https://github.com/ponder-sh/ponder/blob/main/packages/core/src/graphql/index.ts * * Its goal is to mimic the subgraph graphql api for queries we've deemed relevant (see docs). * * 1. inlines some ponder internal types * 2. implement subgraph's simpler offset pagination with first & skip w/out Page types * 3. PascalCase entity names * 4. Polymorphic Interfaces * 5. lower-case and/or filters * 6. relation id shorthand filters (i.e. domains(where: { owner_id: String })) * 7. sortable id columns * 8. temporarily ignores column normalization that was fixed in * https://github.com/ponder-sh/ponder/pull/1517/files */ /** * the following type describes: * 1. `types` — mapping a polymorphic type name to the set of entities that implement that interface * ex: DomainEvent -> [TransferEvent, ...] * 2. `fields` — mapping a typeName to the polymorphic type it represents * ex: Domain.events -> DomainEvent * * NOTE: in future implementations of ponder, this information could be provided by the schema * using materialized views, and most/all of this code can be removed. */ interface PolymorphicConfig { types: Record<string, PgTable<TableConfig>[]>; fields: Record<string, string>; } interface BuildGraphQLSchemaOptions { schema: Schema; polymorphicConfig?: PolymorphicConfig; } declare function subgraphGraphQLMiddleware({ databaseUrl, databaseSchema, schema, polymorphicConfig, }: BuildGraphQLSchemaOptions & { databaseUrl: string; databaseSchema: string; }, { maxOperationTokens, maxOperationDepth, maxOperationAliases, }?: { maxOperationTokens?: number; maxOperationDepth?: number; maxOperationAliases?: number; }): hono_types.MiddlewareHandler<any, string, {}, Response>; export { type Drizzle, type Schema, type SubgraphMeta, type SubgraphMetaVariables, type _SubgraphMeta, subgraphGraphQLMiddleware };