UNPKG

@settlemint/sdk-thegraph

Version:

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

133 lines (130 loc) 4.82 kB
//#region rolldown:runtime var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); //#endregion const __settlemint_sdk_utils_http = __toESM(require("@settlemint/sdk-utils/http")); const __settlemint_sdk_utils_runtime = __toESM(require("@settlemint/sdk-utils/runtime")); const __settlemint_sdk_utils_validation = __toESM(require("@settlemint/sdk-utils/validation")); const gql_tada = __toESM(require("gql.tada")); const graphql_request = __toESM(require("graphql-request")); const zod_v4 = __toESM(require("zod/v4")); //#region src/thegraph.ts /** * Schema for validating client options for the TheGraph client. */ const ClientOptionsSchema = zod_v4.z.object({ instances: zod_v4.z.array(__settlemint_sdk_utils_validation.UrlOrPathSchema), accessToken: __settlemint_sdk_utils_validation.ApplicationAccessTokenSchema.optional(), subgraphName: zod_v4.z.string(), cache: zod_v4.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) { (0, __settlemint_sdk_utils_runtime.ensureServer)(); const validatedOptions = (0, __settlemint_sdk_utils_validation.validate)(ClientOptionsSchema, options); const graphql = (0, gql_tada.initGraphQLTada)(); const fullUrl = getFullUrl(validatedOptions); return { client: new graphql_request.GraphQLClient(fullUrl, { ...clientOptions, headers: (0, __settlemint_sdk_utils_http.appendHeaders)(clientOptions?.headers, { "x-auth-token": validatedOptions.accessToken }) }), graphql }; } //#endregion exports.ClientOptionsSchema = ClientOptionsSchema; exports.createTheGraphClient = createTheGraphClient; Object.defineProperty(exports, 'readFragment', { enumerable: true, get: function () { return gql_tada.readFragment; } }); //# sourceMappingURL=thegraph.cjs.map