@settlemint/sdk-thegraph
Version:
TheGraph integration module for SettleMint SDK, enabling querying and indexing of blockchain data through subgraphs
42 lines • 1.45 kB
JavaScript
// src/thegraph.ts
import { ensureServer } from "@settlemint/sdk-utils/runtime";
import { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from "@settlemint/sdk-utils/validation";
import { initGraphQLTada } from "gql.tada";
import { GraphQLClient } from "graphql-request";
import { z } from "zod";
import { readFragment } from "gql.tada";
var ClientOptionsSchema = z.object({
instances: z.array(UrlOrPathSchema),
accessToken: ApplicationAccessTokenSchema,
subgraphName: z.string(),
cache: z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
});
function getFullUrl(options) {
const instance = options.instances.find((instance2) => instance2.endsWith(`/${options.subgraphName}`));
if (!instance) {
throw new Error(`Instance for subgraph ${options.subgraphName} not found`);
}
return new URL(instance).toString();
}
function createTheGraphClient(options, clientOptions) {
ensureServer();
const validatedOptions = validate(ClientOptionsSchema, options);
const graphql = initGraphQLTada();
const fullUrl = getFullUrl(validatedOptions);
return {
client: new GraphQLClient(fullUrl, {
...clientOptions,
headers: {
...clientOptions?.headers ?? {},
"x-auth-token": validatedOptions.accessToken
}
}),
graphql
};
}
export {
ClientOptionsSchema,
createTheGraphClient,
readFragment
};
//# sourceMappingURL=thegraph.mjs.map