@settlemint/sdk-thegraph
Version:
TheGraph integration module for SettleMint SDK, enabling querying and indexing of blockchain data through subgraphs
98 lines (96 loc) • 3.83 kB
TypeScript
/* SettleMint The Graph SDK - Indexing Protocol */
import { AbstractSetupSchema, FragmentOf, ResultOf, TadaDocumentNode, VariablesOf, initGraphQLTada, readFragment } from "gql.tada";
import { GraphQLClient, RequestDocument, RequestOptions, Variables } from "graphql-request";
import { z } from "zod";
//#region src/utils/pagination.d.ts
/**
* Creates a TheGraph client that supports pagination for list fields
*
* @param theGraphClient - The GraphQL client to use for requests
* @returns A TheGraph client that supports pagination for list fields
* @internal Used internally by createTheGraphClient
*/
declare function createTheGraphClientWithPagination(theGraphClient: Pick<GraphQLClient, "request">): {
readonly query: <TResult, TVariables extends Variables>(documentOrOptions: TadaDocumentNode<TResult, TVariables> | RequestDocument | RequestOptions<TVariables, TResult>, variablesRaw?: Omit<TVariables, "skip" | "first">, requestHeadersRaw?: HeadersInit) => Promise<TResult>;
};
//#endregion
//#region src/thegraph.d.ts
/**
* Type definition for GraphQL client configuration options
*/
type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];
/**
* Schema for validating client options for the TheGraph client.
*/
declare const ClientOptionsSchema: z.ZodObject<{
instances: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodString]>>;
accessToken: z.ZodOptional<z.ZodString>;
subgraphName: z.ZodString;
cache: z.ZodOptional<z.ZodEnum<{
default: "default";
"force-cache": "force-cache";
"no-cache": "no-cache";
"no-store": "no-store";
"only-if-cached": "only-if-cached";
reload: "reload";
}>>;
}, z.core.$strip>;
/**
* Type definition for client options derived from the ClientOptionsSchema
*/
type ClientOptions = z.infer<typeof ClientOptionsSchema>;
/**
* 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 @fetchAll {
* id
* name
* symbol
* }
* }
* `);
*
* const result = await client.request(query);
*/
declare function createTheGraphClient<const Setup extends AbstractSetupSchema>(options: ClientOptions, clientOptions?: RequestConfig): {
client: GraphQLClient;
graphql: initGraphQLTada<Setup>;
};
//#endregion
export { ClientOptions, ClientOptionsSchema, type FragmentOf, RequestConfig, type ResultOf, type VariablesOf, createTheGraphClient, createTheGraphClientWithPagination, readFragment };
//# sourceMappingURL=thegraph.d.ts.map