UNPKG

@settlemint/sdk-thegraph

Version:

TheGraph integration module for SettleMint SDK, enabling querying and indexing of blockchain data through subgraphs

103 lines (101 loc) 3.38 kB
import { appendHeaders } from "@settlemint/sdk-utils/http"; import { ensureServer } from "@settlemint/sdk-utils/runtime"; import { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from "@settlemint/sdk-utils/validation"; import { initGraphQLTada, readFragment } from "gql.tada"; import { GraphQLClient } from "graphql-request"; import { z } from "zod/v4"; //#region src/thegraph.ts /** * Schema for validating client options for the TheGraph client. */ const ClientOptionsSchema = z.object({ instances: z.array(UrlOrPathSchema), accessToken: ApplicationAccessTokenSchema.optional(), subgraphName: z.string(), cache: z.enum([ "default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload" ]).optional() }); /** * Constructs the full URL for TheGraph GraphQL API based on the provided options * * @param options - The client options for configuring TheGraph client * @returns The complete GraphQL API URL as a string * @throws Will throw an error if no matching instance is found for the specified subgraph */ function getFullUrl(options) { const instance = options.instances.find((instance$1) => instance$1.endsWith(`/${options.subgraphName}`)); if (!instance) { throw new Error(`Instance for subgraph ${options.subgraphName} not found`); } return new URL(instance).toString(); } /** * Creates a TheGraph GraphQL client with proper type safety using gql.tada * * @param options - Configuration options for the client including instance URLs, * access token and subgraph name * @param clientOptions - Optional GraphQL client configuration options * @returns An object containing: * - client: The configured GraphQL client instance * - graphql: The initialized gql.tada function for type-safe queries * @throws Will throw an error if the options fail validation against ClientOptionsSchema * @example * import { createTheGraphClient } from '@settlemint/sdk-thegraph'; * import type { introspection } from '@schemas/the-graph-env-kits'; * import { createLogger, requestLogger } from '@settlemint/sdk-utils/logging'; * * const logger = createLogger(); * * const { client, graphql } = createTheGraphClient<{ * introspection: introspection; * disableMasking: true; * scalars: { * Bytes: string; * Int8: string; * BigInt: string; * BigDecimal: string; * Timestamp: string; * }; * }>({ * instances: JSON.parse(process.env.SETTLEMINT_THEGRAPH_SUBGRAPHS_ENDPOINTS || '[]'), * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN, * subgraphName: 'kits' * }, { * fetch: requestLogger(logger, "the-graph-kits", fetch) as typeof fetch, * }); * * // Making GraphQL queries * const query = graphql(` * query SearchAssets { * assets { * id * name * symbol * } * } * `); * * const result = await client.request(query); */ function createTheGraphClient(options, clientOptions) { ensureServer(); const validatedOptions = validate(ClientOptionsSchema, options); const graphql = initGraphQLTada(); const fullUrl = getFullUrl(validatedOptions); return { client: new GraphQLClient(fullUrl, { ...clientOptions, headers: appendHeaders(clientOptions?.headers, { "x-auth-token": validatedOptions.accessToken }) }), graphql }; } //#endregion export { ClientOptionsSchema, createTheGraphClient, readFragment }; //# sourceMappingURL=thegraph.js.map