-
Notifications
You must be signed in to change notification settings - Fork 1
fix: skip empty non-list queries during fetchAll pagination #1395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } | ||
| }, | ||
| }); | ||
|
Comment on lines
+284
to
+291
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't skip
Apply this diff to ensure - Field: (node) => {
- if (!node.name.value.startsWith("__")) {
- if (!node.selectionSet || node.selectionSet.selections.length > 0) {
- fieldCount += 1;
- }
- }
- },
+ Field: (node) => {
+ if (!node.selectionSet || node.selectionSet.selections.length > 0) {
+ fieldCount += 1;
+ }
+ },🤖 Prompt for AI Agents |
||
|
|
||
| return fieldCount; | ||
| } | ||
|
|
||
| // Filter variables to only include used ones | ||
|
|
@@ -416,6 +433,9 @@ export function createTheGraphClientWithPagination(theGraphClient: Pick<GraphQLC | |
| const nonListQuery = createNonListQuery(processedDocument, listFields); | ||
|
|
||
| if (nonListQuery) { | ||
| if (countExecutableFields(nonListQuery) === 0) { | ||
| return result as TResult; | ||
| } | ||
| const nonListResult = await theGraphClient.request( | ||
| nonListQuery, | ||
| filterVariables(variables, nonListQuery) ?? {}, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
countExecutableFields excludes meta fields like __typename, which can cause the non-list request to be skipped and omit requested data when __typename is the only remaining field. Include __typename in the count so the follow-up query executes.
Prompt for AI agents