@settlemint/sdk-thegraph
Version:
TheGraph integration module for SettleMint SDK, enabling querying and indexing of blockchain data through subgraphs
87 lines (84 loc) • 3.14 kB
text/typescript
import { AbstractSetupSchema, initGraphQLTada } from 'gql.tada';
export { FragmentOf, ResultOf, VariablesOf, readFragment } from 'gql.tada';
import { GraphQLClient } from 'graphql-request';
import { z } from 'zod';
/**
* 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<[z.ZodString, z.ZodString]>, "many">;
accessToken: z.ZodString;
subgraphName: z.ZodString;
cache: z.ZodOptional<z.ZodEnum<["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]>>;
}, "strip", z.ZodTypeAny, {
instances: string[];
accessToken: string;
subgraphName: string;
cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
}, {
instances: string[];
accessToken: string;
subgraphName: string;
cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
}>;
/**
* 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 {
* 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>;
};
export { type ClientOptions, ClientOptionsSchema, type RequestConfig, createTheGraphClient };