@settlemint/sdk-thegraph 2.5.3-pre4cee80a → 2.5.3-prfa3ab2c1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/thegraph.d.ts +9 -1
- package/dist/browser/thegraph.js +18 -10
- package/dist/browser/thegraph.js.map +1 -1
- package/dist/thegraph.cjs +18 -10
- package/dist/thegraph.cjs.map +1 -1
- package/dist/thegraph.d.cts +9 -1
- package/dist/thegraph.d.ts +9 -1
- package/dist/thegraph.js +18 -10
- package/dist/thegraph.js.map +1 -1
- package/package.json +2 -2
|
@@ -4,6 +4,14 @@ import { GraphQLClient, Variables } from "graphql-request";
|
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
|
|
6
6
|
//#region src/utils/pagination.d.ts
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates a TheGraph client that supports pagination for list fields
|
|
10
|
+
*
|
|
11
|
+
* @param theGraphClient - The GraphQL client to use for requests
|
|
12
|
+
* @returns A TheGraph client that supports pagination for list fields
|
|
13
|
+
* @internal Used internally by createTheGraphClient
|
|
14
|
+
*/
|
|
7
15
|
declare function createTheGraphClientWithPagination(theGraphClient: Pick<GraphQLClient, "request">): {
|
|
8
16
|
readonly query: <TResult, TVariables extends Variables>(document: TadaDocumentNode<TResult, TVariables>, variables: Omit<TVariables, "skip" | "first">) => Promise<TResult>;
|
|
9
17
|
};
|
|
@@ -71,7 +79,7 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
|
|
|
71
79
|
* // Making GraphQL queries
|
|
72
80
|
* const query = graphql(`
|
|
73
81
|
* query SearchAssets {
|
|
74
|
-
* assets {
|
|
82
|
+
* assets @fetchAll {
|
|
75
83
|
* id
|
|
76
84
|
* name
|
|
77
85
|
* symbol
|
package/dist/browser/thegraph.js
CHANGED
|
@@ -81,7 +81,7 @@ function customMerge(target, source) {
|
|
|
81
81
|
}
|
|
82
82
|
return result;
|
|
83
83
|
}
|
|
84
|
-
function
|
|
84
|
+
function extractFetchAllFields(document, variables, fetchAllFields) {
|
|
85
85
|
const fields = [];
|
|
86
86
|
const pathStack = [];
|
|
87
87
|
visit(document, { Field: {
|
|
@@ -133,9 +133,7 @@ function extractListFields(document, variables, fetchAllFields) {
|
|
|
133
133
|
}
|
|
134
134
|
const fieldIdentifierForDirective = node.alias?.value || node.name.value;
|
|
135
135
|
const hasFetchAllDirective = fetchAllFields?.has(fieldIdentifierForDirective);
|
|
136
|
-
|
|
137
|
-
const shouldPaginate = hasPaginationArgs || hasFetchAllDirective;
|
|
138
|
-
if (shouldPaginate) {
|
|
136
|
+
if (hasFetchAllDirective) {
|
|
139
137
|
const parentFetchAllField = fields.find((field) => pathStack.join(",").startsWith(field.path.join(",")));
|
|
140
138
|
if (parentFetchAllField) {
|
|
141
139
|
throw new Error(`Nesting of @fetchAll directive is not supported: ${pathStack.join(".")} is a child of ${parentFetchAllField.path.join(".")}`);
|
|
@@ -244,6 +242,13 @@ function filterVariables(variables, document) {
|
|
|
244
242
|
}
|
|
245
243
|
return isEmpty(filtered) ? undefined : filtered;
|
|
246
244
|
}
|
|
245
|
+
/**
|
|
246
|
+
* Creates a TheGraph client that supports pagination for list fields
|
|
247
|
+
*
|
|
248
|
+
* @param theGraphClient - The GraphQL client to use for requests
|
|
249
|
+
* @returns A TheGraph client that supports pagination for list fields
|
|
250
|
+
* @internal Used internally by createTheGraphClient
|
|
251
|
+
*/
|
|
247
252
|
function createTheGraphClientWithPagination(theGraphClient) {
|
|
248
253
|
async function executeListFieldPagination(document, variables, field) {
|
|
249
254
|
const results = [];
|
|
@@ -282,7 +287,7 @@ function createTheGraphClientWithPagination(theGraphClient) {
|
|
|
282
287
|
}
|
|
283
288
|
return { async query(document, variables) {
|
|
284
289
|
const { document: processedDocument, fetchAllFields } = stripFetchAllDirective(document);
|
|
285
|
-
const listFields =
|
|
290
|
+
const listFields = extractFetchAllFields(processedDocument, variables, fetchAllFields);
|
|
286
291
|
if (listFields.length === 0) {
|
|
287
292
|
return theGraphClient.request(processedDocument, variables);
|
|
288
293
|
}
|
|
@@ -376,7 +381,7 @@ function getFullUrl(options) {
|
|
|
376
381
|
* // Making GraphQL queries
|
|
377
382
|
* const query = graphql(`
|
|
378
383
|
* query SearchAssets {
|
|
379
|
-
* assets {
|
|
384
|
+
* assets @fetchAll {
|
|
380
385
|
* id
|
|
381
386
|
* name
|
|
382
387
|
* symbol
|
|
@@ -391,11 +396,14 @@ function createTheGraphClient(options, clientOptions) {
|
|
|
391
396
|
const validatedOptions = validate(ClientOptionsSchema, options);
|
|
392
397
|
const graphql = initGraphQLTada();
|
|
393
398
|
const fullUrl = getFullUrl(validatedOptions);
|
|
399
|
+
const client = new GraphQLClient(fullUrl, {
|
|
400
|
+
...clientOptions,
|
|
401
|
+
headers: appendHeaders(clientOptions?.headers, { "x-auth-token": validatedOptions.accessToken })
|
|
402
|
+
});
|
|
403
|
+
const paginatedClient = createTheGraphClientWithPagination(client);
|
|
404
|
+
client.request = paginatedClient.query;
|
|
394
405
|
return {
|
|
395
|
-
client
|
|
396
|
-
...clientOptions,
|
|
397
|
-
headers: appendHeaders(clientOptions?.headers, { "x-auth-token": validatedOptions.accessToken })
|
|
398
|
-
}),
|
|
406
|
+
client,
|
|
399
407
|
graphql
|
|
400
408
|
};
|
|
401
409
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"thegraph.js","names":["document: DocumentNode","target: unknown","source: unknown","result: Record<string, unknown>","variables?: Variables","fetchAllFields?: Set<string>","fields: ListField[]","pathStack: string[]","firstValue: number | undefined","skipValue: number | undefined","otherArgs: ArgumentNode[]","targetField: ListField","skip: number","first: number","newArgs: ArgumentNode[]","listFields: ListField[]","variables: Variables | undefined","filtered: Variables","theGraphClient: Pick<GraphQLClient, \"request\">","field: ListField","results: unknown[]","document: TadaDocumentNode<TResult, TVariables>","variables: Omit<TVariables, \"skip\" | \"first\">","options: ClientOptions","instance","clientOptions?: RequestConfig"],"sources":["../../src/utils/pagination.ts","../../src/thegraph.ts"],"sourcesContent":["import { sortBy } from \"es-toolkit\";\nimport { get, isArray, isEmpty, set } from \"es-toolkit/compat\";\nimport type { TadaDocumentNode } from \"gql.tada\";\nimport { type ArgumentNode, type DocumentNode, Kind, type SelectionNode, visit } from \"graphql\";\nimport type { GraphQLClient, Variables } from \"graphql-request\";\n\n// Constants for TheGraph limits\nconst THE_GRAPH_LIMIT = 500;\nconst FIRST_ARG = \"first\";\nconst SKIP_ARG = \"skip\";\nconst FETCH_ALL_DIRECTIVE = \"fetchAll\";\n\ninterface ListField {\n path: string[];\n fieldName: string;\n alias?: string;\n hasFirstArg: boolean;\n firstValue?: number;\n skipValue?: number;\n otherArgs: ArgumentNode[];\n selections?: ReadonlyArray<SelectionNode>;\n hasFetchAllDirective?: boolean;\n firstValueIsDefault?: boolean; // Track if first value was defaulted\n}\n\n/**\n * Detects and strips @fetchAll directives from a GraphQL document\n *\n * @param {DocumentNode} document - The GraphQL document to process\n * @returns {Object} Processed document and list of fields with @fetchAll\n *\n * @remarks\n * This function:\n * - Identifies fields decorated with @fetchAll directive\n * - Removes the directive from the AST (The Graph doesn't recognize it)\n * - Returns both the cleaned document and a list of fields to auto-paginate\n */\nfunction stripFetchAllDirective(document: DocumentNode): {\n document: DocumentNode;\n fetchAllFields: Set<string>;\n} {\n const fetchAllFields = new Set<string>();\n\n const strippedDocument = visit(document, {\n Field(node) {\n // Check if this field has the @fetchAll directive\n if (node.directives && node.directives.length > 0) {\n const hasFetchAll = node.directives.some((dir) => dir.name.value === FETCH_ALL_DIRECTIVE);\n\n if (hasFetchAll) {\n const fieldIdentifier = node.alias?.value || node.name.value;\n fetchAllFields.add(fieldIdentifier);\n\n // Return a new node without the @fetchAll directive\n return {\n ...node,\n directives: node.directives.filter((dir) => dir.name.value !== FETCH_ALL_DIRECTIVE),\n };\n }\n }\n\n return node;\n },\n });\n\n return {\n document: strippedDocument,\n fetchAllFields,\n };\n}\n\n/**\n * Custom merge function for deep object merging with special handling for lists\n *\n * @param {unknown} target - The target object or value to merge into\n * @param {unknown} source - The source object or value to merge from\n * @returns {unknown} Merged result with preservation of arrays and specific merge logic\n *\n * @remarks\n * Key behaviors:\n * - Preserves existing arrays without merging\n * - Handles null and undefined values\n * - Performs deep merge for nested objects\n * - Prioritizes source values for primitives\n *\n */\nfunction customMerge(target: unknown, source: unknown): unknown {\n if (source == null) return target;\n if (target == null) return source;\n\n // Preserve existing arrays (paginated data) - don't merge them\n if (isArray(target) || isArray(source)) {\n return target;\n }\n\n if (typeof target !== \"object\" || typeof source !== \"object\") {\n return source;\n }\n\n // Manually merge objects to ensure arrays are preserved\n const targetObj = target as Record<string, unknown>;\n const sourceObj = source as Record<string, unknown>;\n const result: Record<string, unknown> = { ...targetObj };\n\n for (const key in sourceObj) {\n if (Object.hasOwn(sourceObj, key)) {\n result[key] = key in result ? customMerge(result[key], sourceObj[key]) : sourceObj[key];\n }\n }\n\n return result;\n}\n\n// Extract all list fields (queries that have pagination arguments or @fetchAll)\nfunction extractListFields(document: DocumentNode, variables?: Variables, fetchAllFields?: Set<string>): ListField[] {\n const fields: ListField[] = [];\n const pathStack: string[] = [];\n\n visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Skip meta fields\n if (node.name.value.startsWith(\"__\")) {\n return;\n }\n\n // Check if this field has pagination arguments (first or skip)\n let hasFirstArg = false;\n let hasSkipArg = false;\n let firstValue: number | undefined;\n let skipValue: number | undefined;\n let firstValueIsDefault = false;\n const otherArgs: ArgumentNode[] = [];\n\n if (node.arguments) {\n for (const arg of node.arguments) {\n if (arg.name.value === FIRST_ARG) {\n hasFirstArg = true;\n if (arg.value.kind === Kind.INT) {\n firstValue = Number.parseInt(arg.value.value);\n } else if (arg.value.kind === Kind.VARIABLE && variables) {\n const varName = arg.value.name.value;\n const varValue = (variables as Record<string, unknown>)[varName];\n firstValue = typeof varValue === \"number\" ? varValue : undefined;\n // If variable is defined in query but not passed in input, check if it's a standard pagination variable\n if (firstValue === undefined && varName === \"first\") {\n hasFirstArg = true;\n firstValue = THE_GRAPH_LIMIT; // Default to THE_GRAPH_LIMIT\n firstValueIsDefault = true; // Mark that this was defaulted\n }\n }\n } else if (arg.name.value === SKIP_ARG) {\n hasSkipArg = true;\n if (arg.value.kind === Kind.INT) {\n skipValue = Number.parseInt(arg.value.value);\n } else if (arg.value.kind === Kind.VARIABLE && variables) {\n const varName = arg.value.name.value;\n const varValue = (variables as Record<string, unknown>)[varName];\n skipValue = typeof varValue === \"number\" ? varValue : undefined;\n // If variable is defined in query but not passed in input, check if it's a standard pagination variable\n if (skipValue === undefined && varName === \"skip\") {\n hasSkipArg = true;\n skipValue = 0; // Default to 0\n }\n }\n } else {\n otherArgs.push(arg);\n }\n }\n }\n\n // Check if this field has @fetchAll directive\n const fieldIdentifierForDirective = node.alias?.value || node.name.value;\n const hasFetchAllDirective = fetchAllFields?.has(fieldIdentifierForDirective);\n\n // Consider it a list field if it has pagination arguments OR @fetchAll\n const hasPaginationArgs = hasFirstArg || hasSkipArg;\n const shouldPaginate = hasPaginationArgs || hasFetchAllDirective;\n\n if (shouldPaginate) {\n // Do not allow nesting @fetchAll fields\n const parentFetchAllField = fields.find((field) => pathStack.join(\",\").startsWith(field.path.join(\",\")));\n if (parentFetchAllField) {\n throw new Error(\n `Nesting of @fetchAll directive is not supported: ${pathStack.join(\".\")} is a child of ${parentFetchAllField.path.join(\".\")}`,\n );\n }\n fields.push({\n path: [...pathStack],\n fieldName: node.name.value,\n alias: node.alias?.value,\n hasFirstArg,\n firstValue: hasFetchAllDirective && !hasFirstArg ? THE_GRAPH_LIMIT : firstValue,\n skipValue: hasFetchAllDirective && !hasSkipArg ? 0 : skipValue,\n otherArgs,\n selections: node.selectionSet?.selections,\n hasFetchAllDirective,\n firstValueIsDefault,\n });\n }\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n\n return fields;\n}\n\n// Create a query for a single field with specific pagination\nfunction createSingleFieldQuery(\n document: DocumentNode,\n targetField: ListField,\n skip: number,\n first: number,\n): DocumentNode {\n const targetPath = [...targetField.path];\n const pathStack: string[] = [];\n\n return visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Check if we're on the path to target field\n const onPath = pathStack.every((segment, i) => i >= targetPath.length || segment === targetPath[i]);\n\n if (!onPath) {\n pathStack.pop();\n return null; // Remove fields not on path\n }\n\n // If this is our target field, update pagination\n const isTarget =\n pathStack.length === targetPath.length && pathStack.every((segment, i) => segment === targetPath[i]);\n\n if (isTarget) {\n const newArgs: ArgumentNode[] = [...targetField.otherArgs];\n\n // Add pagination arguments\n newArgs.push(\n {\n kind: Kind.ARGUMENT,\n name: { kind: Kind.NAME, value: FIRST_ARG },\n value: { kind: Kind.INT, value: first.toString() },\n },\n {\n kind: Kind.ARGUMENT,\n name: { kind: Kind.NAME, value: SKIP_ARG },\n value: { kind: Kind.INT, value: skip.toString() },\n },\n );\n\n return { ...node, arguments: newArgs };\n }\n\n return undefined;\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n}\n\n// Create query without list fields\nfunction createNonListQuery(document: DocumentNode, listFields: ListField[]): DocumentNode | null {\n let hasFields = false;\n const pathStack: string[] = [];\n\n const filtered = visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Check if this field is a list field\n const isList = listFields.some(\n (field) =>\n field.path.length === pathStack.length && field.path.every((segment, i) => segment === pathStack[i]),\n );\n\n if (isList) {\n pathStack.pop();\n return null;\n }\n\n hasFields = true;\n return undefined;\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n\n return hasFields ? filtered : null;\n}\n\n// Filter variables to only include used ones\nfunction filterVariables(variables: Variables | undefined, document: DocumentNode): Variables | undefined {\n if (!variables) return undefined;\n\n const usedVariables = new Set<string>();\n\n visit(document, {\n Variable: (node) => {\n usedVariables.add(node.name.value);\n },\n });\n\n const filtered: Variables = {};\n const varsObj = variables as Record<string, unknown>;\n for (const key of usedVariables) {\n if (key in varsObj) {\n (filtered as Record<string, unknown>)[key] = varsObj[key];\n }\n }\n\n return isEmpty(filtered) ? undefined : filtered;\n}\n\nexport function createTheGraphClientWithPagination(theGraphClient: Pick<GraphQLClient, \"request\">) {\n // Execute pagination for a list field\n async function executeListFieldPagination(\n document: DocumentNode,\n variables: Variables | undefined,\n field: ListField,\n ): Promise<unknown[]> {\n const results: unknown[] = [];\n let currentSkip = field.skipValue || 0;\n let hasMore = true;\n\n // For fields with pagination arguments, always attempt to fetch data\n // and continue if we get a full page (indicating more data might exist)\n const batchSize = Math.min(field.firstValue || THE_GRAPH_LIMIT, THE_GRAPH_LIMIT);\n\n while (hasMore) {\n const query = createSingleFieldQuery(document, field, currentSkip, batchSize);\n const existingVariables = filterVariables(variables, query) ?? {};\n const response = await theGraphClient.request(query, {\n ...existingVariables,\n first: batchSize,\n skip: currentSkip,\n });\n\n // Use array path format for es-toolkit's get function\n const data = get(response, field.path) ?? get(response, field.fieldName);\n\n const parentPath = field.path.slice(0, -1);\n const parentData = get(response, parentPath);\n if (isArray(parentData) && parentData.length > 0) {\n throw new Error(\n `Response is an array, but expected a single object for field ${parentPath.join(\".\")}. The @fetchAll directive is not supported inside a query that returns a list of items.`,\n );\n }\n\n if (isArray(data) && data.length > 0) {\n results.push(...data);\n\n // Continue fetching if:\n // 1. We have @fetchAll directive (fetch everything)\n // 2. We have an explicit first value > THE_GRAPH_LIMIT and haven't reached it\n // 3. We have a defaulted first value and got a full batch (treating it as \"no explicit value\")\n // 4. We have no first value and got a full batch\n if (field.hasFetchAllDirective) {\n // With @fetchAll, continue if we got a full batch\n hasMore = data.length === batchSize;\n } else if (field.firstValue && !field.firstValueIsDefault) {\n // With explicit first value (not defaulted), only continue if:\n // - We haven't reached the requested amount yet\n // - We got a full batch (indicating more data might exist)\n hasMore = data.length === batchSize && results.length < field.firstValue;\n } else {\n // When first is not specified or was defaulted (using default batch size),\n // continue if we got a full batch (standard TheGraph pagination behavior)\n hasMore = data.length === batchSize;\n }\n } else {\n hasMore = false;\n }\n\n currentSkip += batchSize;\n }\n\n return results;\n }\n\n return {\n async query<TResult, TVariables extends Variables>(\n document: TadaDocumentNode<TResult, TVariables>,\n variables: Omit<TVariables, \"skip\" | \"first\">,\n ): Promise<TResult> {\n // First, detect and strip @fetchAll directives\n const { document: processedDocument, fetchAllFields } = stripFetchAllDirective(document);\n\n // Extract all list fields (including those with @fetchAll)\n const listFields = extractListFields(processedDocument, variables, fetchAllFields);\n\n // If no list fields, execute normally\n if (listFields.length === 0) {\n return theGraphClient.request(processedDocument, variables as Variables);\n }\n\n // Execute paginated queries for all list fields\n const result: Record<string, unknown> = {};\n\n // Sort fields by depth to handle nested fields correctly\n const sortedFields = sortBy(listFields, [(field) => field.path.length]);\n\n // Process list fields in parallel for better performance\n const fieldDataPromises = sortedFields.map(async (field) => ({\n field,\n data: await executeListFieldPagination(processedDocument, variables, field),\n }));\n\n const fieldResults = await Promise.all(fieldDataPromises);\n\n // Set results in order\n for (const { field, data } of fieldResults) {\n // Use array path format for es-toolkit's set function\n set(result, field.path, data);\n }\n\n // Execute non-list fields (single entity queries)\n const nonListQuery = createNonListQuery(processedDocument, listFields);\n\n if (nonListQuery) {\n const nonListResult = await theGraphClient.request(\n nonListQuery,\n filterVariables(variables, nonListQuery) ?? {},\n );\n\n // Merge results, preserving list data\n const merged = customMerge(result, nonListResult);\n return merged as TResult;\n }\n\n return result as TResult;\n },\n } as const;\n}\n","import { appendHeaders } from \"@settlemint/sdk-utils/http\";\nimport { ensureServer } from \"@settlemint/sdk-utils/runtime\";\nimport { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from \"@settlemint/sdk-utils/validation\";\nimport { type AbstractSetupSchema, initGraphQLTada } from \"gql.tada\";\nimport { GraphQLClient } from \"graphql-request\";\nimport { z } from \"zod\";\n\n/**\n * Type definition for GraphQL client configuration options\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating client options for the TheGraph client.\n */\nexport const ClientOptionsSchema = z.object({\n instances: z.array(UrlOrPathSchema),\n accessToken: ApplicationAccessTokenSchema.optional(),\n subgraphName: z.string(),\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n});\n\n/**\n * Type definition for client options derived from the ClientOptionsSchema\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Constructs the full URL for TheGraph GraphQL API based on the provided options\n *\n * @param options - The client options for configuring TheGraph client\n * @returns The complete GraphQL API URL as a string\n * @throws Will throw an error if no matching instance is found for the specified subgraph\n */\nfunction getFullUrl(options: ClientOptions): string {\n const instance = options.instances.find((instance) => instance.endsWith(`/${options.subgraphName}`));\n if (!instance) {\n throw new Error(`Instance for subgraph ${options.subgraphName} not found`);\n }\n return new URL(instance).toString();\n}\n\n/**\n * Creates a TheGraph GraphQL client with proper type safety using gql.tada\n *\n * @param options - Configuration options for the client including instance URLs,\n * access token and subgraph name\n * @param clientOptions - Optional GraphQL client configuration options\n * @returns An object containing:\n * - client: The configured GraphQL client instance\n * - graphql: The initialized gql.tada function for type-safe queries\n * @throws Will throw an error if the options fail validation against ClientOptionsSchema\n * @example\n * import { createTheGraphClient } from '@settlemint/sdk-thegraph';\n * import type { introspection } from '@schemas/the-graph-env-kits';\n * import { createLogger, requestLogger } from '@settlemint/sdk-utils/logging';\n *\n * const logger = createLogger();\n *\n * const { client, graphql } = createTheGraphClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * Bytes: string;\n * Int8: string;\n * BigInt: string;\n * BigDecimal: string;\n * Timestamp: string;\n * };\n * }>({\n * instances: JSON.parse(process.env.SETTLEMINT_THEGRAPH_SUBGRAPHS_ENDPOINTS || '[]'),\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN,\n * subgraphName: 'kits'\n * }, {\n * fetch: requestLogger(logger, \"the-graph-kits\", fetch) as typeof fetch,\n * });\n *\n * // Making GraphQL queries\n * const query = graphql(`\n * query SearchAssets {\n * assets {\n * id\n * name\n * symbol\n * }\n * }\n * `);\n *\n * const result = await client.request(query);\n */\nexport function createTheGraphClient<const Setup extends AbstractSetupSchema>(\n options: ClientOptions,\n clientOptions?: RequestConfig,\n): {\n client: GraphQLClient;\n graphql: initGraphQLTada<Setup>;\n} {\n ensureServer();\n const validatedOptions = validate(ClientOptionsSchema, options);\n const graphql = initGraphQLTada<Setup>();\n const fullUrl = getFullUrl(validatedOptions);\n\n return {\n client: new GraphQLClient(fullUrl, {\n ...clientOptions,\n headers: appendHeaders(clientOptions?.headers, { \"x-auth-token\": validatedOptions.accessToken }),\n }),\n graphql,\n };\n}\n\nexport type { FragmentOf, ResultOf, VariablesOf } from \"gql.tada\";\nexport { readFragment } from \"gql.tada\";\nexport { createTheGraphClientWithPagination } from \"./utils/pagination.js\";\n"],"mappings":";;;;;;;;;;;;AAOA,MAAM,kBAAkB;AACxB,MAAM,YAAY;AAClB,MAAM,WAAW;AACjB,MAAM,sBAAsB;;;;;;;;;;;;;AA2B5B,SAAS,uBAAuBA,UAG9B;CACA,MAAM,iBAAiB,IAAI;CAE3B,MAAM,mBAAmB,MAAM,UAAU,EACvC,MAAM,MAAM;AAEV,MAAI,KAAK,cAAc,KAAK,WAAW,SAAS,GAAG;GACjD,MAAM,cAAc,KAAK,WAAW,KAAK,CAAC,QAAQ,IAAI,KAAK,UAAU,oBAAoB;AAEzF,OAAI,aAAa;IACf,MAAM,kBAAkB,KAAK,OAAO,SAAS,KAAK,KAAK;AACvD,mBAAe,IAAI,gBAAgB;AAGnC,WAAO;KACL,GAAG;KACH,YAAY,KAAK,WAAW,OAAO,CAAC,QAAQ,IAAI,KAAK,UAAU,oBAAoB;IACpF;GACF;EACF;AAED,SAAO;CACR,EACF,EAAC;AAEF,QAAO;EACL,UAAU;EACV;CACD;AACF;;;;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nC,aAAa,6BAA6B,UAAU;CACpD,cAAc,EAAE,QAAQ;CACxB,OAAO,EAAE,KAAK;EAAC;EAAW;EAAe;EAAY;EAAY;EAAkB;CAAS,EAAC,CAAC,UAAU;AACzG,EAAC;;;;;;;;AAcF,SAAS,WAAWoB,SAAgC;CAClD,MAAM,WAAW,QAAQ,UAAU,KAAK,CAACC,eAAa,WAAS,UAAU,GAAG,QAAQ,aAAa,EAAE,CAAC;AACpG,MAAK,UAAU;AACb,QAAM,IAAI,OAAO,wBAAwB,QAAQ,aAAa;CAC/D;AACD,QAAO,IAAI,IAAI,UAAU,UAAU;AACpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkDD,SAAgB,qBACdD,SACAE,eAIA;AACA,eAAc;CACd,MAAM,mBAAmB,SAAS,qBAAqB,QAAQ;CAC/D,MAAM,UAAU,iBAAwB;CACxC,MAAM,UAAU,WAAW,iBAAiB;AAE5C,QAAO;EACL,QAAQ,IAAI,cAAc,SAAS;GACjC,GAAG;GACH,SAAS,cAAc,eAAe,SAAS,EAAE,gBAAgB,iBAAiB,YAAa,EAAC;EACjG;EACD;CACD;AACF"}
|
|
1
|
+
{"version":3,"file":"thegraph.js","names":["document: DocumentNode","target: unknown","source: unknown","result: Record<string, unknown>","variables?: Variables","fetchAllFields?: Set<string>","fields: ListField[]","pathStack: string[]","firstValue: number | undefined","skipValue: number | undefined","otherArgs: ArgumentNode[]","targetField: ListField","skip: number","first: number","newArgs: ArgumentNode[]","listFields: ListField[]","variables: Variables | undefined","filtered: Variables","theGraphClient: Pick<GraphQLClient, \"request\">","field: ListField","results: unknown[]","document: TadaDocumentNode<TResult, TVariables>","variables: Omit<TVariables, \"skip\" | \"first\">","options: ClientOptions","instance","clientOptions?: RequestConfig"],"sources":["../../src/utils/pagination.ts","../../src/thegraph.ts"],"sourcesContent":["import { sortBy } from \"es-toolkit\";\nimport { get, isArray, isEmpty, set } from \"es-toolkit/compat\";\nimport type { TadaDocumentNode } from \"gql.tada\";\nimport { type ArgumentNode, type DocumentNode, Kind, type SelectionNode, visit } from \"graphql\";\nimport type { GraphQLClient, Variables } from \"graphql-request\";\n\n// Constants for TheGraph limits\nconst THE_GRAPH_LIMIT = 500;\nconst FIRST_ARG = \"first\";\nconst SKIP_ARG = \"skip\";\nconst FETCH_ALL_DIRECTIVE = \"fetchAll\";\n\ninterface ListField {\n path: string[];\n fieldName: string;\n alias?: string;\n hasFirstArg: boolean;\n firstValue?: number;\n skipValue?: number;\n otherArgs: ArgumentNode[];\n selections?: ReadonlyArray<SelectionNode>;\n hasFetchAllDirective?: boolean;\n firstValueIsDefault?: boolean; // Track if first value was defaulted\n}\n\n/**\n * Detects and strips @fetchAll directives from a GraphQL document\n *\n * @param {DocumentNode} document - The GraphQL document to process\n * @returns {Object} Processed document and list of fields with @fetchAll\n *\n * @remarks\n * This function:\n * - Identifies fields decorated with @fetchAll directive\n * - Removes the directive from the AST (The Graph doesn't recognize it)\n * - Returns both the cleaned document and a list of fields to auto-paginate\n */\nfunction stripFetchAllDirective(document: DocumentNode): {\n document: DocumentNode;\n fetchAllFields: Set<string>;\n} {\n const fetchAllFields = new Set<string>();\n\n const strippedDocument = visit(document, {\n Field(node) {\n // Check if this field has the @fetchAll directive\n if (node.directives && node.directives.length > 0) {\n const hasFetchAll = node.directives.some((dir) => dir.name.value === FETCH_ALL_DIRECTIVE);\n\n if (hasFetchAll) {\n const fieldIdentifier = node.alias?.value || node.name.value;\n fetchAllFields.add(fieldIdentifier);\n\n // Return a new node without the @fetchAll directive\n return {\n ...node,\n directives: node.directives.filter((dir) => dir.name.value !== FETCH_ALL_DIRECTIVE),\n };\n }\n }\n\n return node;\n },\n });\n\n return {\n document: strippedDocument,\n fetchAllFields,\n };\n}\n\n/**\n * Custom merge function for deep object merging with special handling for lists\n *\n * @param {unknown} target - The target object or value to merge into\n * @param {unknown} source - The source object or value to merge from\n * @returns {unknown} Merged result with preservation of arrays and specific merge logic\n *\n * @remarks\n * Key behaviors:\n * - Preserves existing arrays without merging\n * - Handles null and undefined values\n * - Performs deep merge for nested objects\n * - Prioritizes source values for primitives\n *\n */\nfunction customMerge(target: unknown, source: unknown): unknown {\n if (source == null) return target;\n if (target == null) return source;\n\n // Preserve existing arrays (paginated data) - don't merge them\n if (isArray(target) || isArray(source)) {\n return target;\n }\n\n if (typeof target !== \"object\" || typeof source !== \"object\") {\n return source;\n }\n\n // Manually merge objects to ensure arrays are preserved\n const targetObj = target as Record<string, unknown>;\n const sourceObj = source as Record<string, unknown>;\n const result: Record<string, unknown> = { ...targetObj };\n\n for (const key in sourceObj) {\n if (Object.hasOwn(sourceObj, key)) {\n result[key] = key in result ? customMerge(result[key], sourceObj[key]) : sourceObj[key];\n }\n }\n\n return result;\n}\n\n// Extract all fields that have @fetchAll directive\nfunction extractFetchAllFields(\n document: DocumentNode,\n variables?: Variables,\n fetchAllFields?: Set<string>,\n): ListField[] {\n const fields: ListField[] = [];\n const pathStack: string[] = [];\n\n visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Skip meta fields\n if (node.name.value.startsWith(\"__\")) {\n return;\n }\n\n // Check if this field has pagination arguments (first or skip)\n let hasFirstArg = false;\n let hasSkipArg = false;\n let firstValue: number | undefined;\n let skipValue: number | undefined;\n let firstValueIsDefault = false;\n const otherArgs: ArgumentNode[] = [];\n\n if (node.arguments) {\n for (const arg of node.arguments) {\n if (arg.name.value === FIRST_ARG) {\n hasFirstArg = true;\n if (arg.value.kind === Kind.INT) {\n firstValue = Number.parseInt(arg.value.value);\n } else if (arg.value.kind === Kind.VARIABLE && variables) {\n const varName = arg.value.name.value;\n const varValue = (variables as Record<string, unknown>)[varName];\n firstValue = typeof varValue === \"number\" ? varValue : undefined;\n // If variable is defined in query but not passed in input, check if it's a standard pagination variable\n if (firstValue === undefined && varName === \"first\") {\n hasFirstArg = true;\n firstValue = THE_GRAPH_LIMIT; // Default to THE_GRAPH_LIMIT\n firstValueIsDefault = true; // Mark that this was defaulted\n }\n }\n } else if (arg.name.value === SKIP_ARG) {\n hasSkipArg = true;\n if (arg.value.kind === Kind.INT) {\n skipValue = Number.parseInt(arg.value.value);\n } else if (arg.value.kind === Kind.VARIABLE && variables) {\n const varName = arg.value.name.value;\n const varValue = (variables as Record<string, unknown>)[varName];\n skipValue = typeof varValue === \"number\" ? varValue : undefined;\n // If variable is defined in query but not passed in input, check if it's a standard pagination variable\n if (skipValue === undefined && varName === \"skip\") {\n hasSkipArg = true;\n skipValue = 0; // Default to 0\n }\n }\n } else {\n otherArgs.push(arg);\n }\n }\n }\n\n // Check if this field has @fetchAll directive\n const fieldIdentifierForDirective = node.alias?.value || node.name.value;\n const hasFetchAllDirective = fetchAllFields?.has(fieldIdentifierForDirective);\n\n if (hasFetchAllDirective) {\n // Do not allow nesting @fetchAll fields\n const parentFetchAllField = fields.find((field) => pathStack.join(\",\").startsWith(field.path.join(\",\")));\n if (parentFetchAllField) {\n throw new Error(\n `Nesting of @fetchAll directive is not supported: ${pathStack.join(\".\")} is a child of ${parentFetchAllField.path.join(\".\")}`,\n );\n }\n fields.push({\n path: [...pathStack],\n fieldName: node.name.value,\n alias: node.alias?.value,\n hasFirstArg,\n firstValue: hasFetchAllDirective && !hasFirstArg ? THE_GRAPH_LIMIT : firstValue,\n skipValue: hasFetchAllDirective && !hasSkipArg ? 0 : skipValue,\n otherArgs,\n selections: node.selectionSet?.selections,\n hasFetchAllDirective,\n firstValueIsDefault,\n });\n }\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n\n return fields;\n}\n\n// Create a query for a single field with specific pagination\nfunction createSingleFieldQuery(\n document: DocumentNode,\n targetField: ListField,\n skip: number,\n first: number,\n): DocumentNode {\n const targetPath = [...targetField.path];\n const pathStack: string[] = [];\n\n return visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Check if we're on the path to target field\n const onPath = pathStack.every((segment, i) => i >= targetPath.length || segment === targetPath[i]);\n\n if (!onPath) {\n pathStack.pop();\n return null; // Remove fields not on path\n }\n\n // If this is our target field, update pagination\n const isTarget =\n pathStack.length === targetPath.length && pathStack.every((segment, i) => segment === targetPath[i]);\n\n if (isTarget) {\n const newArgs: ArgumentNode[] = [...targetField.otherArgs];\n\n // Add pagination arguments\n newArgs.push(\n {\n kind: Kind.ARGUMENT,\n name: { kind: Kind.NAME, value: FIRST_ARG },\n value: { kind: Kind.INT, value: first.toString() },\n },\n {\n kind: Kind.ARGUMENT,\n name: { kind: Kind.NAME, value: SKIP_ARG },\n value: { kind: Kind.INT, value: skip.toString() },\n },\n );\n\n return { ...node, arguments: newArgs };\n }\n\n return undefined;\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n}\n\n// Create query without list fields\nfunction createNonListQuery(document: DocumentNode, listFields: ListField[]): DocumentNode | null {\n let hasFields = false;\n const pathStack: string[] = [];\n\n const filtered = visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Check if this field is a list field\n const isList = listFields.some(\n (field) =>\n field.path.length === pathStack.length && field.path.every((segment, i) => segment === pathStack[i]),\n );\n\n if (isList) {\n pathStack.pop();\n return null;\n }\n\n hasFields = true;\n return undefined;\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n\n return hasFields ? filtered : null;\n}\n\n// Filter variables to only include used ones\nfunction filterVariables(variables: Variables | undefined, document: DocumentNode): Variables | undefined {\n if (!variables) return undefined;\n\n const usedVariables = new Set<string>();\n\n visit(document, {\n Variable: (node) => {\n usedVariables.add(node.name.value);\n },\n });\n\n const filtered: Variables = {};\n const varsObj = variables as Record<string, unknown>;\n for (const key of usedVariables) {\n if (key in varsObj) {\n (filtered as Record<string, unknown>)[key] = varsObj[key];\n }\n }\n\n return isEmpty(filtered) ? undefined : filtered;\n}\n\n/**\n * Creates a TheGraph client that supports pagination for list fields\n *\n * @param theGraphClient - The GraphQL client to use for requests\n * @returns A TheGraph client that supports pagination for list fields\n * @internal Used internally by createTheGraphClient\n */\nexport function createTheGraphClientWithPagination(theGraphClient: Pick<GraphQLClient, \"request\">) {\n // Execute pagination for a list field\n async function executeListFieldPagination(\n document: DocumentNode,\n variables: Variables | undefined,\n field: ListField,\n ): Promise<unknown[]> {\n const results: unknown[] = [];\n let currentSkip = field.skipValue || 0;\n let hasMore = true;\n\n // For fields with pagination arguments, always attempt to fetch data\n // and continue if we get a full page (indicating more data might exist)\n const batchSize = Math.min(field.firstValue || THE_GRAPH_LIMIT, THE_GRAPH_LIMIT);\n\n while (hasMore) {\n const query = createSingleFieldQuery(document, field, currentSkip, batchSize);\n const existingVariables = filterVariables(variables, query) ?? {};\n const response = await theGraphClient.request(query, {\n ...existingVariables,\n first: batchSize,\n skip: currentSkip,\n });\n\n // Use array path format for es-toolkit's get function\n const data = get(response, field.path) ?? get(response, field.fieldName);\n\n const parentPath = field.path.slice(0, -1);\n const parentData = get(response, parentPath);\n if (isArray(parentData) && parentData.length > 0) {\n throw new Error(\n `Response is an array, but expected a single object for field ${parentPath.join(\".\")}. The @fetchAll directive is not supported inside a query that returns a list of items.`,\n );\n }\n\n if (isArray(data) && data.length > 0) {\n results.push(...data);\n\n // Continue fetching if:\n // 1. We have @fetchAll directive (fetch everything)\n // 2. We have an explicit first value > THE_GRAPH_LIMIT and haven't reached it\n // 3. We have a defaulted first value and got a full batch (treating it as \"no explicit value\")\n // 4. We have no first value and got a full batch\n if (field.hasFetchAllDirective) {\n // With @fetchAll, continue if we got a full batch\n hasMore = data.length === batchSize;\n } else if (field.firstValue && !field.firstValueIsDefault) {\n // With explicit first value (not defaulted), only continue if:\n // - We haven't reached the requested amount yet\n // - We got a full batch (indicating more data might exist)\n hasMore = data.length === batchSize && results.length < field.firstValue;\n } else {\n // When first is not specified or was defaulted (using default batch size),\n // continue if we got a full batch (standard TheGraph pagination behavior)\n hasMore = data.length === batchSize;\n }\n } else {\n hasMore = false;\n }\n\n currentSkip += batchSize;\n }\n\n return results;\n }\n\n return {\n async query<TResult, TVariables extends Variables>(\n document: TadaDocumentNode<TResult, TVariables>,\n variables: Omit<TVariables, \"skip\" | \"first\">,\n ): Promise<TResult> {\n // First, detect and strip @fetchAll directives\n const { document: processedDocument, fetchAllFields } = stripFetchAllDirective(document);\n\n // Extract all list fields (including those with @fetchAll)\n const listFields = extractFetchAllFields(processedDocument, variables, fetchAllFields);\n\n // If no list fields, execute normally\n if (listFields.length === 0) {\n return theGraphClient.request(processedDocument, variables as Variables);\n }\n\n // Execute paginated queries for all list fields\n const result: Record<string, unknown> = {};\n\n // Sort fields by depth to handle nested fields correctly\n const sortedFields = sortBy(listFields, [(field) => field.path.length]);\n\n // Process list fields in parallel for better performance\n const fieldDataPromises = sortedFields.map(async (field) => ({\n field,\n data: await executeListFieldPagination(processedDocument, variables, field),\n }));\n\n const fieldResults = await Promise.all(fieldDataPromises);\n\n // Set results in order\n for (const { field, data } of fieldResults) {\n // Use array path format for es-toolkit's set function\n set(result, field.path, data);\n }\n\n // Execute non-list fields (single entity queries)\n const nonListQuery = createNonListQuery(processedDocument, listFields);\n\n if (nonListQuery) {\n const nonListResult = await theGraphClient.request(\n nonListQuery,\n filterVariables(variables, nonListQuery) ?? {},\n );\n\n // Merge results, preserving list data\n const merged = customMerge(result, nonListResult);\n return merged as TResult;\n }\n\n return result as TResult;\n },\n } as const;\n}\n","import { appendHeaders } from \"@settlemint/sdk-utils/http\";\nimport { ensureServer } from \"@settlemint/sdk-utils/runtime\";\nimport { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from \"@settlemint/sdk-utils/validation\";\nimport { type AbstractSetupSchema, initGraphQLTada } from \"gql.tada\";\nimport { GraphQLClient } from \"graphql-request\";\nimport { z } from \"zod\";\nimport { createTheGraphClientWithPagination } from \"./utils/pagination.js\";\n\n/**\n * Type definition for GraphQL client configuration options\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating client options for the TheGraph client.\n */\nexport const ClientOptionsSchema = z.object({\n instances: z.array(UrlOrPathSchema),\n accessToken: ApplicationAccessTokenSchema.optional(),\n subgraphName: z.string(),\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n});\n\n/**\n * Type definition for client options derived from the ClientOptionsSchema\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Constructs the full URL for TheGraph GraphQL API based on the provided options\n *\n * @param options - The client options for configuring TheGraph client\n * @returns The complete GraphQL API URL as a string\n * @throws Will throw an error if no matching instance is found for the specified subgraph\n */\nfunction getFullUrl(options: ClientOptions): string {\n const instance = options.instances.find((instance) => instance.endsWith(`/${options.subgraphName}`));\n if (!instance) {\n throw new Error(`Instance for subgraph ${options.subgraphName} not found`);\n }\n return new URL(instance).toString();\n}\n\n/**\n * Creates a TheGraph GraphQL client with proper type safety using gql.tada\n *\n * @param options - Configuration options for the client including instance URLs,\n * access token and subgraph name\n * @param clientOptions - Optional GraphQL client configuration options\n * @returns An object containing:\n * - client: The configured GraphQL client instance\n * - graphql: The initialized gql.tada function for type-safe queries\n * @throws Will throw an error if the options fail validation against ClientOptionsSchema\n * @example\n * import { createTheGraphClient } from '@settlemint/sdk-thegraph';\n * import type { introspection } from '@schemas/the-graph-env-kits';\n * import { createLogger, requestLogger } from '@settlemint/sdk-utils/logging';\n *\n * const logger = createLogger();\n *\n * const { client, graphql } = createTheGraphClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * Bytes: string;\n * Int8: string;\n * BigInt: string;\n * BigDecimal: string;\n * Timestamp: string;\n * };\n * }>({\n * instances: JSON.parse(process.env.SETTLEMINT_THEGRAPH_SUBGRAPHS_ENDPOINTS || '[]'),\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN,\n * subgraphName: 'kits'\n * }, {\n * fetch: requestLogger(logger, \"the-graph-kits\", fetch) as typeof fetch,\n * });\n *\n * // Making GraphQL queries\n * const query = graphql(`\n * query SearchAssets {\n * assets @fetchAll {\n * id\n * name\n * symbol\n * }\n * }\n * `);\n *\n * const result = await client.request(query);\n */\nexport function createTheGraphClient<const Setup extends AbstractSetupSchema>(\n options: ClientOptions,\n clientOptions?: RequestConfig,\n): {\n client: GraphQLClient;\n graphql: initGraphQLTada<Setup>;\n} {\n ensureServer();\n const validatedOptions = validate(ClientOptionsSchema, options);\n const graphql = initGraphQLTada<Setup>();\n const fullUrl = getFullUrl(validatedOptions);\n\n const client = new GraphQLClient(fullUrl, {\n ...clientOptions,\n headers: appendHeaders(clientOptions?.headers, { \"x-auth-token\": validatedOptions.accessToken }),\n }) as GraphQLClient & {\n query: ReturnType<typeof createTheGraphClientWithPagination>;\n };\n const paginatedClient = createTheGraphClientWithPagination(client);\n client.request = paginatedClient.query as typeof client.request;\n return {\n client,\n graphql,\n };\n}\n\nexport type { FragmentOf, ResultOf, VariablesOf } from \"gql.tada\";\nexport { readFragment } from \"gql.tada\";\nexport { createTheGraphClientWithPagination } from \"./utils/pagination.js\";\n"],"mappings":";;;;;;;;;;;;AAOA,MAAM,kBAAkB;AACxB,MAAM,YAAY;AAClB,MAAM,WAAW;AACjB,MAAM,sBAAsB;;;;;;;;;;;;;AA2B5B,SAAS,uBAAuBA,UAG9B;CACA,MAAM,iBAAiB,IAAI;CAE3B,MAAM,mBAAmB,MAAM,UAAU,EACvC,MAAM,MAAM;AAEV,MAAI,KAAK,cAAc,KAAK,WAAW,SAAS,GAAG;GACjD,MAAM,cAAc,KAAK,WAAW,KAAK,CAAC,QAAQ,IAAI,KAAK,UAAU,oBAAoB;AAEzF,OAAI,aAAa;IACf,MAAM,kBAAkB,KAAK,OAAO,SAAS,KAAK,KAAK;AACvD,mBAAe,IAAI,gBAAgB;AAGnC,WAAO;KACL,GAAG;KACH,YAAY,KAAK,WAAW,OAAO,CAAC,QAAQ,IAAI,KAAK,UAAU,oBAAoB;IACpF;GACF;EACF;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AAgB;CACnC,aAAa,6BAA6B,UAAU;CACpD,cAAc,EAAE,QAAQ;CACxB,OAAO,EAAE,KAAK;EAAC;EAAW;EAAe;EAAY;EAAY;EAAkB;CAAS,EAAC,CAAC,UAAU;AACzG,EAAC;;;;;;;;AAcF,SAAS,WAAWoB,SAAgC;CAClD,MAAM,WAAW,QAAQ,UAAU,KAAK,CAACC,eAAa,WAAS,UAAU,GAAG,QAAQ,aAAa,EAAE,CAAC;AACpG,MAAK,UAAU;AACb,QAAM,IAAI,OAAO,wBAAwB,QAAQ,aAAa;CAC/D;AACD,QAAO,IAAI,IAAI,UAAU,UAAU;AACpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkDD,SAAgB,qBACdD,SACAE,eAIA;AACA,eAAc;CACd,MAAM,mBAAmB,SAAS,qBAAqB,QAAQ;CAC/D,MAAM,UAAU,iBAAwB;CACxC,MAAM,UAAU,WAAW,iBAAiB;CAE5C,MAAM,SAAS,IAAI,cAAc,SAAS;EACxC,GAAG;EACH,SAAS,cAAc,eAAe,SAAS,EAAE,gBAAgB,iBAAiB,YAAa,EAAC;CACjG;CAGD,MAAM,kBAAkB,mCAAmC,OAAO;AAClE,QAAO,UAAU,gBAAgB;AACjC,QAAO;EACL;EACA;CACD;AACF"}
|
package/dist/thegraph.cjs
CHANGED
|
@@ -104,7 +104,7 @@ function customMerge(target, source) {
|
|
|
104
104
|
}
|
|
105
105
|
return result;
|
|
106
106
|
}
|
|
107
|
-
function
|
|
107
|
+
function extractFetchAllFields(document, variables, fetchAllFields) {
|
|
108
108
|
const fields = [];
|
|
109
109
|
const pathStack = [];
|
|
110
110
|
(0, graphql.visit)(document, { Field: {
|
|
@@ -156,9 +156,7 @@ function extractListFields(document, variables, fetchAllFields) {
|
|
|
156
156
|
}
|
|
157
157
|
const fieldIdentifierForDirective = node.alias?.value || node.name.value;
|
|
158
158
|
const hasFetchAllDirective = fetchAllFields?.has(fieldIdentifierForDirective);
|
|
159
|
-
|
|
160
|
-
const shouldPaginate = hasPaginationArgs || hasFetchAllDirective;
|
|
161
|
-
if (shouldPaginate) {
|
|
159
|
+
if (hasFetchAllDirective) {
|
|
162
160
|
const parentFetchAllField = fields.find((field) => pathStack.join(",").startsWith(field.path.join(",")));
|
|
163
161
|
if (parentFetchAllField) {
|
|
164
162
|
throw new Error(`Nesting of @fetchAll directive is not supported: ${pathStack.join(".")} is a child of ${parentFetchAllField.path.join(".")}`);
|
|
@@ -267,6 +265,13 @@ function filterVariables(variables, document) {
|
|
|
267
265
|
}
|
|
268
266
|
return (0, es_toolkit_compat.isEmpty)(filtered) ? undefined : filtered;
|
|
269
267
|
}
|
|
268
|
+
/**
|
|
269
|
+
* Creates a TheGraph client that supports pagination for list fields
|
|
270
|
+
*
|
|
271
|
+
* @param theGraphClient - The GraphQL client to use for requests
|
|
272
|
+
* @returns A TheGraph client that supports pagination for list fields
|
|
273
|
+
* @internal Used internally by createTheGraphClient
|
|
274
|
+
*/
|
|
270
275
|
function createTheGraphClientWithPagination(theGraphClient) {
|
|
271
276
|
async function executeListFieldPagination(document, variables, field) {
|
|
272
277
|
const results = [];
|
|
@@ -305,7 +310,7 @@ function createTheGraphClientWithPagination(theGraphClient) {
|
|
|
305
310
|
}
|
|
306
311
|
return { async query(document, variables) {
|
|
307
312
|
const { document: processedDocument, fetchAllFields } = stripFetchAllDirective(document);
|
|
308
|
-
const listFields =
|
|
313
|
+
const listFields = extractFetchAllFields(processedDocument, variables, fetchAllFields);
|
|
309
314
|
if (listFields.length === 0) {
|
|
310
315
|
return theGraphClient.request(processedDocument, variables);
|
|
311
316
|
}
|
|
@@ -399,7 +404,7 @@ function getFullUrl(options) {
|
|
|
399
404
|
* // Making GraphQL queries
|
|
400
405
|
* const query = graphql(`
|
|
401
406
|
* query SearchAssets {
|
|
402
|
-
* assets {
|
|
407
|
+
* assets @fetchAll {
|
|
403
408
|
* id
|
|
404
409
|
* name
|
|
405
410
|
* symbol
|
|
@@ -414,11 +419,14 @@ function createTheGraphClient(options, clientOptions) {
|
|
|
414
419
|
const validatedOptions = (0, __settlemint_sdk_utils_validation.validate)(ClientOptionsSchema, options);
|
|
415
420
|
const graphql$1 = (0, gql_tada.initGraphQLTada)();
|
|
416
421
|
const fullUrl = getFullUrl(validatedOptions);
|
|
422
|
+
const client = new graphql_request.GraphQLClient(fullUrl, {
|
|
423
|
+
...clientOptions,
|
|
424
|
+
headers: (0, __settlemint_sdk_utils_http.appendHeaders)(clientOptions?.headers, { "x-auth-token": validatedOptions.accessToken })
|
|
425
|
+
});
|
|
426
|
+
const paginatedClient = createTheGraphClientWithPagination(client);
|
|
427
|
+
client.request = paginatedClient.query;
|
|
417
428
|
return {
|
|
418
|
-
client
|
|
419
|
-
...clientOptions,
|
|
420
|
-
headers: (0, __settlemint_sdk_utils_http.appendHeaders)(clientOptions?.headers, { "x-auth-token": validatedOptions.accessToken })
|
|
421
|
-
}),
|
|
429
|
+
client,
|
|
422
430
|
graphql: graphql$1
|
|
423
431
|
};
|
|
424
432
|
}
|
package/dist/thegraph.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"thegraph.cjs","names":["document: DocumentNode","target: unknown","source: unknown","result: Record<string, unknown>","variables?: Variables","fetchAllFields?: Set<string>","fields: ListField[]","pathStack: string[]","firstValue: number | undefined","skipValue: number | undefined","otherArgs: ArgumentNode[]","Kind","targetField: ListField","skip: number","first: number","newArgs: ArgumentNode[]","listFields: ListField[]","variables: Variables | undefined","filtered: Variables","theGraphClient: Pick<GraphQLClient, \"request\">","field: ListField","results: unknown[]","document: TadaDocumentNode<TResult, TVariables>","variables: Omit<TVariables, \"skip\" | \"first\">","UrlOrPathSchema","options: ClientOptions","instance","clientOptions?: RequestConfig","graphql","GraphQLClient"],"sources":["../src/utils/pagination.ts","../src/thegraph.ts"],"sourcesContent":["import { sortBy } from \"es-toolkit\";\nimport { get, isArray, isEmpty, set } from \"es-toolkit/compat\";\nimport type { TadaDocumentNode } from \"gql.tada\";\nimport { type ArgumentNode, type DocumentNode, Kind, type SelectionNode, visit } from \"graphql\";\nimport type { GraphQLClient, Variables } from \"graphql-request\";\n\n// Constants for TheGraph limits\nconst THE_GRAPH_LIMIT = 500;\nconst FIRST_ARG = \"first\";\nconst SKIP_ARG = \"skip\";\nconst FETCH_ALL_DIRECTIVE = \"fetchAll\";\n\ninterface ListField {\n path: string[];\n fieldName: string;\n alias?: string;\n hasFirstArg: boolean;\n firstValue?: number;\n skipValue?: number;\n otherArgs: ArgumentNode[];\n selections?: ReadonlyArray<SelectionNode>;\n hasFetchAllDirective?: boolean;\n firstValueIsDefault?: boolean; // Track if first value was defaulted\n}\n\n/**\n * Detects and strips @fetchAll directives from a GraphQL document\n *\n * @param {DocumentNode} document - The GraphQL document to process\n * @returns {Object} Processed document and list of fields with @fetchAll\n *\n * @remarks\n * This function:\n * - Identifies fields decorated with @fetchAll directive\n * - Removes the directive from the AST (The Graph doesn't recognize it)\n * - Returns both the cleaned document and a list of fields to auto-paginate\n */\nfunction stripFetchAllDirective(document: DocumentNode): {\n document: DocumentNode;\n fetchAllFields: Set<string>;\n} {\n const fetchAllFields = new Set<string>();\n\n const strippedDocument = visit(document, {\n Field(node) {\n // Check if this field has the @fetchAll directive\n if (node.directives && node.directives.length > 0) {\n const hasFetchAll = node.directives.some((dir) => dir.name.value === FETCH_ALL_DIRECTIVE);\n\n if (hasFetchAll) {\n const fieldIdentifier = node.alias?.value || node.name.value;\n fetchAllFields.add(fieldIdentifier);\n\n // Return a new node without the @fetchAll directive\n return {\n ...node,\n directives: node.directives.filter((dir) => dir.name.value !== FETCH_ALL_DIRECTIVE),\n };\n }\n }\n\n return node;\n },\n });\n\n return {\n document: strippedDocument,\n fetchAllFields,\n };\n}\n\n/**\n * Custom merge function for deep object merging with special handling for lists\n *\n * @param {unknown} target - The target object or value to merge into\n * @param {unknown} source - The source object or value to merge from\n * @returns {unknown} Merged result with preservation of arrays and specific merge logic\n *\n * @remarks\n * Key behaviors:\n * - Preserves existing arrays without merging\n * - Handles null and undefined values\n * - Performs deep merge for nested objects\n * - Prioritizes source values for primitives\n *\n */\nfunction customMerge(target: unknown, source: unknown): unknown {\n if (source == null) return target;\n if (target == null) return source;\n\n // Preserve existing arrays (paginated data) - don't merge them\n if (isArray(target) || isArray(source)) {\n return target;\n }\n\n if (typeof target !== \"object\" || typeof source !== \"object\") {\n return source;\n }\n\n // Manually merge objects to ensure arrays are preserved\n const targetObj = target as Record<string, unknown>;\n const sourceObj = source as Record<string, unknown>;\n const result: Record<string, unknown> = { ...targetObj };\n\n for (const key in sourceObj) {\n if (Object.hasOwn(sourceObj, key)) {\n result[key] = key in result ? customMerge(result[key], sourceObj[key]) : sourceObj[key];\n }\n }\n\n return result;\n}\n\n// Extract all list fields (queries that have pagination arguments or @fetchAll)\nfunction extractListFields(document: DocumentNode, variables?: Variables, fetchAllFields?: Set<string>): ListField[] {\n const fields: ListField[] = [];\n const pathStack: string[] = [];\n\n visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Skip meta fields\n if (node.name.value.startsWith(\"__\")) {\n return;\n }\n\n // Check if this field has pagination arguments (first or skip)\n let hasFirstArg = false;\n let hasSkipArg = false;\n let firstValue: number | undefined;\n let skipValue: number | undefined;\n let firstValueIsDefault = false;\n const otherArgs: ArgumentNode[] = [];\n\n if (node.arguments) {\n for (const arg of node.arguments) {\n if (arg.name.value === FIRST_ARG) {\n hasFirstArg = true;\n if (arg.value.kind === Kind.INT) {\n firstValue = Number.parseInt(arg.value.value);\n } else if (arg.value.kind === Kind.VARIABLE && variables) {\n const varName = arg.value.name.value;\n const varValue = (variables as Record<string, unknown>)[varName];\n firstValue = typeof varValue === \"number\" ? varValue : undefined;\n // If variable is defined in query but not passed in input, check if it's a standard pagination variable\n if (firstValue === undefined && varName === \"first\") {\n hasFirstArg = true;\n firstValue = THE_GRAPH_LIMIT; // Default to THE_GRAPH_LIMIT\n firstValueIsDefault = true; // Mark that this was defaulted\n }\n }\n } else if (arg.name.value === SKIP_ARG) {\n hasSkipArg = true;\n if (arg.value.kind === Kind.INT) {\n skipValue = Number.parseInt(arg.value.value);\n } else if (arg.value.kind === Kind.VARIABLE && variables) {\n const varName = arg.value.name.value;\n const varValue = (variables as Record<string, unknown>)[varName];\n skipValue = typeof varValue === \"number\" ? varValue : undefined;\n // If variable is defined in query but not passed in input, check if it's a standard pagination variable\n if (skipValue === undefined && varName === \"skip\") {\n hasSkipArg = true;\n skipValue = 0; // Default to 0\n }\n }\n } else {\n otherArgs.push(arg);\n }\n }\n }\n\n // Check if this field has @fetchAll directive\n const fieldIdentifierForDirective = node.alias?.value || node.name.value;\n const hasFetchAllDirective = fetchAllFields?.has(fieldIdentifierForDirective);\n\n // Consider it a list field if it has pagination arguments OR @fetchAll\n const hasPaginationArgs = hasFirstArg || hasSkipArg;\n const shouldPaginate = hasPaginationArgs || hasFetchAllDirective;\n\n if (shouldPaginate) {\n // Do not allow nesting @fetchAll fields\n const parentFetchAllField = fields.find((field) => pathStack.join(\",\").startsWith(field.path.join(\",\")));\n if (parentFetchAllField) {\n throw new Error(\n `Nesting of @fetchAll directive is not supported: ${pathStack.join(\".\")} is a child of ${parentFetchAllField.path.join(\".\")}`,\n );\n }\n fields.push({\n path: [...pathStack],\n fieldName: node.name.value,\n alias: node.alias?.value,\n hasFirstArg,\n firstValue: hasFetchAllDirective && !hasFirstArg ? THE_GRAPH_LIMIT : firstValue,\n skipValue: hasFetchAllDirective && !hasSkipArg ? 0 : skipValue,\n otherArgs,\n selections: node.selectionSet?.selections,\n hasFetchAllDirective,\n firstValueIsDefault,\n });\n }\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n\n return fields;\n}\n\n// Create a query for a single field with specific pagination\nfunction createSingleFieldQuery(\n document: DocumentNode,\n targetField: ListField,\n skip: number,\n first: number,\n): DocumentNode {\n const targetPath = [...targetField.path];\n const pathStack: string[] = [];\n\n return visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Check if we're on the path to target field\n const onPath = pathStack.every((segment, i) => i >= targetPath.length || segment === targetPath[i]);\n\n if (!onPath) {\n pathStack.pop();\n return null; // Remove fields not on path\n }\n\n // If this is our target field, update pagination\n const isTarget =\n pathStack.length === targetPath.length && pathStack.every((segment, i) => segment === targetPath[i]);\n\n if (isTarget) {\n const newArgs: ArgumentNode[] = [...targetField.otherArgs];\n\n // Add pagination arguments\n newArgs.push(\n {\n kind: Kind.ARGUMENT,\n name: { kind: Kind.NAME, value: FIRST_ARG },\n value: { kind: Kind.INT, value: first.toString() },\n },\n {\n kind: Kind.ARGUMENT,\n name: { kind: Kind.NAME, value: SKIP_ARG },\n value: { kind: Kind.INT, value: skip.toString() },\n },\n );\n\n return { ...node, arguments: newArgs };\n }\n\n return undefined;\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n}\n\n// Create query without list fields\nfunction createNonListQuery(document: DocumentNode, listFields: ListField[]): DocumentNode | null {\n let hasFields = false;\n const pathStack: string[] = [];\n\n const filtered = visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Check if this field is a list field\n const isList = listFields.some(\n (field) =>\n field.path.length === pathStack.length && field.path.every((segment, i) => segment === pathStack[i]),\n );\n\n if (isList) {\n pathStack.pop();\n return null;\n }\n\n hasFields = true;\n return undefined;\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n\n return hasFields ? filtered : null;\n}\n\n// Filter variables to only include used ones\nfunction filterVariables(variables: Variables | undefined, document: DocumentNode): Variables | undefined {\n if (!variables) return undefined;\n\n const usedVariables = new Set<string>();\n\n visit(document, {\n Variable: (node) => {\n usedVariables.add(node.name.value);\n },\n });\n\n const filtered: Variables = {};\n const varsObj = variables as Record<string, unknown>;\n for (const key of usedVariables) {\n if (key in varsObj) {\n (filtered as Record<string, unknown>)[key] = varsObj[key];\n }\n }\n\n return isEmpty(filtered) ? undefined : filtered;\n}\n\nexport function createTheGraphClientWithPagination(theGraphClient: Pick<GraphQLClient, \"request\">) {\n // Execute pagination for a list field\n async function executeListFieldPagination(\n document: DocumentNode,\n variables: Variables | undefined,\n field: ListField,\n ): Promise<unknown[]> {\n const results: unknown[] = [];\n let currentSkip = field.skipValue || 0;\n let hasMore = true;\n\n // For fields with pagination arguments, always attempt to fetch data\n // and continue if we get a full page (indicating more data might exist)\n const batchSize = Math.min(field.firstValue || THE_GRAPH_LIMIT, THE_GRAPH_LIMIT);\n\n while (hasMore) {\n const query = createSingleFieldQuery(document, field, currentSkip, batchSize);\n const existingVariables = filterVariables(variables, query) ?? {};\n const response = await theGraphClient.request(query, {\n ...existingVariables,\n first: batchSize,\n skip: currentSkip,\n });\n\n // Use array path format for es-toolkit's get function\n const data = get(response, field.path) ?? get(response, field.fieldName);\n\n const parentPath = field.path.slice(0, -1);\n const parentData = get(response, parentPath);\n if (isArray(parentData) && parentData.length > 0) {\n throw new Error(\n `Response is an array, but expected a single object for field ${parentPath.join(\".\")}. The @fetchAll directive is not supported inside a query that returns a list of items.`,\n );\n }\n\n if (isArray(data) && data.length > 0) {\n results.push(...data);\n\n // Continue fetching if:\n // 1. We have @fetchAll directive (fetch everything)\n // 2. We have an explicit first value > THE_GRAPH_LIMIT and haven't reached it\n // 3. We have a defaulted first value and got a full batch (treating it as \"no explicit value\")\n // 4. We have no first value and got a full batch\n if (field.hasFetchAllDirective) {\n // With @fetchAll, continue if we got a full batch\n hasMore = data.length === batchSize;\n } else if (field.firstValue && !field.firstValueIsDefault) {\n // With explicit first value (not defaulted), only continue if:\n // - We haven't reached the requested amount yet\n // - We got a full batch (indicating more data might exist)\n hasMore = data.length === batchSize && results.length < field.firstValue;\n } else {\n // When first is not specified or was defaulted (using default batch size),\n // continue if we got a full batch (standard TheGraph pagination behavior)\n hasMore = data.length === batchSize;\n }\n } else {\n hasMore = false;\n }\n\n currentSkip += batchSize;\n }\n\n return results;\n }\n\n return {\n async query<TResult, TVariables extends Variables>(\n document: TadaDocumentNode<TResult, TVariables>,\n variables: Omit<TVariables, \"skip\" | \"first\">,\n ): Promise<TResult> {\n // First, detect and strip @fetchAll directives\n const { document: processedDocument, fetchAllFields } = stripFetchAllDirective(document);\n\n // Extract all list fields (including those with @fetchAll)\n const listFields = extractListFields(processedDocument, variables, fetchAllFields);\n\n // If no list fields, execute normally\n if (listFields.length === 0) {\n return theGraphClient.request(processedDocument, variables as Variables);\n }\n\n // Execute paginated queries for all list fields\n const result: Record<string, unknown> = {};\n\n // Sort fields by depth to handle nested fields correctly\n const sortedFields = sortBy(listFields, [(field) => field.path.length]);\n\n // Process list fields in parallel for better performance\n const fieldDataPromises = sortedFields.map(async (field) => ({\n field,\n data: await executeListFieldPagination(processedDocument, variables, field),\n }));\n\n const fieldResults = await Promise.all(fieldDataPromises);\n\n // Set results in order\n for (const { field, data } of fieldResults) {\n // Use array path format for es-toolkit's set function\n set(result, field.path, data);\n }\n\n // Execute non-list fields (single entity queries)\n const nonListQuery = createNonListQuery(processedDocument, listFields);\n\n if (nonListQuery) {\n const nonListResult = await theGraphClient.request(\n nonListQuery,\n filterVariables(variables, nonListQuery) ?? {},\n );\n\n // Merge results, preserving list data\n const merged = customMerge(result, nonListResult);\n return merged as TResult;\n }\n\n return result as TResult;\n },\n } as const;\n}\n","import { appendHeaders } from \"@settlemint/sdk-utils/http\";\nimport { ensureServer } from \"@settlemint/sdk-utils/runtime\";\nimport { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from \"@settlemint/sdk-utils/validation\";\nimport { type AbstractSetupSchema, initGraphQLTada } from \"gql.tada\";\nimport { GraphQLClient } from \"graphql-request\";\nimport { z } from \"zod\";\n\n/**\n * Type definition for GraphQL client configuration options\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating client options for the TheGraph client.\n */\nexport const ClientOptionsSchema = z.object({\n instances: z.array(UrlOrPathSchema),\n accessToken: ApplicationAccessTokenSchema.optional(),\n subgraphName: z.string(),\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n});\n\n/**\n * Type definition for client options derived from the ClientOptionsSchema\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Constructs the full URL for TheGraph GraphQL API based on the provided options\n *\n * @param options - The client options for configuring TheGraph client\n * @returns The complete GraphQL API URL as a string\n * @throws Will throw an error if no matching instance is found for the specified subgraph\n */\nfunction getFullUrl(options: ClientOptions): string {\n const instance = options.instances.find((instance) => instance.endsWith(`/${options.subgraphName}`));\n if (!instance) {\n throw new Error(`Instance for subgraph ${options.subgraphName} not found`);\n }\n return new URL(instance).toString();\n}\n\n/**\n * Creates a TheGraph GraphQL client with proper type safety using gql.tada\n *\n * @param options - Configuration options for the client including instance URLs,\n * access token and subgraph name\n * @param clientOptions - Optional GraphQL client configuration options\n * @returns An object containing:\n * - client: The configured GraphQL client instance\n * - graphql: The initialized gql.tada function for type-safe queries\n * @throws Will throw an error if the options fail validation against ClientOptionsSchema\n * @example\n * import { createTheGraphClient } from '@settlemint/sdk-thegraph';\n * import type { introspection } from '@schemas/the-graph-env-kits';\n * import { createLogger, requestLogger } from '@settlemint/sdk-utils/logging';\n *\n * const logger = createLogger();\n *\n * const { client, graphql } = createTheGraphClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * Bytes: string;\n * Int8: string;\n * BigInt: string;\n * BigDecimal: string;\n * Timestamp: string;\n * };\n * }>({\n * instances: JSON.parse(process.env.SETTLEMINT_THEGRAPH_SUBGRAPHS_ENDPOINTS || '[]'),\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN,\n * subgraphName: 'kits'\n * }, {\n * fetch: requestLogger(logger, \"the-graph-kits\", fetch) as typeof fetch,\n * });\n *\n * // Making GraphQL queries\n * const query = graphql(`\n * query SearchAssets {\n * assets {\n * id\n * name\n * symbol\n * }\n * }\n * `);\n *\n * const result = await client.request(query);\n */\nexport function createTheGraphClient<const Setup extends AbstractSetupSchema>(\n options: ClientOptions,\n clientOptions?: RequestConfig,\n): {\n client: GraphQLClient;\n graphql: initGraphQLTada<Setup>;\n} {\n ensureServer();\n const validatedOptions = validate(ClientOptionsSchema, options);\n const graphql = initGraphQLTada<Setup>();\n const fullUrl = getFullUrl(validatedOptions);\n\n return {\n client: new GraphQLClient(fullUrl, {\n ...clientOptions,\n headers: appendHeaders(clientOptions?.headers, { \"x-auth-token\": validatedOptions.accessToken }),\n }),\n graphql,\n };\n}\n\nexport type { FragmentOf, ResultOf, VariablesOf } from \"gql.tada\";\nexport { readFragment } from \"gql.tada\";\nexport { createTheGraphClientWithPagination } from \"./utils/pagination.js\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOA,MAAM,kBAAkB;AACxB,MAAM,YAAY;AAClB,MAAM,WAAW;AACjB,MAAM,sBAAsB;;;;;;;;;;;;;AA2B5B,SAAS,uBAAuBA,UAG9B;CACA,MAAM,iBAAiB,IAAI;CAE3B,MAAM,mBAAmB,mBAAM,UAAU,EACvC,MAAM,MAAM;AAEV,MAAI,KAAK,cAAc,KAAK,WAAW,SAAS,GAAG;GACjD,MAAM,cAAc,KAAK,WAAW,KAAK,CAAC,QAAQ,IAAI,KAAK,UAAU,oBAAoB;AAEzF,OAAI,aAAa;IACf,MAAM,kBAAkB,KAAK,OAAO,SAAS,KAAK,KAAK;AACvD,mBAAe,IAAI,gBAAgB;AAGnC,WAAO;KACL,GAAG;KACH,YAAY,KAAK,WAAW,OAAO,CAAC,QAAQ,IAAI,KAAK,UAAU,oBAAoB;IACpF;GACF;EACF;AAED,SAAO;CACR,EACF,EAAC;AAEF,QAAO;EACL,UAAU;EACV;CACD;AACF;;;;;;;;;;;;;;;;AAiBD,SAAS,YAAYC,QAAiBC,QAA0B;AAC9D,KAAI,UAAU,KAAM,QAAO;AAC3B,KAA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,MAAE,KAAK;EAAC;EAAW;EAAe;EAAY;EAAY;EAAkB;CAAS,EAAC,CAAC,UAAU;AACzG,EAAC;;;;;;;;AAcF,SAAS,WAAWC,SAAgC;CAClD,MAAM,WAAW,QAAQ,UAAU,KAAK,CAACC,eAAa,WAAS,UAAU,GAAG,QAAQ,aAAa,EAAE,CAAC;AACpG,MAAK,UAAU;AACb,QAAM,IAAI,OAAO,wBAAwB,QAAQ,aAAa;CAC/D;AACD,QAAO,IAAI,IAAI,UAAU,UAAU;AACpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkDD,SAAgB,qBACdD,SACAE,eAIA;AACA,mDAAc;CACd,MAAM,mBAAmB,gDAAS,qBAAqB,QAAQ;CAC/D,MAAMC,YAAU,+BAAwB;CACxC,MAAM,UAAU,WAAW,iBAAiB;AAE5C,QAAO;EACL,QAAQ,IAAIC,8BAAc,SAAS;GACjC,GAAG;GACH,SAAS,+CAAc,eAAe,SAAS,EAAE,gBAAgB,iBAAiB,YAAa,EAAC;EACjG;EACD;CACD;AACF"}
|
|
1
|
+
{"version":3,"file":"thegraph.cjs","names":["document: DocumentNode","target: unknown","source: unknown","result: Record<string, unknown>","variables?: Variables","fetchAllFields?: Set<string>","fields: ListField[]","pathStack: string[]","firstValue: number | undefined","skipValue: number | undefined","otherArgs: ArgumentNode[]","Kind","targetField: ListField","skip: number","first: number","newArgs: ArgumentNode[]","listFields: ListField[]","variables: Variables | undefined","filtered: Variables","theGraphClient: Pick<GraphQLClient, \"request\">","field: ListField","results: unknown[]","document: TadaDocumentNode<TResult, TVariables>","variables: Omit<TVariables, \"skip\" | \"first\">","UrlOrPathSchema","options: ClientOptions","instance","clientOptions?: RequestConfig","graphql","GraphQLClient"],"sources":["../src/utils/pagination.ts","../src/thegraph.ts"],"sourcesContent":["import { sortBy } from \"es-toolkit\";\nimport { get, isArray, isEmpty, set } from \"es-toolkit/compat\";\nimport type { TadaDocumentNode } from \"gql.tada\";\nimport { type ArgumentNode, type DocumentNode, Kind, type SelectionNode, visit } from \"graphql\";\nimport type { GraphQLClient, Variables } from \"graphql-request\";\n\n// Constants for TheGraph limits\nconst THE_GRAPH_LIMIT = 500;\nconst FIRST_ARG = \"first\";\nconst SKIP_ARG = \"skip\";\nconst FETCH_ALL_DIRECTIVE = \"fetchAll\";\n\ninterface ListField {\n path: string[];\n fieldName: string;\n alias?: string;\n hasFirstArg: boolean;\n firstValue?: number;\n skipValue?: number;\n otherArgs: ArgumentNode[];\n selections?: ReadonlyArray<SelectionNode>;\n hasFetchAllDirective?: boolean;\n firstValueIsDefault?: boolean; // Track if first value was defaulted\n}\n\n/**\n * Detects and strips @fetchAll directives from a GraphQL document\n *\n * @param {DocumentNode} document - The GraphQL document to process\n * @returns {Object} Processed document and list of fields with @fetchAll\n *\n * @remarks\n * This function:\n * - Identifies fields decorated with @fetchAll directive\n * - Removes the directive from the AST (The Graph doesn't recognize it)\n * - Returns both the cleaned document and a list of fields to auto-paginate\n */\nfunction stripFetchAllDirective(document: DocumentNode): {\n document: DocumentNode;\n fetchAllFields: Set<string>;\n} {\n const fetchAllFields = new Set<string>();\n\n const strippedDocument = visit(document, {\n Field(node) {\n // Check if this field has the @fetchAll directive\n if (node.directives && node.directives.length > 0) {\n const hasFetchAll = node.directives.some((dir) => dir.name.value === FETCH_ALL_DIRECTIVE);\n\n if (hasFetchAll) {\n const fieldIdentifier = node.alias?.value || node.name.value;\n fetchAllFields.add(fieldIdentifier);\n\n // Return a new node without the @fetchAll directive\n return {\n ...node,\n directives: node.directives.filter((dir) => dir.name.value !== FETCH_ALL_DIRECTIVE),\n };\n }\n }\n\n return node;\n },\n });\n\n return {\n document: strippedDocument,\n fetchAllFields,\n };\n}\n\n/**\n * Custom merge function for deep object merging with special handling for lists\n *\n * @param {unknown} target - The target object or value to merge into\n * @param {unknown} source - The source object or value to merge from\n * @returns {unknown} Merged result with preservation of arrays and specific merge logic\n *\n * @remarks\n * Key behaviors:\n * - Preserves existing arrays without merging\n * - Handles null and undefined values\n * - Performs deep merge for nested objects\n * - Prioritizes source values for primitives\n *\n */\nfunction customMerge(target: unknown, source: unknown): unknown {\n if (source == null) return target;\n if (target == null) return source;\n\n // Preserve existing arrays (paginated data) - don't merge them\n if (isArray(target) || isArray(source)) {\n return target;\n }\n\n if (typeof target !== \"object\" || typeof source !== \"object\") {\n return source;\n }\n\n // Manually merge objects to ensure arrays are preserved\n const targetObj = target as Record<string, unknown>;\n const sourceObj = source as Record<string, unknown>;\n const result: Record<string, unknown> = { ...targetObj };\n\n for (const key in sourceObj) {\n if (Object.hasOwn(sourceObj, key)) {\n result[key] = key in result ? customMerge(result[key], sourceObj[key]) : sourceObj[key];\n }\n }\n\n return result;\n}\n\n// Extract all fields that have @fetchAll directive\nfunction extractFetchAllFields(\n document: DocumentNode,\n variables?: Variables,\n fetchAllFields?: Set<string>,\n): ListField[] {\n const fields: ListField[] = [];\n const pathStack: string[] = [];\n\n visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Skip meta fields\n if (node.name.value.startsWith(\"__\")) {\n return;\n }\n\n // Check if this field has pagination arguments (first or skip)\n let hasFirstArg = false;\n let hasSkipArg = false;\n let firstValue: number | undefined;\n let skipValue: number | undefined;\n let firstValueIsDefault = false;\n const otherArgs: ArgumentNode[] = [];\n\n if (node.arguments) {\n for (const arg of node.arguments) {\n if (arg.name.value === FIRST_ARG) {\n hasFirstArg = true;\n if (arg.value.kind === Kind.INT) {\n firstValue = Number.parseInt(arg.value.value);\n } else if (arg.value.kind === Kind.VARIABLE && variables) {\n const varName = arg.value.name.value;\n const varValue = (variables as Record<string, unknown>)[varName];\n firstValue = typeof varValue === \"number\" ? varValue : undefined;\n // If variable is defined in query but not passed in input, check if it's a standard pagination variable\n if (firstValue === undefined && varName === \"first\") {\n hasFirstArg = true;\n firstValue = THE_GRAPH_LIMIT; // Default to THE_GRAPH_LIMIT\n firstValueIsDefault = true; // Mark that this was defaulted\n }\n }\n } else if (arg.name.value === SKIP_ARG) {\n hasSkipArg = true;\n if (arg.value.kind === Kind.INT) {\n skipValue = Number.parseInt(arg.value.value);\n } else if (arg.value.kind === Kind.VARIABLE && variables) {\n const varName = arg.value.name.value;\n const varValue = (variables as Record<string, unknown>)[varName];\n skipValue = typeof varValue === \"number\" ? varValue : undefined;\n // If variable is defined in query but not passed in input, check if it's a standard pagination variable\n if (skipValue === undefined && varName === \"skip\") {\n hasSkipArg = true;\n skipValue = 0; // Default to 0\n }\n }\n } else {\n otherArgs.push(arg);\n }\n }\n }\n\n // Check if this field has @fetchAll directive\n const fieldIdentifierForDirective = node.alias?.value || node.name.value;\n const hasFetchAllDirective = fetchAllFields?.has(fieldIdentifierForDirective);\n\n if (hasFetchAllDirective) {\n // Do not allow nesting @fetchAll fields\n const parentFetchAllField = fields.find((field) => pathStack.join(\",\").startsWith(field.path.join(\",\")));\n if (parentFetchAllField) {\n throw new Error(\n `Nesting of @fetchAll directive is not supported: ${pathStack.join(\".\")} is a child of ${parentFetchAllField.path.join(\".\")}`,\n );\n }\n fields.push({\n path: [...pathStack],\n fieldName: node.name.value,\n alias: node.alias?.value,\n hasFirstArg,\n firstValue: hasFetchAllDirective && !hasFirstArg ? THE_GRAPH_LIMIT : firstValue,\n skipValue: hasFetchAllDirective && !hasSkipArg ? 0 : skipValue,\n otherArgs,\n selections: node.selectionSet?.selections,\n hasFetchAllDirective,\n firstValueIsDefault,\n });\n }\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n\n return fields;\n}\n\n// Create a query for a single field with specific pagination\nfunction createSingleFieldQuery(\n document: DocumentNode,\n targetField: ListField,\n skip: number,\n first: number,\n): DocumentNode {\n const targetPath = [...targetField.path];\n const pathStack: string[] = [];\n\n return visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Check if we're on the path to target field\n const onPath = pathStack.every((segment, i) => i >= targetPath.length || segment === targetPath[i]);\n\n if (!onPath) {\n pathStack.pop();\n return null; // Remove fields not on path\n }\n\n // If this is our target field, update pagination\n const isTarget =\n pathStack.length === targetPath.length && pathStack.every((segment, i) => segment === targetPath[i]);\n\n if (isTarget) {\n const newArgs: ArgumentNode[] = [...targetField.otherArgs];\n\n // Add pagination arguments\n newArgs.push(\n {\n kind: Kind.ARGUMENT,\n name: { kind: Kind.NAME, value: FIRST_ARG },\n value: { kind: Kind.INT, value: first.toString() },\n },\n {\n kind: Kind.ARGUMENT,\n name: { kind: Kind.NAME, value: SKIP_ARG },\n value: { kind: Kind.INT, value: skip.toString() },\n },\n );\n\n return { ...node, arguments: newArgs };\n }\n\n return undefined;\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n}\n\n// Create query without list fields\nfunction createNonListQuery(document: DocumentNode, listFields: ListField[]): DocumentNode | null {\n let hasFields = false;\n const pathStack: string[] = [];\n\n const filtered = visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Check if this field is a list field\n const isList = listFields.some(\n (field) =>\n field.path.length === pathStack.length && field.path.every((segment, i) => segment === pathStack[i]),\n );\n\n if (isList) {\n pathStack.pop();\n return null;\n }\n\n hasFields = true;\n return undefined;\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n\n return hasFields ? filtered : null;\n}\n\n// Filter variables to only include used ones\nfunction filterVariables(variables: Variables | undefined, document: DocumentNode): Variables | undefined {\n if (!variables) return undefined;\n\n const usedVariables = new Set<string>();\n\n visit(document, {\n Variable: (node) => {\n usedVariables.add(node.name.value);\n },\n });\n\n const filtered: Variables = {};\n const varsObj = variables as Record<string, unknown>;\n for (const key of usedVariables) {\n if (key in varsObj) {\n (filtered as Record<string, unknown>)[key] = varsObj[key];\n }\n }\n\n return isEmpty(filtered) ? undefined : filtered;\n}\n\n/**\n * Creates a TheGraph client that supports pagination for list fields\n *\n * @param theGraphClient - The GraphQL client to use for requests\n * @returns A TheGraph client that supports pagination for list fields\n * @internal Used internally by createTheGraphClient\n */\nexport function createTheGraphClientWithPagination(theGraphClient: Pick<GraphQLClient, \"request\">) {\n // Execute pagination for a list field\n async function executeListFieldPagination(\n document: DocumentNode,\n variables: Variables | undefined,\n field: ListField,\n ): Promise<unknown[]> {\n const results: unknown[] = [];\n let currentSkip = field.skipValue || 0;\n let hasMore = true;\n\n // For fields with pagination arguments, always attempt to fetch data\n // and continue if we get a full page (indicating more data might exist)\n const batchSize = Math.min(field.firstValue || THE_GRAPH_LIMIT, THE_GRAPH_LIMIT);\n\n while (hasMore) {\n const query = createSingleFieldQuery(document, field, currentSkip, batchSize);\n const existingVariables = filterVariables(variables, query) ?? {};\n const response = await theGraphClient.request(query, {\n ...existingVariables,\n first: batchSize,\n skip: currentSkip,\n });\n\n // Use array path format for es-toolkit's get function\n const data = get(response, field.path) ?? get(response, field.fieldName);\n\n const parentPath = field.path.slice(0, -1);\n const parentData = get(response, parentPath);\n if (isArray(parentData) && parentData.length > 0) {\n throw new Error(\n `Response is an array, but expected a single object for field ${parentPath.join(\".\")}. The @fetchAll directive is not supported inside a query that returns a list of items.`,\n );\n }\n\n if (isArray(data) && data.length > 0) {\n results.push(...data);\n\n // Continue fetching if:\n // 1. We have @fetchAll directive (fetch everything)\n // 2. We have an explicit first value > THE_GRAPH_LIMIT and haven't reached it\n // 3. We have a defaulted first value and got a full batch (treating it as \"no explicit value\")\n // 4. We have no first value and got a full batch\n if (field.hasFetchAllDirective) {\n // With @fetchAll, continue if we got a full batch\n hasMore = data.length === batchSize;\n } else if (field.firstValue && !field.firstValueIsDefault) {\n // With explicit first value (not defaulted), only continue if:\n // - We haven't reached the requested amount yet\n // - We got a full batch (indicating more data might exist)\n hasMore = data.length === batchSize && results.length < field.firstValue;\n } else {\n // When first is not specified or was defaulted (using default batch size),\n // continue if we got a full batch (standard TheGraph pagination behavior)\n hasMore = data.length === batchSize;\n }\n } else {\n hasMore = false;\n }\n\n currentSkip += batchSize;\n }\n\n return results;\n }\n\n return {\n async query<TResult, TVariables extends Variables>(\n document: TadaDocumentNode<TResult, TVariables>,\n variables: Omit<TVariables, \"skip\" | \"first\">,\n ): Promise<TResult> {\n // First, detect and strip @fetchAll directives\n const { document: processedDocument, fetchAllFields } = stripFetchAllDirective(document);\n\n // Extract all list fields (including those with @fetchAll)\n const listFields = extractFetchAllFields(processedDocument, variables, fetchAllFields);\n\n // If no list fields, execute normally\n if (listFields.length === 0) {\n return theGraphClient.request(processedDocument, variables as Variables);\n }\n\n // Execute paginated queries for all list fields\n const result: Record<string, unknown> = {};\n\n // Sort fields by depth to handle nested fields correctly\n const sortedFields = sortBy(listFields, [(field) => field.path.length]);\n\n // Process list fields in parallel for better performance\n const fieldDataPromises = sortedFields.map(async (field) => ({\n field,\n data: await executeListFieldPagination(processedDocument, variables, field),\n }));\n\n const fieldResults = await Promise.all(fieldDataPromises);\n\n // Set results in order\n for (const { field, data } of fieldResults) {\n // Use array path format for es-toolkit's set function\n set(result, field.path, data);\n }\n\n // Execute non-list fields (single entity queries)\n const nonListQuery = createNonListQuery(processedDocument, listFields);\n\n if (nonListQuery) {\n const nonListResult = await theGraphClient.request(\n nonListQuery,\n filterVariables(variables, nonListQuery) ?? {},\n );\n\n // Merge results, preserving list data\n const merged = customMerge(result, nonListResult);\n return merged as TResult;\n }\n\n return result as TResult;\n },\n } as const;\n}\n","import { appendHeaders } from \"@settlemint/sdk-utils/http\";\nimport { ensureServer } from \"@settlemint/sdk-utils/runtime\";\nimport { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from \"@settlemint/sdk-utils/validation\";\nimport { type AbstractSetupSchema, initGraphQLTada } from \"gql.tada\";\nimport { GraphQLClient } from \"graphql-request\";\nimport { z } from \"zod\";\nimport { createTheGraphClientWithPagination } from \"./utils/pagination.js\";\n\n/**\n * Type definition for GraphQL client configuration options\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating client options for the TheGraph client.\n */\nexport const ClientOptionsSchema = z.object({\n instances: z.array(UrlOrPathSchema),\n accessToken: ApplicationAccessTokenSchema.optional(),\n subgraphName: z.string(),\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n});\n\n/**\n * Type definition for client options derived from the ClientOptionsSchema\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Constructs the full URL for TheGraph GraphQL API based on the provided options\n *\n * @param options - The client options for configuring TheGraph client\n * @returns The complete GraphQL API URL as a string\n * @throws Will throw an error if no matching instance is found for the specified subgraph\n */\nfunction getFullUrl(options: ClientOptions): string {\n const instance = options.instances.find((instance) => instance.endsWith(`/${options.subgraphName}`));\n if (!instance) {\n throw new Error(`Instance for subgraph ${options.subgraphName} not found`);\n }\n return new URL(instance).toString();\n}\n\n/**\n * Creates a TheGraph GraphQL client with proper type safety using gql.tada\n *\n * @param options - Configuration options for the client including instance URLs,\n * access token and subgraph name\n * @param clientOptions - Optional GraphQL client configuration options\n * @returns An object containing:\n * - client: The configured GraphQL client instance\n * - graphql: The initialized gql.tada function for type-safe queries\n * @throws Will throw an error if the options fail validation against ClientOptionsSchema\n * @example\n * import { createTheGraphClient } from '@settlemint/sdk-thegraph';\n * import type { introspection } from '@schemas/the-graph-env-kits';\n * import { createLogger, requestLogger } from '@settlemint/sdk-utils/logging';\n *\n * const logger = createLogger();\n *\n * const { client, graphql } = createTheGraphClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * Bytes: string;\n * Int8: string;\n * BigInt: string;\n * BigDecimal: string;\n * Timestamp: string;\n * };\n * }>({\n * instances: JSON.parse(process.env.SETTLEMINT_THEGRAPH_SUBGRAPHS_ENDPOINTS || '[]'),\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN,\n * subgraphName: 'kits'\n * }, {\n * fetch: requestLogger(logger, \"the-graph-kits\", fetch) as typeof fetch,\n * });\n *\n * // Making GraphQL queries\n * const query = graphql(`\n * query SearchAssets {\n * assets @fetchAll {\n * id\n * name\n * symbol\n * }\n * }\n * `);\n *\n * const result = await client.request(query);\n */\nexport function createTheGraphClient<const Setup extends AbstractSetupSchema>(\n options: ClientOptions,\n clientOptions?: RequestConfig,\n): {\n client: GraphQLClient;\n graphql: initGraphQLTada<Setup>;\n} {\n ensureServer();\n const validatedOptions = validate(ClientOptionsSchema, options);\n const graphql = initGraphQLTada<Setup>();\n const fullUrl = getFullUrl(validatedOptions);\n\n const client = new GraphQLClient(fullUrl, {\n ...clientOptions,\n headers: appendHeaders(clientOptions?.headers, { \"x-auth-token\": validatedOptions.accessToken }),\n }) as GraphQLClient & {\n query: ReturnType<typeof createTheGraphClientWithPagination>;\n };\n const paginatedClient = createTheGraphClientWithPagination(client);\n client.request = paginatedClient.query as typeof client.request;\n return {\n client,\n graphql,\n };\n}\n\nexport type { FragmentOf, ResultOf, VariablesOf } from \"gql.tada\";\nexport { readFragment } from \"gql.tada\";\nexport { createTheGraphClientWithPagination } from \"./utils/pagination.js\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOA,MAAM,kBAAkB;AACxB,MAAM,YAAY;AAClB,MAAM,WAAW;AACjB,MAAM,sBAAsB;;;;;;;;;;;;;AA2B5B,SAAS,uBAAuBA,UAG9B;CACA,MAAM,iBAAiB,IAAI;CAE3B,MAAM,mBAAmB,mBAAM,UAAU,EACvC,MAAM,MAAM;AAEV,MAAI,KAAK,cAAc,KAAK,WAAW,SAAS,GAAG;GACjD,MAAM,cAAc,KAAK,WAAW,KAAK,CAAC,QAAQ,IAAI,KAAK,UAAU,oBAAoB;AAEzF,OAAI,aAAa;IACf,MAAM,kBAAkB,KAAK,OAAO,SAAS,KAAK,KAAK;AACvD,mBAAe,IAAI,gBAAgB;AAGnC,WAAO;KACL,GAAG;KACH,YAAY,KAAK,WAAW,OAAO,CAAC,QAAQ,IAAI,KAAK,UAAU,oBAAoB;IACpF;GAC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AE,OAAO;CAC1C,WAAW,MAAE,MAAMqB,kDAAgB;CACnC,aAAa,+DAA6B,UAAU;CACpD,cAAc,MAAE,QAAQ;CACxB,OAAO,MAAE,KAAK;EAAC;EAAW;EAAe;EAAY;EAAY;EAAkB;CAAS,EAAC,CAAC,UAAU;AACzG,EAAC;;;;;;;;AAcF,SAAS,WAAWC,SAAgC;CAClD,MAAM,WAAW,QAAQ,UAAU,KAAK,CAACC,eAAa,WAAS,UAAU,GAAG,QAAQ,aAAa,EAAE,CAAC;AACpG,MAAK,UAAU;AACb,QAAM,IAAI,OAAO,wBAAwB,QAAQ,aAAa;CAC/D;AACD,QAAO,IAAI,IAAI,UAAU,UAAU;AACpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkDD,SAAgB,qBACdD,SACAE,eAIA;AACA,mDAAc;CACd,MAAM,mBAAmB,gDAAS,qBAAqB,QAAQ;CAC/D,MAAMC,YAAU,+BAAwB;CACxC,MAAM,UAAU,WAAW,iBAAiB;CAE5C,MAAM,SAAS,IAAIC,8BAAc,SAAS;EACxC,GAAG;EACH,SAAS,+CAAc,eAAe,SAAS,EAAE,gBAAgB,iBAAiB,YAAa,EAAC;CACjG;CAGD,MAAM,kBAAkB,mCAAmC,OAAO;AAClE,QAAO,UAAU,gBAAgB;AACjC,QAAO;EACL;EACA;CACD;AACF"}
|
package/dist/thegraph.d.cts
CHANGED
|
@@ -4,6 +4,14 @@ import { GraphQLClient, Variables } from "graphql-request";
|
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
|
|
6
6
|
//#region src/utils/pagination.d.ts
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates a TheGraph client that supports pagination for list fields
|
|
10
|
+
*
|
|
11
|
+
* @param theGraphClient - The GraphQL client to use for requests
|
|
12
|
+
* @returns A TheGraph client that supports pagination for list fields
|
|
13
|
+
* @internal Used internally by createTheGraphClient
|
|
14
|
+
*/
|
|
7
15
|
declare function createTheGraphClientWithPagination(theGraphClient: Pick<GraphQLClient, "request">): {
|
|
8
16
|
readonly query: <TResult, TVariables extends Variables>(document: TadaDocumentNode<TResult, TVariables>, variables: Omit<TVariables, "skip" | "first">) => Promise<TResult>;
|
|
9
17
|
};
|
|
@@ -71,7 +79,7 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
|
|
|
71
79
|
* // Making GraphQL queries
|
|
72
80
|
* const query = graphql(`
|
|
73
81
|
* query SearchAssets {
|
|
74
|
-
* assets {
|
|
82
|
+
* assets @fetchAll {
|
|
75
83
|
* id
|
|
76
84
|
* name
|
|
77
85
|
* symbol
|
package/dist/thegraph.d.ts
CHANGED
|
@@ -4,6 +4,14 @@ import { GraphQLClient, Variables } from "graphql-request";
|
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
|
|
6
6
|
//#region src/utils/pagination.d.ts
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates a TheGraph client that supports pagination for list fields
|
|
10
|
+
*
|
|
11
|
+
* @param theGraphClient - The GraphQL client to use for requests
|
|
12
|
+
* @returns A TheGraph client that supports pagination for list fields
|
|
13
|
+
* @internal Used internally by createTheGraphClient
|
|
14
|
+
*/
|
|
7
15
|
declare function createTheGraphClientWithPagination(theGraphClient: Pick<GraphQLClient, "request">): {
|
|
8
16
|
readonly query: <TResult, TVariables extends Variables>(document: TadaDocumentNode<TResult, TVariables>, variables: Omit<TVariables, "skip" | "first">) => Promise<TResult>;
|
|
9
17
|
};
|
|
@@ -71,7 +79,7 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
|
|
|
71
79
|
* // Making GraphQL queries
|
|
72
80
|
* const query = graphql(`
|
|
73
81
|
* query SearchAssets {
|
|
74
|
-
* assets {
|
|
82
|
+
* assets @fetchAll {
|
|
75
83
|
* id
|
|
76
84
|
* name
|
|
77
85
|
* symbol
|
package/dist/thegraph.js
CHANGED
|
@@ -81,7 +81,7 @@ function customMerge(target, source) {
|
|
|
81
81
|
}
|
|
82
82
|
return result;
|
|
83
83
|
}
|
|
84
|
-
function
|
|
84
|
+
function extractFetchAllFields(document, variables, fetchAllFields) {
|
|
85
85
|
const fields = [];
|
|
86
86
|
const pathStack = [];
|
|
87
87
|
visit(document, { Field: {
|
|
@@ -133,9 +133,7 @@ function extractListFields(document, variables, fetchAllFields) {
|
|
|
133
133
|
}
|
|
134
134
|
const fieldIdentifierForDirective = node.alias?.value || node.name.value;
|
|
135
135
|
const hasFetchAllDirective = fetchAllFields?.has(fieldIdentifierForDirective);
|
|
136
|
-
|
|
137
|
-
const shouldPaginate = hasPaginationArgs || hasFetchAllDirective;
|
|
138
|
-
if (shouldPaginate) {
|
|
136
|
+
if (hasFetchAllDirective) {
|
|
139
137
|
const parentFetchAllField = fields.find((field) => pathStack.join(",").startsWith(field.path.join(",")));
|
|
140
138
|
if (parentFetchAllField) {
|
|
141
139
|
throw new Error(`Nesting of @fetchAll directive is not supported: ${pathStack.join(".")} is a child of ${parentFetchAllField.path.join(".")}`);
|
|
@@ -244,6 +242,13 @@ function filterVariables(variables, document) {
|
|
|
244
242
|
}
|
|
245
243
|
return isEmpty(filtered) ? undefined : filtered;
|
|
246
244
|
}
|
|
245
|
+
/**
|
|
246
|
+
* Creates a TheGraph client that supports pagination for list fields
|
|
247
|
+
*
|
|
248
|
+
* @param theGraphClient - The GraphQL client to use for requests
|
|
249
|
+
* @returns A TheGraph client that supports pagination for list fields
|
|
250
|
+
* @internal Used internally by createTheGraphClient
|
|
251
|
+
*/
|
|
247
252
|
function createTheGraphClientWithPagination(theGraphClient) {
|
|
248
253
|
async function executeListFieldPagination(document, variables, field) {
|
|
249
254
|
const results = [];
|
|
@@ -282,7 +287,7 @@ function createTheGraphClientWithPagination(theGraphClient) {
|
|
|
282
287
|
}
|
|
283
288
|
return { async query(document, variables) {
|
|
284
289
|
const { document: processedDocument, fetchAllFields } = stripFetchAllDirective(document);
|
|
285
|
-
const listFields =
|
|
290
|
+
const listFields = extractFetchAllFields(processedDocument, variables, fetchAllFields);
|
|
286
291
|
if (listFields.length === 0) {
|
|
287
292
|
return theGraphClient.request(processedDocument, variables);
|
|
288
293
|
}
|
|
@@ -376,7 +381,7 @@ function getFullUrl(options) {
|
|
|
376
381
|
* // Making GraphQL queries
|
|
377
382
|
* const query = graphql(`
|
|
378
383
|
* query SearchAssets {
|
|
379
|
-
* assets {
|
|
384
|
+
* assets @fetchAll {
|
|
380
385
|
* id
|
|
381
386
|
* name
|
|
382
387
|
* symbol
|
|
@@ -391,11 +396,14 @@ function createTheGraphClient(options, clientOptions) {
|
|
|
391
396
|
const validatedOptions = validate(ClientOptionsSchema, options);
|
|
392
397
|
const graphql = initGraphQLTada();
|
|
393
398
|
const fullUrl = getFullUrl(validatedOptions);
|
|
399
|
+
const client = new GraphQLClient(fullUrl, {
|
|
400
|
+
...clientOptions,
|
|
401
|
+
headers: appendHeaders(clientOptions?.headers, { "x-auth-token": validatedOptions.accessToken })
|
|
402
|
+
});
|
|
403
|
+
const paginatedClient = createTheGraphClientWithPagination(client);
|
|
404
|
+
client.request = paginatedClient.query;
|
|
394
405
|
return {
|
|
395
|
-
client
|
|
396
|
-
...clientOptions,
|
|
397
|
-
headers: appendHeaders(clientOptions?.headers, { "x-auth-token": validatedOptions.accessToken })
|
|
398
|
-
}),
|
|
406
|
+
client,
|
|
399
407
|
graphql
|
|
400
408
|
};
|
|
401
409
|
}
|
package/dist/thegraph.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"thegraph.js","names":["document: DocumentNode","target: unknown","source: unknown","result: Record<string, unknown>","variables?: Variables","fetchAllFields?: Set<string>","fields: ListField[]","pathStack: string[]","firstValue: number | undefined","skipValue: number | undefined","otherArgs: ArgumentNode[]","targetField: ListField","skip: number","first: number","newArgs: ArgumentNode[]","listFields: ListField[]","variables: Variables | undefined","filtered: Variables","theGraphClient: Pick<GraphQLClient, \"request\">","field: ListField","results: unknown[]","document: TadaDocumentNode<TResult, TVariables>","variables: Omit<TVariables, \"skip\" | \"first\">","options: ClientOptions","instance","clientOptions?: RequestConfig"],"sources":["../src/utils/pagination.ts","../src/thegraph.ts"],"sourcesContent":["import { sortBy } from \"es-toolkit\";\nimport { get, isArray, isEmpty, set } from \"es-toolkit/compat\";\nimport type { TadaDocumentNode } from \"gql.tada\";\nimport { type ArgumentNode, type DocumentNode, Kind, type SelectionNode, visit } from \"graphql\";\nimport type { GraphQLClient, Variables } from \"graphql-request\";\n\n// Constants for TheGraph limits\nconst THE_GRAPH_LIMIT = 500;\nconst FIRST_ARG = \"first\";\nconst SKIP_ARG = \"skip\";\nconst FETCH_ALL_DIRECTIVE = \"fetchAll\";\n\ninterface ListField {\n path: string[];\n fieldName: string;\n alias?: string;\n hasFirstArg: boolean;\n firstValue?: number;\n skipValue?: number;\n otherArgs: ArgumentNode[];\n selections?: ReadonlyArray<SelectionNode>;\n hasFetchAllDirective?: boolean;\n firstValueIsDefault?: boolean; // Track if first value was defaulted\n}\n\n/**\n * Detects and strips @fetchAll directives from a GraphQL document\n *\n * @param {DocumentNode} document - The GraphQL document to process\n * @returns {Object} Processed document and list of fields with @fetchAll\n *\n * @remarks\n * This function:\n * - Identifies fields decorated with @fetchAll directive\n * - Removes the directive from the AST (The Graph doesn't recognize it)\n * - Returns both the cleaned document and a list of fields to auto-paginate\n */\nfunction stripFetchAllDirective(document: DocumentNode): {\n document: DocumentNode;\n fetchAllFields: Set<string>;\n} {\n const fetchAllFields = new Set<string>();\n\n const strippedDocument = visit(document, {\n Field(node) {\n // Check if this field has the @fetchAll directive\n if (node.directives && node.directives.length > 0) {\n const hasFetchAll = node.directives.some((dir) => dir.name.value === FETCH_ALL_DIRECTIVE);\n\n if (hasFetchAll) {\n const fieldIdentifier = node.alias?.value || node.name.value;\n fetchAllFields.add(fieldIdentifier);\n\n // Return a new node without the @fetchAll directive\n return {\n ...node,\n directives: node.directives.filter((dir) => dir.name.value !== FETCH_ALL_DIRECTIVE),\n };\n }\n }\n\n return node;\n },\n });\n\n return {\n document: strippedDocument,\n fetchAllFields,\n };\n}\n\n/**\n * Custom merge function for deep object merging with special handling for lists\n *\n * @param {unknown} target - The target object or value to merge into\n * @param {unknown} source - The source object or value to merge from\n * @returns {unknown} Merged result with preservation of arrays and specific merge logic\n *\n * @remarks\n * Key behaviors:\n * - Preserves existing arrays without merging\n * - Handles null and undefined values\n * - Performs deep merge for nested objects\n * - Prioritizes source values for primitives\n *\n */\nfunction customMerge(target: unknown, source: unknown): unknown {\n if (source == null) return target;\n if (target == null) return source;\n\n // Preserve existing arrays (paginated data) - don't merge them\n if (isArray(target) || isArray(source)) {\n return target;\n }\n\n if (typeof target !== \"object\" || typeof source !== \"object\") {\n return source;\n }\n\n // Manually merge objects to ensure arrays are preserved\n const targetObj = target as Record<string, unknown>;\n const sourceObj = source as Record<string, unknown>;\n const result: Record<string, unknown> = { ...targetObj };\n\n for (const key in sourceObj) {\n if (Object.hasOwn(sourceObj, key)) {\n result[key] = key in result ? customMerge(result[key], sourceObj[key]) : sourceObj[key];\n }\n }\n\n return result;\n}\n\n// Extract all list fields (queries that have pagination arguments or @fetchAll)\nfunction extractListFields(document: DocumentNode, variables?: Variables, fetchAllFields?: Set<string>): ListField[] {\n const fields: ListField[] = [];\n const pathStack: string[] = [];\n\n visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Skip meta fields\n if (node.name.value.startsWith(\"__\")) {\n return;\n }\n\n // Check if this field has pagination arguments (first or skip)\n let hasFirstArg = false;\n let hasSkipArg = false;\n let firstValue: number | undefined;\n let skipValue: number | undefined;\n let firstValueIsDefault = false;\n const otherArgs: ArgumentNode[] = [];\n\n if (node.arguments) {\n for (const arg of node.arguments) {\n if (arg.name.value === FIRST_ARG) {\n hasFirstArg = true;\n if (arg.value.kind === Kind.INT) {\n firstValue = Number.parseInt(arg.value.value);\n } else if (arg.value.kind === Kind.VARIABLE && variables) {\n const varName = arg.value.name.value;\n const varValue = (variables as Record<string, unknown>)[varName];\n firstValue = typeof varValue === \"number\" ? varValue : undefined;\n // If variable is defined in query but not passed in input, check if it's a standard pagination variable\n if (firstValue === undefined && varName === \"first\") {\n hasFirstArg = true;\n firstValue = THE_GRAPH_LIMIT; // Default to THE_GRAPH_LIMIT\n firstValueIsDefault = true; // Mark that this was defaulted\n }\n }\n } else if (arg.name.value === SKIP_ARG) {\n hasSkipArg = true;\n if (arg.value.kind === Kind.INT) {\n skipValue = Number.parseInt(arg.value.value);\n } else if (arg.value.kind === Kind.VARIABLE && variables) {\n const varName = arg.value.name.value;\n const varValue = (variables as Record<string, unknown>)[varName];\n skipValue = typeof varValue === \"number\" ? varValue : undefined;\n // If variable is defined in query but not passed in input, check if it's a standard pagination variable\n if (skipValue === undefined && varName === \"skip\") {\n hasSkipArg = true;\n skipValue = 0; // Default to 0\n }\n }\n } else {\n otherArgs.push(arg);\n }\n }\n }\n\n // Check if this field has @fetchAll directive\n const fieldIdentifierForDirective = node.alias?.value || node.name.value;\n const hasFetchAllDirective = fetchAllFields?.has(fieldIdentifierForDirective);\n\n // Consider it a list field if it has pagination arguments OR @fetchAll\n const hasPaginationArgs = hasFirstArg || hasSkipArg;\n const shouldPaginate = hasPaginationArgs || hasFetchAllDirective;\n\n if (shouldPaginate) {\n // Do not allow nesting @fetchAll fields\n const parentFetchAllField = fields.find((field) => pathStack.join(\",\").startsWith(field.path.join(\",\")));\n if (parentFetchAllField) {\n throw new Error(\n `Nesting of @fetchAll directive is not supported: ${pathStack.join(\".\")} is a child of ${parentFetchAllField.path.join(\".\")}`,\n );\n }\n fields.push({\n path: [...pathStack],\n fieldName: node.name.value,\n alias: node.alias?.value,\n hasFirstArg,\n firstValue: hasFetchAllDirective && !hasFirstArg ? THE_GRAPH_LIMIT : firstValue,\n skipValue: hasFetchAllDirective && !hasSkipArg ? 0 : skipValue,\n otherArgs,\n selections: node.selectionSet?.selections,\n hasFetchAllDirective,\n firstValueIsDefault,\n });\n }\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n\n return fields;\n}\n\n// Create a query for a single field with specific pagination\nfunction createSingleFieldQuery(\n document: DocumentNode,\n targetField: ListField,\n skip: number,\n first: number,\n): DocumentNode {\n const targetPath = [...targetField.path];\n const pathStack: string[] = [];\n\n return visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Check if we're on the path to target field\n const onPath = pathStack.every((segment, i) => i >= targetPath.length || segment === targetPath[i]);\n\n if (!onPath) {\n pathStack.pop();\n return null; // Remove fields not on path\n }\n\n // If this is our target field, update pagination\n const isTarget =\n pathStack.length === targetPath.length && pathStack.every((segment, i) => segment === targetPath[i]);\n\n if (isTarget) {\n const newArgs: ArgumentNode[] = [...targetField.otherArgs];\n\n // Add pagination arguments\n newArgs.push(\n {\n kind: Kind.ARGUMENT,\n name: { kind: Kind.NAME, value: FIRST_ARG },\n value: { kind: Kind.INT, value: first.toString() },\n },\n {\n kind: Kind.ARGUMENT,\n name: { kind: Kind.NAME, value: SKIP_ARG },\n value: { kind: Kind.INT, value: skip.toString() },\n },\n );\n\n return { ...node, arguments: newArgs };\n }\n\n return undefined;\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n}\n\n// Create query without list fields\nfunction createNonListQuery(document: DocumentNode, listFields: ListField[]): DocumentNode | null {\n let hasFields = false;\n const pathStack: string[] = [];\n\n const filtered = visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Check if this field is a list field\n const isList = listFields.some(\n (field) =>\n field.path.length === pathStack.length && field.path.every((segment, i) => segment === pathStack[i]),\n );\n\n if (isList) {\n pathStack.pop();\n return null;\n }\n\n hasFields = true;\n return undefined;\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n\n return hasFields ? filtered : null;\n}\n\n// Filter variables to only include used ones\nfunction filterVariables(variables: Variables | undefined, document: DocumentNode): Variables | undefined {\n if (!variables) return undefined;\n\n const usedVariables = new Set<string>();\n\n visit(document, {\n Variable: (node) => {\n usedVariables.add(node.name.value);\n },\n });\n\n const filtered: Variables = {};\n const varsObj = variables as Record<string, unknown>;\n for (const key of usedVariables) {\n if (key in varsObj) {\n (filtered as Record<string, unknown>)[key] = varsObj[key];\n }\n }\n\n return isEmpty(filtered) ? undefined : filtered;\n}\n\nexport function createTheGraphClientWithPagination(theGraphClient: Pick<GraphQLClient, \"request\">) {\n // Execute pagination for a list field\n async function executeListFieldPagination(\n document: DocumentNode,\n variables: Variables | undefined,\n field: ListField,\n ): Promise<unknown[]> {\n const results: unknown[] = [];\n let currentSkip = field.skipValue || 0;\n let hasMore = true;\n\n // For fields with pagination arguments, always attempt to fetch data\n // and continue if we get a full page (indicating more data might exist)\n const batchSize = Math.min(field.firstValue || THE_GRAPH_LIMIT, THE_GRAPH_LIMIT);\n\n while (hasMore) {\n const query = createSingleFieldQuery(document, field, currentSkip, batchSize);\n const existingVariables = filterVariables(variables, query) ?? {};\n const response = await theGraphClient.request(query, {\n ...existingVariables,\n first: batchSize,\n skip: currentSkip,\n });\n\n // Use array path format for es-toolkit's get function\n const data = get(response, field.path) ?? get(response, field.fieldName);\n\n const parentPath = field.path.slice(0, -1);\n const parentData = get(response, parentPath);\n if (isArray(parentData) && parentData.length > 0) {\n throw new Error(\n `Response is an array, but expected a single object for field ${parentPath.join(\".\")}. The @fetchAll directive is not supported inside a query that returns a list of items.`,\n );\n }\n\n if (isArray(data) && data.length > 0) {\n results.push(...data);\n\n // Continue fetching if:\n // 1. We have @fetchAll directive (fetch everything)\n // 2. We have an explicit first value > THE_GRAPH_LIMIT and haven't reached it\n // 3. We have a defaulted first value and got a full batch (treating it as \"no explicit value\")\n // 4. We have no first value and got a full batch\n if (field.hasFetchAllDirective) {\n // With @fetchAll, continue if we got a full batch\n hasMore = data.length === batchSize;\n } else if (field.firstValue && !field.firstValueIsDefault) {\n // With explicit first value (not defaulted), only continue if:\n // - We haven't reached the requested amount yet\n // - We got a full batch (indicating more data might exist)\n hasMore = data.length === batchSize && results.length < field.firstValue;\n } else {\n // When first is not specified or was defaulted (using default batch size),\n // continue if we got a full batch (standard TheGraph pagination behavior)\n hasMore = data.length === batchSize;\n }\n } else {\n hasMore = false;\n }\n\n currentSkip += batchSize;\n }\n\n return results;\n }\n\n return {\n async query<TResult, TVariables extends Variables>(\n document: TadaDocumentNode<TResult, TVariables>,\n variables: Omit<TVariables, \"skip\" | \"first\">,\n ): Promise<TResult> {\n // First, detect and strip @fetchAll directives\n const { document: processedDocument, fetchAllFields } = stripFetchAllDirective(document);\n\n // Extract all list fields (including those with @fetchAll)\n const listFields = extractListFields(processedDocument, variables, fetchAllFields);\n\n // If no list fields, execute normally\n if (listFields.length === 0) {\n return theGraphClient.request(processedDocument, variables as Variables);\n }\n\n // Execute paginated queries for all list fields\n const result: Record<string, unknown> = {};\n\n // Sort fields by depth to handle nested fields correctly\n const sortedFields = sortBy(listFields, [(field) => field.path.length]);\n\n // Process list fields in parallel for better performance\n const fieldDataPromises = sortedFields.map(async (field) => ({\n field,\n data: await executeListFieldPagination(processedDocument, variables, field),\n }));\n\n const fieldResults = await Promise.all(fieldDataPromises);\n\n // Set results in order\n for (const { field, data } of fieldResults) {\n // Use array path format for es-toolkit's set function\n set(result, field.path, data);\n }\n\n // Execute non-list fields (single entity queries)\n const nonListQuery = createNonListQuery(processedDocument, listFields);\n\n if (nonListQuery) {\n const nonListResult = await theGraphClient.request(\n nonListQuery,\n filterVariables(variables, nonListQuery) ?? {},\n );\n\n // Merge results, preserving list data\n const merged = customMerge(result, nonListResult);\n return merged as TResult;\n }\n\n return result as TResult;\n },\n } as const;\n}\n","import { appendHeaders } from \"@settlemint/sdk-utils/http\";\nimport { ensureServer } from \"@settlemint/sdk-utils/runtime\";\nimport { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from \"@settlemint/sdk-utils/validation\";\nimport { type AbstractSetupSchema, initGraphQLTada } from \"gql.tada\";\nimport { GraphQLClient } from \"graphql-request\";\nimport { z } from \"zod\";\n\n/**\n * Type definition for GraphQL client configuration options\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating client options for the TheGraph client.\n */\nexport const ClientOptionsSchema = z.object({\n instances: z.array(UrlOrPathSchema),\n accessToken: ApplicationAccessTokenSchema.optional(),\n subgraphName: z.string(),\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n});\n\n/**\n * Type definition for client options derived from the ClientOptionsSchema\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Constructs the full URL for TheGraph GraphQL API based on the provided options\n *\n * @param options - The client options for configuring TheGraph client\n * @returns The complete GraphQL API URL as a string\n * @throws Will throw an error if no matching instance is found for the specified subgraph\n */\nfunction getFullUrl(options: ClientOptions): string {\n const instance = options.instances.find((instance) => instance.endsWith(`/${options.subgraphName}`));\n if (!instance) {\n throw new Error(`Instance for subgraph ${options.subgraphName} not found`);\n }\n return new URL(instance).toString();\n}\n\n/**\n * Creates a TheGraph GraphQL client with proper type safety using gql.tada\n *\n * @param options - Configuration options for the client including instance URLs,\n * access token and subgraph name\n * @param clientOptions - Optional GraphQL client configuration options\n * @returns An object containing:\n * - client: The configured GraphQL client instance\n * - graphql: The initialized gql.tada function for type-safe queries\n * @throws Will throw an error if the options fail validation against ClientOptionsSchema\n * @example\n * import { createTheGraphClient } from '@settlemint/sdk-thegraph';\n * import type { introspection } from '@schemas/the-graph-env-kits';\n * import { createLogger, requestLogger } from '@settlemint/sdk-utils/logging';\n *\n * const logger = createLogger();\n *\n * const { client, graphql } = createTheGraphClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * Bytes: string;\n * Int8: string;\n * BigInt: string;\n * BigDecimal: string;\n * Timestamp: string;\n * };\n * }>({\n * instances: JSON.parse(process.env.SETTLEMINT_THEGRAPH_SUBGRAPHS_ENDPOINTS || '[]'),\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN,\n * subgraphName: 'kits'\n * }, {\n * fetch: requestLogger(logger, \"the-graph-kits\", fetch) as typeof fetch,\n * });\n *\n * // Making GraphQL queries\n * const query = graphql(`\n * query SearchAssets {\n * assets {\n * id\n * name\n * symbol\n * }\n * }\n * `);\n *\n * const result = await client.request(query);\n */\nexport function createTheGraphClient<const Setup extends AbstractSetupSchema>(\n options: ClientOptions,\n clientOptions?: RequestConfig,\n): {\n client: GraphQLClient;\n graphql: initGraphQLTada<Setup>;\n} {\n ensureServer();\n const validatedOptions = validate(ClientOptionsSchema, options);\n const graphql = initGraphQLTada<Setup>();\n const fullUrl = getFullUrl(validatedOptions);\n\n return {\n client: new GraphQLClient(fullUrl, {\n ...clientOptions,\n headers: appendHeaders(clientOptions?.headers, { \"x-auth-token\": validatedOptions.accessToken }),\n }),\n graphql,\n };\n}\n\nexport type { FragmentOf, ResultOf, VariablesOf } from \"gql.tada\";\nexport { readFragment } from \"gql.tada\";\nexport { createTheGraphClientWithPagination } from \"./utils/pagination.js\";\n"],"mappings":";;;;;;;;;;;;AAOA,MAAM,kBAAkB;AACxB,MAAM,YAAY;AAClB,MAAM,WAAW;AACjB,MAAM,sBAAsB;;;;;;;;;;;;;AA2B5B,SAAS,uBAAuBA,UAG9B;CACA,MAAM,iBAAiB,IAAI;CAE3B,MAAM,mBAAmB,MAAM,UAAU,EACvC,MAAM,MAAM;AAEV,MAAI,KAAK,cAAc,KAAK,WAAW,SAAS,GAAG;GACjD,MAAM,cAAc,KAAK,WAAW,KAAK,CAAC,QAAQ,IAAI,KAAK,UAAU,oBAAoB;AAEzF,OAAI,aAAa;IACf,MAAM,kBAAkB,KAAK,OAAO,SAAS,KAAK,KAAK;AACvD,mBAAe,IAAI,gBAAgB;AAGnC,WAAO;KACL,GAAG;KACH,YAAY,KAAK,WAAW,OAAO,CAAC,QAAQ,IAAI,KAAK,UAAU,oBAAoB;IACpF;GACF;EACF;AAED,SAAO;CAC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AAsB,EAAE,OAAO;CAC1C,WAAW,EAAE,MAAM,gBAAgB;CACnC,aAAa,6BAA6B,UAAU;CACpD,cAAc,EAAE,QAAQ;CACxB,OAAO,EAAE,KAAK;EAAC;EAAW;EAAe;EAAY;EAAY;EAAkB;CAAS,EAAC,CAAC,UAAU;AACzG,EAAC;;;;;;;;AAcF,SAAS,WAAWoB,SAAgC;CAClD,MAAM,WAAW,QAAQ,UAAU,KAAK,CAACC,eAAa,WAAS,UAAU,GAAG,QAAQ,aAAa,EAAE,CAAC;AACpG,MAAK,UAAU;AACb,QAAM,IAAI,OAAO,wBAAwB,QAAQ,aAAa;CAC/D;AACD,QAAO,IAAI,IAAI,UAAU,UAAU;AACpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkDD,SAAgB,qBACdD,SACAE,eAIA;AACA,eAAc;CACd,MAAM,mBAAmB,SAAS,qBAAqB,QAAQ;CAC/D,MAAM,UAAU,iBAAwB;CACxC,MAAM,UAAU,WAAW,iBAAiB;AAE5C,QAAO;EACL,QAAQ,IAAI,cAAc,SAAS;GACjC,GAAG;GACH,SAAS,cAAc,eAAe,SAAS,EAAE,gBAAgB,iBAAiB,YAAa,EAAC;EACjG;EACD;CACD;AACF"}
|
|
1
|
+
{"version":3,"file":"thegraph.js","names":["document: DocumentNode","target: unknown","source: unknown","result: Record<string, unknown>","variables?: Variables","fetchAllFields?: Set<string>","fields: ListField[]","pathStack: string[]","firstValue: number | undefined","skipValue: number | undefined","otherArgs: ArgumentNode[]","targetField: ListField","skip: number","first: number","newArgs: ArgumentNode[]","listFields: ListField[]","variables: Variables | undefined","filtered: Variables","theGraphClient: Pick<GraphQLClient, \"request\">","field: ListField","results: unknown[]","document: TadaDocumentNode<TResult, TVariables>","variables: Omit<TVariables, \"skip\" | \"first\">","options: ClientOptions","instance","clientOptions?: RequestConfig"],"sources":["../src/utils/pagination.ts","../src/thegraph.ts"],"sourcesContent":["import { sortBy } from \"es-toolkit\";\nimport { get, isArray, isEmpty, set } from \"es-toolkit/compat\";\nimport type { TadaDocumentNode } from \"gql.tada\";\nimport { type ArgumentNode, type DocumentNode, Kind, type SelectionNode, visit } from \"graphql\";\nimport type { GraphQLClient, Variables } from \"graphql-request\";\n\n// Constants for TheGraph limits\nconst THE_GRAPH_LIMIT = 500;\nconst FIRST_ARG = \"first\";\nconst SKIP_ARG = \"skip\";\nconst FETCH_ALL_DIRECTIVE = \"fetchAll\";\n\ninterface ListField {\n path: string[];\n fieldName: string;\n alias?: string;\n hasFirstArg: boolean;\n firstValue?: number;\n skipValue?: number;\n otherArgs: ArgumentNode[];\n selections?: ReadonlyArray<SelectionNode>;\n hasFetchAllDirective?: boolean;\n firstValueIsDefault?: boolean; // Track if first value was defaulted\n}\n\n/**\n * Detects and strips @fetchAll directives from a GraphQL document\n *\n * @param {DocumentNode} document - The GraphQL document to process\n * @returns {Object} Processed document and list of fields with @fetchAll\n *\n * @remarks\n * This function:\n * - Identifies fields decorated with @fetchAll directive\n * - Removes the directive from the AST (The Graph doesn't recognize it)\n * - Returns both the cleaned document and a list of fields to auto-paginate\n */\nfunction stripFetchAllDirective(document: DocumentNode): {\n document: DocumentNode;\n fetchAllFields: Set<string>;\n} {\n const fetchAllFields = new Set<string>();\n\n const strippedDocument = visit(document, {\n Field(node) {\n // Check if this field has the @fetchAll directive\n if (node.directives && node.directives.length > 0) {\n const hasFetchAll = node.directives.some((dir) => dir.name.value === FETCH_ALL_DIRECTIVE);\n\n if (hasFetchAll) {\n const fieldIdentifier = node.alias?.value || node.name.value;\n fetchAllFields.add(fieldIdentifier);\n\n // Return a new node without the @fetchAll directive\n return {\n ...node,\n directives: node.directives.filter((dir) => dir.name.value !== FETCH_ALL_DIRECTIVE),\n };\n }\n }\n\n return node;\n },\n });\n\n return {\n document: strippedDocument,\n fetchAllFields,\n };\n}\n\n/**\n * Custom merge function for deep object merging with special handling for lists\n *\n * @param {unknown} target - The target object or value to merge into\n * @param {unknown} source - The source object or value to merge from\n * @returns {unknown} Merged result with preservation of arrays and specific merge logic\n *\n * @remarks\n * Key behaviors:\n * - Preserves existing arrays without merging\n * - Handles null and undefined values\n * - Performs deep merge for nested objects\n * - Prioritizes source values for primitives\n *\n */\nfunction customMerge(target: unknown, source: unknown): unknown {\n if (source == null) return target;\n if (target == null) return source;\n\n // Preserve existing arrays (paginated data) - don't merge them\n if (isArray(target) || isArray(source)) {\n return target;\n }\n\n if (typeof target !== \"object\" || typeof source !== \"object\") {\n return source;\n }\n\n // Manually merge objects to ensure arrays are preserved\n const targetObj = target as Record<string, unknown>;\n const sourceObj = source as Record<string, unknown>;\n const result: Record<string, unknown> = { ...targetObj };\n\n for (const key in sourceObj) {\n if (Object.hasOwn(sourceObj, key)) {\n result[key] = key in result ? customMerge(result[key], sourceObj[key]) : sourceObj[key];\n }\n }\n\n return result;\n}\n\n// Extract all fields that have @fetchAll directive\nfunction extractFetchAllFields(\n document: DocumentNode,\n variables?: Variables,\n fetchAllFields?: Set<string>,\n): ListField[] {\n const fields: ListField[] = [];\n const pathStack: string[] = [];\n\n visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Skip meta fields\n if (node.name.value.startsWith(\"__\")) {\n return;\n }\n\n // Check if this field has pagination arguments (first or skip)\n let hasFirstArg = false;\n let hasSkipArg = false;\n let firstValue: number | undefined;\n let skipValue: number | undefined;\n let firstValueIsDefault = false;\n const otherArgs: ArgumentNode[] = [];\n\n if (node.arguments) {\n for (const arg of node.arguments) {\n if (arg.name.value === FIRST_ARG) {\n hasFirstArg = true;\n if (arg.value.kind === Kind.INT) {\n firstValue = Number.parseInt(arg.value.value);\n } else if (arg.value.kind === Kind.VARIABLE && variables) {\n const varName = arg.value.name.value;\n const varValue = (variables as Record<string, unknown>)[varName];\n firstValue = typeof varValue === \"number\" ? varValue : undefined;\n // If variable is defined in query but not passed in input, check if it's a standard pagination variable\n if (firstValue === undefined && varName === \"first\") {\n hasFirstArg = true;\n firstValue = THE_GRAPH_LIMIT; // Default to THE_GRAPH_LIMIT\n firstValueIsDefault = true; // Mark that this was defaulted\n }\n }\n } else if (arg.name.value === SKIP_ARG) {\n hasSkipArg = true;\n if (arg.value.kind === Kind.INT) {\n skipValue = Number.parseInt(arg.value.value);\n } else if (arg.value.kind === Kind.VARIABLE && variables) {\n const varName = arg.value.name.value;\n const varValue = (variables as Record<string, unknown>)[varName];\n skipValue = typeof varValue === \"number\" ? varValue : undefined;\n // If variable is defined in query but not passed in input, check if it's a standard pagination variable\n if (skipValue === undefined && varName === \"skip\") {\n hasSkipArg = true;\n skipValue = 0; // Default to 0\n }\n }\n } else {\n otherArgs.push(arg);\n }\n }\n }\n\n // Check if this field has @fetchAll directive\n const fieldIdentifierForDirective = node.alias?.value || node.name.value;\n const hasFetchAllDirective = fetchAllFields?.has(fieldIdentifierForDirective);\n\n if (hasFetchAllDirective) {\n // Do not allow nesting @fetchAll fields\n const parentFetchAllField = fields.find((field) => pathStack.join(\",\").startsWith(field.path.join(\",\")));\n if (parentFetchAllField) {\n throw new Error(\n `Nesting of @fetchAll directive is not supported: ${pathStack.join(\".\")} is a child of ${parentFetchAllField.path.join(\".\")}`,\n );\n }\n fields.push({\n path: [...pathStack],\n fieldName: node.name.value,\n alias: node.alias?.value,\n hasFirstArg,\n firstValue: hasFetchAllDirective && !hasFirstArg ? THE_GRAPH_LIMIT : firstValue,\n skipValue: hasFetchAllDirective && !hasSkipArg ? 0 : skipValue,\n otherArgs,\n selections: node.selectionSet?.selections,\n hasFetchAllDirective,\n firstValueIsDefault,\n });\n }\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n\n return fields;\n}\n\n// Create a query for a single field with specific pagination\nfunction createSingleFieldQuery(\n document: DocumentNode,\n targetField: ListField,\n skip: number,\n first: number,\n): DocumentNode {\n const targetPath = [...targetField.path];\n const pathStack: string[] = [];\n\n return visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Check if we're on the path to target field\n const onPath = pathStack.every((segment, i) => i >= targetPath.length || segment === targetPath[i]);\n\n if (!onPath) {\n pathStack.pop();\n return null; // Remove fields not on path\n }\n\n // If this is our target field, update pagination\n const isTarget =\n pathStack.length === targetPath.length && pathStack.every((segment, i) => segment === targetPath[i]);\n\n if (isTarget) {\n const newArgs: ArgumentNode[] = [...targetField.otherArgs];\n\n // Add pagination arguments\n newArgs.push(\n {\n kind: Kind.ARGUMENT,\n name: { kind: Kind.NAME, value: FIRST_ARG },\n value: { kind: Kind.INT, value: first.toString() },\n },\n {\n kind: Kind.ARGUMENT,\n name: { kind: Kind.NAME, value: SKIP_ARG },\n value: { kind: Kind.INT, value: skip.toString() },\n },\n );\n\n return { ...node, arguments: newArgs };\n }\n\n return undefined;\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n}\n\n// Create query without list fields\nfunction createNonListQuery(document: DocumentNode, listFields: ListField[]): DocumentNode | null {\n let hasFields = false;\n const pathStack: string[] = [];\n\n const filtered = visit(document, {\n Field: {\n enter: (node) => {\n const fieldIdentifier = node.alias?.value || node.name.value;\n pathStack.push(fieldIdentifier);\n\n // Check if this field is a list field\n const isList = listFields.some(\n (field) =>\n field.path.length === pathStack.length && field.path.every((segment, i) => segment === pathStack[i]),\n );\n\n if (isList) {\n pathStack.pop();\n return null;\n }\n\n hasFields = true;\n return undefined;\n },\n leave: () => {\n pathStack.pop();\n },\n },\n });\n\n return hasFields ? filtered : null;\n}\n\n// Filter variables to only include used ones\nfunction filterVariables(variables: Variables | undefined, document: DocumentNode): Variables | undefined {\n if (!variables) return undefined;\n\n const usedVariables = new Set<string>();\n\n visit(document, {\n Variable: (node) => {\n usedVariables.add(node.name.value);\n },\n });\n\n const filtered: Variables = {};\n const varsObj = variables as Record<string, unknown>;\n for (const key of usedVariables) {\n if (key in varsObj) {\n (filtered as Record<string, unknown>)[key] = varsObj[key];\n }\n }\n\n return isEmpty(filtered) ? undefined : filtered;\n}\n\n/**\n * Creates a TheGraph client that supports pagination for list fields\n *\n * @param theGraphClient - The GraphQL client to use for requests\n * @returns A TheGraph client that supports pagination for list fields\n * @internal Used internally by createTheGraphClient\n */\nexport function createTheGraphClientWithPagination(theGraphClient: Pick<GraphQLClient, \"request\">) {\n // Execute pagination for a list field\n async function executeListFieldPagination(\n document: DocumentNode,\n variables: Variables | undefined,\n field: ListField,\n ): Promise<unknown[]> {\n const results: unknown[] = [];\n let currentSkip = field.skipValue || 0;\n let hasMore = true;\n\n // For fields with pagination arguments, always attempt to fetch data\n // and continue if we get a full page (indicating more data might exist)\n const batchSize = Math.min(field.firstValue || THE_GRAPH_LIMIT, THE_GRAPH_LIMIT);\n\n while (hasMore) {\n const query = createSingleFieldQuery(document, field, currentSkip, batchSize);\n const existingVariables = filterVariables(variables, query) ?? {};\n const response = await theGraphClient.request(query, {\n ...existingVariables,\n first: batchSize,\n skip: currentSkip,\n });\n\n // Use array path format for es-toolkit's get function\n const data = get(response, field.path) ?? get(response, field.fieldName);\n\n const parentPath = field.path.slice(0, -1);\n const parentData = get(response, parentPath);\n if (isArray(parentData) && parentData.length > 0) {\n throw new Error(\n `Response is an array, but expected a single object for field ${parentPath.join(\".\")}. The @fetchAll directive is not supported inside a query that returns a list of items.`,\n );\n }\n\n if (isArray(data) && data.length > 0) {\n results.push(...data);\n\n // Continue fetching if:\n // 1. We have @fetchAll directive (fetch everything)\n // 2. We have an explicit first value > THE_GRAPH_LIMIT and haven't reached it\n // 3. We have a defaulted first value and got a full batch (treating it as \"no explicit value\")\n // 4. We have no first value and got a full batch\n if (field.hasFetchAllDirective) {\n // With @fetchAll, continue if we got a full batch\n hasMore = data.length === batchSize;\n } else if (field.firstValue && !field.firstValueIsDefault) {\n // With explicit first value (not defaulted), only continue if:\n // - We haven't reached the requested amount yet\n // - We got a full batch (indicating more data might exist)\n hasMore = data.length === batchSize && results.length < field.firstValue;\n } else {\n // When first is not specified or was defaulted (using default batch size),\n // continue if we got a full batch (standard TheGraph pagination behavior)\n hasMore = data.length === batchSize;\n }\n } else {\n hasMore = false;\n }\n\n currentSkip += batchSize;\n }\n\n return results;\n }\n\n return {\n async query<TResult, TVariables extends Variables>(\n document: TadaDocumentNode<TResult, TVariables>,\n variables: Omit<TVariables, \"skip\" | \"first\">,\n ): Promise<TResult> {\n // First, detect and strip @fetchAll directives\n const { document: processedDocument, fetchAllFields } = stripFetchAllDirective(document);\n\n // Extract all list fields (including those with @fetchAll)\n const listFields = extractFetchAllFields(processedDocument, variables, fetchAllFields);\n\n // If no list fields, execute normally\n if (listFields.length === 0) {\n return theGraphClient.request(processedDocument, variables as Variables);\n }\n\n // Execute paginated queries for all list fields\n const result: Record<string, unknown> = {};\n\n // Sort fields by depth to handle nested fields correctly\n const sortedFields = sortBy(listFields, [(field) => field.path.length]);\n\n // Process list fields in parallel for better performance\n const fieldDataPromises = sortedFields.map(async (field) => ({\n field,\n data: await executeListFieldPagination(processedDocument, variables, field),\n }));\n\n const fieldResults = await Promise.all(fieldDataPromises);\n\n // Set results in order\n for (const { field, data } of fieldResults) {\n // Use array path format for es-toolkit's set function\n set(result, field.path, data);\n }\n\n // Execute non-list fields (single entity queries)\n const nonListQuery = createNonListQuery(processedDocument, listFields);\n\n if (nonListQuery) {\n const nonListResult = await theGraphClient.request(\n nonListQuery,\n filterVariables(variables, nonListQuery) ?? {},\n );\n\n // Merge results, preserving list data\n const merged = customMerge(result, nonListResult);\n return merged as TResult;\n }\n\n return result as TResult;\n },\n } as const;\n}\n","import { appendHeaders } from \"@settlemint/sdk-utils/http\";\nimport { ensureServer } from \"@settlemint/sdk-utils/runtime\";\nimport { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from \"@settlemint/sdk-utils/validation\";\nimport { type AbstractSetupSchema, initGraphQLTada } from \"gql.tada\";\nimport { GraphQLClient } from \"graphql-request\";\nimport { z } from \"zod\";\nimport { createTheGraphClientWithPagination } from \"./utils/pagination.js\";\n\n/**\n * Type definition for GraphQL client configuration options\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating client options for the TheGraph client.\n */\nexport const ClientOptionsSchema = z.object({\n instances: z.array(UrlOrPathSchema),\n accessToken: ApplicationAccessTokenSchema.optional(),\n subgraphName: z.string(),\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n});\n\n/**\n * Type definition for client options derived from the ClientOptionsSchema\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Constructs the full URL for TheGraph GraphQL API based on the provided options\n *\n * @param options - The client options for configuring TheGraph client\n * @returns The complete GraphQL API URL as a string\n * @throws Will throw an error if no matching instance is found for the specified subgraph\n */\nfunction getFullUrl(options: ClientOptions): string {\n const instance = options.instances.find((instance) => instance.endsWith(`/${options.subgraphName}`));\n if (!instance) {\n throw new Error(`Instance for subgraph ${options.subgraphName} not found`);\n }\n return new URL(instance).toString();\n}\n\n/**\n * Creates a TheGraph GraphQL client with proper type safety using gql.tada\n *\n * @param options - Configuration options for the client including instance URLs,\n * access token and subgraph name\n * @param clientOptions - Optional GraphQL client configuration options\n * @returns An object containing:\n * - client: The configured GraphQL client instance\n * - graphql: The initialized gql.tada function for type-safe queries\n * @throws Will throw an error if the options fail validation against ClientOptionsSchema\n * @example\n * import { createTheGraphClient } from '@settlemint/sdk-thegraph';\n * import type { introspection } from '@schemas/the-graph-env-kits';\n * import { createLogger, requestLogger } from '@settlemint/sdk-utils/logging';\n *\n * const logger = createLogger();\n *\n * const { client, graphql } = createTheGraphClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * Bytes: string;\n * Int8: string;\n * BigInt: string;\n * BigDecimal: string;\n * Timestamp: string;\n * };\n * }>({\n * instances: JSON.parse(process.env.SETTLEMINT_THEGRAPH_SUBGRAPHS_ENDPOINTS || '[]'),\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN,\n * subgraphName: 'kits'\n * }, {\n * fetch: requestLogger(logger, \"the-graph-kits\", fetch) as typeof fetch,\n * });\n *\n * // Making GraphQL queries\n * const query = graphql(`\n * query SearchAssets {\n * assets @fetchAll {\n * id\n * name\n * symbol\n * }\n * }\n * `);\n *\n * const result = await client.request(query);\n */\nexport function createTheGraphClient<const Setup extends AbstractSetupSchema>(\n options: ClientOptions,\n clientOptions?: RequestConfig,\n): {\n client: GraphQLClient;\n graphql: initGraphQLTada<Setup>;\n} {\n ensureServer();\n const validatedOptions = validate(ClientOptionsSchema, options);\n const graphql = initGraphQLTada<Setup>();\n const fullUrl = getFullUrl(validatedOptions);\n\n const client = new GraphQLClient(fullUrl, {\n ...clientOptions,\n headers: appendHeaders(clientOptions?.headers, { \"x-auth-token\": validatedOptions.accessToken }),\n }) as GraphQLClient & {\n query: ReturnType<typeof createTheGraphClientWithPagination>;\n };\n const paginatedClient = createTheGraphClientWithPagination(client);\n client.request = paginatedClient.query as typeof client.request;\n return {\n client,\n graphql,\n };\n}\n\nexport type { FragmentOf, ResultOf, VariablesOf } from \"gql.tada\";\nexport { readFragment } from \"gql.tada\";\nexport { createTheGraphClientWithPagination } from \"./utils/pagination.js\";\n"],"mappings":";;;;;;;;;;;;AAOA,MAAM,kBAAkB;AACxB,MAAM,YAAY;AAClB,MAAM,WAAW;AACjB,MAAM,sBAAsB;;;;;;;;;;;;;AA2B5B,SAAS,uBAAuBA,UAG9B;CACA,MAAM,iBAAiB,IAAI;CAE3B,MAAM,mBAAmB,MAAM,UAAU,EACvC,MAAM,MAAM;AAEV,MAAI,KAAK,cAAc,KAAK,WAAW,SAAS,GAAG;GACjD,MAAM,cAAc,KAAK,WAAW,KAAK,CAAC,QAAQ,IAAI,KAAK,UAAU,oBAAoB;AAEzF,OAAI,aAAa;IACf,MAAM,kBAAkB,KAAK,OAAO,SAAS,KAAK,KAAK;AACvD,mBAAe,IAAI,gBAAgB;AAGnC,WAAO;KACL,GAAG;KACH,YAAY,KAAK,WAAW,OAAO,CAA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Aa,sBAAsB,EAAE,OAAO;CAC1C,WAAW,EAAE,MAAM,gBAAgB;CACnC,aAAa,6BAA6B,UAAU;CACpD,cAAc,EAAE,QAAQ;CACxB,OAAO,EAAE,KAAK;EAAC;EAAW;EAAe;EAAY;EAAY;EAAkB;CAAS,EAAC,CAAC,UAAU;AACzG,EAAC;;;;;;;;AAcF,SAAS,WAAWoB,SAAgC;CAClD,MAAM,WAAW,QAAQ,UAAU,KAAK,CAACC,eAAa,WAAS,UAAU,GAAG,QAAQ,aAAa,EAAE,CAAC;AACpG,MAAK,UAAU;AACb,QAAM,IAAI,OAAO,wBAAwB,QAAQ,aAAa;CAC/D;AACD,QAAO,IAAI,IAAI,UAAU,UAAU;AACpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkDD,SAAgB,qBACdD,SACAE,eAIA;AACA,eAAc;CACd,MAAM,mBAAmB,SAAS,qBAAqB,QAAQ;CAC/D,MAAM,UAAU,iBAAwB;CACxC,MAAM,UAAU,WAAW,iBAAiB;CAE5C,MAAM,SAAS,IAAI,cAAc,SAAS;EACxC,GAAG;EACH,SAAS,cAAc,eAAe,SAAS,EAAE,gBAAgB,iBAAiB,YAAa,EAAC;CACjG;CAGD,MAAM,kBAAkB,mCAAmC,OAAO;AAClE,QAAO,UAAU,gBAAgB;AACjC,QAAO;EACL;EACA;CACD;AACF"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@settlemint/sdk-thegraph",
|
|
3
3
|
"description": "TheGraph integration module for SettleMint SDK, enabling querying and indexing of blockchain data through subgraphs",
|
|
4
|
-
"version": "2.5.3-
|
|
4
|
+
"version": "2.5.3-prfa3ab2c1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
7
7
|
"license": "FSL-1.1-MIT",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@settlemint/sdk-utils": "2.5.3-
|
|
56
|
+
"@settlemint/sdk-utils": "2.5.3-prfa3ab2c1",
|
|
57
57
|
"es-toolkit": "^1",
|
|
58
58
|
"gql.tada": "^1",
|
|
59
59
|
"graphql": "^16",
|