Skip to content

Commit

Permalink
V2: Add overload to make sure createQueryOptions only returns skipTok…
Browse files Browse the repository at this point in the history
…en when could be passed skipToken (#469)

In order to use createQueryOptions with recent versions of react-query
and `useSuspenseQuery`, we need to make sure createQueryOptions isn't
passing `skipToken` as a potential type when the input type couldn't be
skipToken. This is because the typings for `useSuspenseQuery` now
disallow skipping (which was always the case, just was not reflected in
the types.)

To fix this, we overload the `createQueryOptions` call to indicate with
it's types that it will only return a skipToken when input is a
skipToken.

Signed-off-by: Paul Sachs <psachs@buf.build>
  • Loading branch information
paul-sachs authored Oct 28, 2024
1 parent 1d5e7c9 commit 4f29429
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const config = {
"import/resolver": {
typescript: {},
},
vitest: {
typecheck: true,
},
},
rules: {
...vitest.configs.recommended.rules,
Expand Down
3 changes: 2 additions & 1 deletion packages/connect-query/src/create-query-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import { skipToken } from "@tanstack/react-query";
import { describe, expect, it } from "vitest";
import { describe, expect, expectTypeOf, it } from "vitest";

import { createConnectQueryKey } from "./connect-query-key.js";
import { createQueryOptions } from "./create-query-options.js";
Expand All @@ -31,6 +31,7 @@ describe("createQueryOptions", () => {
transport: mockedElizaTransport,
});
expect(opt.queryFn).toBe(skipToken);
expectTypeOf(opt.queryFn).toEqualTypeOf(skipToken);
});
it("sets queryKey", () => {
const want = createConnectQueryKey({
Expand Down
48 changes: 48 additions & 0 deletions packages/connect-query/src/create-query-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,54 @@ function createUnaryQueryFn<I extends DescMessage, O extends DescMessage>(
/**
* Creates all options required to make a query. Useful in combination with `useQueries` from tanstack/react-query.
*/
export function createQueryOptions<
I extends DescMessage,
O extends DescMessage,
>(
schema: DescMethodUnary<I, O>,
input: MessageInitShape<I> | undefined,
{
transport,
}: {
transport: Transport;
},
): {
queryKey: ConnectQueryKey;
queryFn: QueryFunction<MessageShape<O>, ConnectQueryKey>;
structuralSharing: (oldData: unknown, newData: unknown) => unknown;
};
export function createQueryOptions<
I extends DescMessage,
O extends DescMessage,
>(
schema: DescMethodUnary<I, O>,
input: SkipToken,
{
transport,
}: {
transport: Transport;
},
): {
queryKey: ConnectQueryKey;
queryFn: SkipToken;
structuralSharing: (oldData: unknown, newData: unknown) => unknown;
};
export function createQueryOptions<
I extends DescMessage,
O extends DescMessage,
>(
schema: DescMethodUnary<I, O>,
input: SkipToken | MessageInitShape<I> | undefined,
{
transport,
}: {
transport: Transport;
},
): {
queryKey: ConnectQueryKey;
queryFn: QueryFunction<MessageShape<O>, ConnectQueryKey> | SkipToken;
structuralSharing: (oldData: unknown, newData: unknown) => unknown;
};
export function createQueryOptions<
I extends DescMessage,
O extends DescMessage,
Expand Down

0 comments on commit 4f29429

Please sign in to comment.