diff --git a/sdk/thegraph/src/utils/pagination.test.ts b/sdk/thegraph/src/utils/pagination.test.ts index 66e74c9b7..35f42ff94 100644 --- a/sdk/thegraph/src/utils/pagination.test.ts +++ b/sdk/thegraph/src/utils/pagination.test.ts @@ -353,6 +353,29 @@ describe("createTheGraphClientWithPagination", () => { }); }); + it("should return token if only @fetchAll field is requested", async () => { + const result = await client.query( + theGraphGraphql(` + query($name: String) { + token(name: $name) { + holders @fetchAll { + name + } + } + }`), + { + name: "Token 100", + }, + ); + + expect(result).toEqual({ + token: { + holders: TEST_HOLDERS, + }, + }); + expect(requestMock).toHaveBeenCalledTimes(3); + }); + it("should allow multiple queries in a single request", async () => { const result = await client.query( theGraphGraphql(` diff --git a/sdk/thegraph/src/utils/pagination.ts b/sdk/thegraph/src/utils/pagination.ts index 0d56c04b1..03e1bffbd 100644 --- a/sdk/thegraph/src/utils/pagination.ts +++ b/sdk/thegraph/src/utils/pagination.ts @@ -1,7 +1,7 @@ import { sortBy } from "es-toolkit"; import { get, isArray, isEmpty, set } from "es-toolkit/compat"; import type { TadaDocumentNode } from "gql.tada"; -import { type ArgumentNode, type DocumentNode, Kind, parse, visit } from "graphql"; +import { type ArgumentNode, type DocumentNode, type FieldNode, Kind, parse, visit } from "graphql"; import type { GraphQLClient, RequestDocument, RequestOptions, Variables } from "graphql-request"; // Constants for TheGraph limits @@ -244,7 +244,6 @@ function createSingleFieldQuery( // Create query without list fields function createNonListQuery(document: DocumentNode, listFields: ListFieldWithFetchAllDirective[]): DocumentNode | null { - let hasFields = false; const pathStack: string[] = []; const filtered = visit(document, { @@ -263,17 +262,35 @@ function createNonListQuery(document: DocumentNode, listFields: ListFieldWithFet pathStack.pop(); return null; } - - hasFields = true; return undefined; }, - leave: () => { + leave: (node: FieldNode) => { pathStack.pop(); + if (node.selectionSet && node.selectionSet.selections.length === 0) { + return null; + } + return undefined; }, }, }); - return hasFields ? filtered : null; + return filtered; +} + +function countExecutableFields(document: DocumentNode): number { + let fieldCount = 0; + + visit(document, { + Field: (node) => { + if (!node.name.value.startsWith("__")) { + if (!node.selectionSet || node.selectionSet.selections.length > 0) { + fieldCount += 1; + } + } + }, + }); + + return fieldCount; } // Filter variables to only include used ones @@ -416,6 +433,9 @@ export function createTheGraphClientWithPagination(theGraphClient: Pick