Skip to content

V2: Support disabled queries consistently #427

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

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export function createUseInfiniteQueryOptions<
>;
structuralSharing?: Exclude<UseQueryOptions["structuralSharing"], undefined>;
initialPageParam: MessageInitShape<I>[ParamKey];
enabled: boolean;
enabled?: false;
} {
const queryKey = createConnectInfiniteQueryKey(
schema,
Expand All @@ -196,6 +196,6 @@ export function createUseInfiniteQueryOptions<
pageParamKey,
}),
structuralSharing,
enabled: input !== disableQuery,
...(input === disableQuery ? { enabled: false } : undefined),
};
}
4 changes: 2 additions & 2 deletions packages/connect-query/src/create-use-query-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function createUseQueryOptions<
queryKey: ConnectQueryKey<I>;
queryFn: QueryFunction<MessageShape<O>, ConnectQueryKey<I>>;
structuralSharing?: Exclude<UseQueryOptions["structuralSharing"], undefined>;
enabled: boolean | undefined;
enabled?: false;
} {
const queryKey = createConnectQueryKey(schema, input);
const structuralSharing = createStructuralSharing(schema.output);
Expand All @@ -127,6 +127,6 @@ export function createUseQueryOptions<
callOptions,
}),
structuralSharing,
enabled: input === disableQuery ? false : undefined,
...(input === disableQuery ? { enabled: false } : undefined),
};
}
18 changes: 10 additions & 8 deletions packages/connect-query/src/use-infinite-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function useInfiniteQuery<
callOptions,
pageParamKey,
getNextPageParam,
...options
...queryOptions
}: Omit<CreateInfiniteQueryOptions<I, O, ParamKey>, "transport"> & {
transport?: Transport;
},
Expand All @@ -66,10 +66,10 @@ export function useInfiniteQuery<
pageParamKey,
callOptions,
});
// The query cannot be enabled if the base options are disabled, regardless of
// incoming query options.
const enabled = baseOptions.enabled && (options.enabled ?? true);
return tsUseInfiniteQuery({ ...options, ...baseOptions, enabled });
return tsUseInfiniteQuery({
...queryOptions,
...baseOptions,
});
}

/**
Expand All @@ -87,7 +87,7 @@ export function useSuspenseInfiniteQuery<
callOptions,
pageParamKey,
getNextPageParam,
...options
...queryOptions
}: Omit<CreateSuspenseInfiniteQueryOptions<I, O, ParamKey>, "transport"> & {
transport?: Transport;
},
Expand All @@ -99,6 +99,8 @@ export function useSuspenseInfiniteQuery<
pageParamKey,
callOptions,
});

return tsUseSuspenseInfiniteQuery({ ...options, ...baseOptions });
return tsUseSuspenseInfiniteQuery({
...queryOptions,
...baseOptions,
});
}
14 changes: 3 additions & 11 deletions packages/connect-query/src/use-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,10 @@ export function useQuery<
transport: transport ?? transportFromCtx,
callOptions,
});
const { enabled: baseEnabled, ...baseRest } = baseOptions;
const tsOpts = {
return tsUseQuery({
...queryOptions,
...baseRest,
};
// The query cannot be enabled if the base options are disabled, regardless of
// incoming query options.
const enabled = baseEnabled ?? queryOptions.enabled;
if (enabled !== undefined) {
tsOpts.enabled = enabled;
}
return tsUseQuery(tsOpts);
...baseOptions,
});
}

/**
Expand Down