From 1d8bf31421ea83b9bd5632d4eb821dbafa64bbb7 Mon Sep 17 00:00:00 2001 From: Timo Stamm Date: Fri, 27 Sep 2024 19:10:26 +0200 Subject: [PATCH 1/4] Replace disableQuery with skipToken, allow overriding computed options Signed-off-by: Timo Stamm --- .../src/connect-query-key.test.ts | 6 +- .../connect-query/src/connect-query-key.ts | 10 ++-- .../src/create-use-infinite-query-options.ts | 55 ++++++++++--------- .../src/create-use-query-options.test.ts | 46 ++++++++++++++++ .../src/create-use-query-options.ts | 32 +++++------ packages/connect-query/src/index.ts | 4 +- .../src/use-infinite-query.test.ts | 7 +-- .../connect-query/src/use-infinite-query.ts | 8 +-- packages/connect-query/src/use-mutation.ts | 8 ++- packages/connect-query/src/use-query.test.ts | 36 ++---------- packages/connect-query/src/use-query.ts | 8 +-- packages/connect-query/src/utils.ts | 10 ---- 12 files changed, 123 insertions(+), 107 deletions(-) create mode 100644 packages/connect-query/src/create-use-query-options.test.ts diff --git a/packages/connect-query/src/connect-query-key.test.ts b/packages/connect-query/src/connect-query-key.test.ts index 168b5661..c615a883 100644 --- a/packages/connect-query/src/connect-query-key.test.ts +++ b/packages/connect-query/src/connect-query-key.test.ts @@ -13,11 +13,11 @@ // limitations under the License. import { create } from "@bufbuild/protobuf"; +import { skipToken } from "@tanstack/react-query"; import { describe, expect, it } from "vitest"; import { createConnectQueryKey } from "./connect-query-key.js"; import { ElizaService, SayRequestSchema } from "./gen/eliza_pb.js"; -import { disableQuery } from "./utils.js"; describe("makeQueryKey", () => { const methodDescriptor = { @@ -46,8 +46,8 @@ describe("makeQueryKey", () => { ]); }); - it("makes a query key with a disabled input", () => { - const key = createConnectQueryKey(methodDescriptor, disableQuery); + it("makes a query key with a skipToken", () => { + const key = createConnectQueryKey(methodDescriptor, skipToken); expect(key).toStrictEqual([ ElizaService.typeName, "name", diff --git a/packages/connect-query/src/connect-query-key.ts b/packages/connect-query/src/connect-query-key.ts index 13842178..f4eb07fb 100644 --- a/packages/connect-query/src/connect-query-key.ts +++ b/packages/connect-query/src/connect-query-key.ts @@ -18,10 +18,10 @@ import type { MessageShape, } from "@bufbuild/protobuf"; import { create } from "@bufbuild/protobuf"; +import type { SkipToken } from "@tanstack/react-query"; +import { skipToken } from "@tanstack/react-query"; import type { MethodUnaryDescriptor } from "./method-unary-descriptor.js"; -import type { DisableQuery } from "./utils.js"; -import { disableQuery } from "./utils.js"; /** * TanStack Query requires query keys in order to decide when the query should automatically update. @@ -55,12 +55,12 @@ export function createConnectQueryKey< O extends DescMessage, >( schema: Pick, "input" | "parent" | "name">, - input?: DisableQuery | MessageInitShape | undefined, + input?: SkipToken | MessageInitShape | undefined, ): ConnectQueryKey { return [ schema.parent.typeName, schema.name, - create(schema.input, input === disableQuery || !input ? undefined : input), + create(schema.input, input === skipToken || !input ? undefined : input), ]; } @@ -82,7 +82,7 @@ export function createConnectInfiniteQueryKey< O extends DescMessage, >( schema: Pick, "input" | "parent" | "name">, - input?: DisableQuery | MessageInitShape | undefined, + input?: SkipToken | MessageInitShape | undefined, ): ConnectInfiniteQueryKey { return [...createConnectQueryKey(schema, input), "infinite"]; } diff --git a/packages/connect-query/src/create-use-infinite-query-options.ts b/packages/connect-query/src/create-use-infinite-query-options.ts index a5dcd47c..71c5f2f5 100644 --- a/packages/connect-query/src/create-use-infinite-query-options.ts +++ b/packages/connect-query/src/create-use-infinite-query-options.ts @@ -21,11 +21,14 @@ import type { CallOptions, ConnectError, Transport } from "@connectrpc/connect"; import type { GetNextPageParamFunction, InfiniteData, + Optional, QueryFunction, + SkipToken, UseInfiniteQueryOptions, UseQueryOptions, UseSuspenseInfiniteQueryOptions, } from "@tanstack/react-query"; +import { skipToken } from "@tanstack/react-query"; import { callUnaryMethod } from "./call-unary-method.js"; import { @@ -34,7 +37,7 @@ import { } from "./connect-query-key.js"; import type { MethodUnaryDescriptor } from "./method-unary-descriptor.js"; import { createStructuralSharing } from "./structural-sharing.js"; -import { assert, type DisableQuery, disableQuery } from "./utils.js"; +import { assert } from "./utils.js"; /** * Options specific to connect-query @@ -65,7 +68,7 @@ export type CreateInfiniteQueryOptions< O extends DescMessage, ParamKey extends keyof MessageInitShape, > = ConnectInfiniteQueryOptions & - Omit< + Optional< UseInfiniteQueryOptions< MessageShape, ConnectError, @@ -74,7 +77,7 @@ export type CreateInfiniteQueryOptions< ConnectInfiniteQueryKey, MessageInitShape[ParamKey] >, - "getNextPageParam" | "initialPageParam" | "queryFn" | "queryKey" + "queryKey" >; /** @@ -85,7 +88,7 @@ export type CreateSuspenseInfiniteQueryOptions< O extends DescMessage, ParamKey extends keyof MessageInitShape, > = ConnectInfiniteQueryOptions & - Omit< + Optional< UseSuspenseInfiniteQueryOptions< MessageShape, ConnectError, @@ -94,7 +97,7 @@ export type CreateSuspenseInfiniteQueryOptions< ConnectInfiniteQueryKey, MessageInitShape[ParamKey] >, - "getNextPageParam" | "initialPageParam" | "queryFn" | "queryKey" + "queryKey" >; function createUnaryInfiniteQueryFn< @@ -103,7 +106,7 @@ function createUnaryInfiniteQueryFn< ParamKey extends keyof MessageInitShape, >( schema: MethodUnaryDescriptor, - input: DisableQuery | MessageInitShape, + input: MessageInitShape, { callOptions, transport, @@ -119,7 +122,6 @@ function createUnaryInfiniteQueryFn< MessageInitShape[ParamKey] > { return async (context) => { - assert(input !== disableQuery, "Disabled query cannot be fetched"); assert("pageParam" in context, "pageParam must be part of context"); const inputCombinedWithPageParam = { @@ -138,9 +140,6 @@ function createUnaryInfiniteQueryFn< /** * Query the method provided. Maps to useInfiniteQuery on tanstack/react-query - * - * @param schema - * @returns */ export function createUseInfiniteQueryOptions< I extends DescMessage, @@ -149,7 +148,7 @@ export function createUseInfiniteQueryOptions< >( schema: MethodUnaryDescriptor, input: - | DisableQuery + | SkipToken | (MessageInitShape & Required, ParamKey>>), { transport, @@ -164,18 +163,19 @@ export function createUseInfiniteQueryOptions< ParamKey >["getNextPageParam"]; queryKey: ConnectInfiniteQueryKey; - queryFn: QueryFunction< - MessageShape, - ConnectInfiniteQueryKey, - MessageInitShape[ParamKey] - >; - structuralSharing?: Exclude; + queryFn: + | QueryFunction< + MessageShape, + ConnectInfiniteQueryKey, + MessageInitShape[ParamKey] + > + | SkipToken; + structuralSharing: Exclude; initialPageParam: MessageInitShape[ParamKey]; - enabled?: false; } { const queryKey = createConnectInfiniteQueryKey( schema, - input === disableQuery + input === skipToken ? undefined : { ...input, @@ -183,19 +183,22 @@ export function createUseInfiniteQueryOptions< }, ); const structuralSharing = createStructuralSharing(schema.output); + const queryFn = + input === skipToken + ? skipToken + : createUnaryInfiniteQueryFn(schema, input, { + transport, + callOptions, + pageParamKey, + }); return { getNextPageParam, initialPageParam: - input === disableQuery + input === skipToken ? (undefined as MessageInitShape[ParamKey]) : (input[pageParamKey] as MessageInitShape[ParamKey]), queryKey, - queryFn: createUnaryInfiniteQueryFn(schema, input, { - transport, - callOptions, - pageParamKey, - }), + queryFn, structuralSharing, - ...(input === disableQuery ? { enabled: false } : undefined), }; } diff --git a/packages/connect-query/src/create-use-query-options.test.ts b/packages/connect-query/src/create-use-query-options.test.ts new file mode 100644 index 00000000..e3c566fc --- /dev/null +++ b/packages/connect-query/src/create-use-query-options.test.ts @@ -0,0 +1,46 @@ +// Copyright 2021-2023 The Connect Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { skipToken } from "@tanstack/react-query"; +import { describe, expect, it } from "vitest"; + +import { createConnectQueryKey } from "./connect-query-key.js"; +import { createUseQueryOptions } from "./create-use-query-options.js"; +import { ElizaService } from "./gen/eliza_pb.js"; +import { mockEliza } from "./test/test-utils.js"; + +// TODO: maybe create a helper to take a service and method and generate this. +const sayMethodDescriptor = ElizaService.method.say; + +const mockedElizaTransport = mockEliza(); + +describe("createUseQueryOptions", () => { + it("honors skipToken", () => { + const opt = createUseQueryOptions(sayMethodDescriptor, skipToken, { + transport: mockedElizaTransport, + }); + expect(opt.queryFn).toBe(skipToken); + }); + it("sets queryKey", () => { + const want = createConnectQueryKey(sayMethodDescriptor, { sentence: "hi" }); + const opt = createUseQueryOptions( + sayMethodDescriptor, + { sentence: "hi" }, + { + transport: mockedElizaTransport, + }, + ); + expect(opt.queryKey).toStrictEqual(want); + }); +}); diff --git a/packages/connect-query/src/create-use-query-options.ts b/packages/connect-query/src/create-use-query-options.ts index c8f05efa..e113c8ae 100644 --- a/packages/connect-query/src/create-use-query-options.ts +++ b/packages/connect-query/src/create-use-query-options.ts @@ -19,17 +19,19 @@ import type { } from "@bufbuild/protobuf"; import type { CallOptions, ConnectError, Transport } from "@connectrpc/connect"; import type { + Optional, QueryFunction, + SkipToken, UseQueryOptions, UseSuspenseQueryOptions, } from "@tanstack/react-query"; +import { skipToken } from "@tanstack/react-query"; import { callUnaryMethod } from "./call-unary-method.js"; import type { ConnectQueryKey } from "./connect-query-key.js"; import { createConnectQueryKey } from "./connect-query-key.js"; import type { MethodUnaryDescriptor } from "./method-unary-descriptor.js"; import { createStructuralSharing } from "./structural-sharing.js"; -import { assert, type DisableQuery, disableQuery } from "./utils.js"; export interface ConnectQueryOptions { /** The transport to be used for the fetching. */ @@ -46,14 +48,14 @@ export type CreateQueryOptions< O extends DescMessage, SelectOutData = MessageShape, > = ConnectQueryOptions & - Omit< + Optional< UseQueryOptions< MessageShape, ConnectError, SelectOutData, ConnectQueryKey >, - "queryFn" | "queryKey" + "queryKey" >; /** @@ -64,19 +66,19 @@ export type CreateSuspenseQueryOptions< O extends DescMessage, SelectOutData = 0, > = ConnectQueryOptions & - Omit< + Optional< UseSuspenseQueryOptions< MessageShape, ConnectError, SelectOutData, ConnectQueryKey >, - "queryFn" | "queryKey" + "queryKey" >; function createUnaryQueryFn( schema: MethodUnaryDescriptor, - input: DisableQuery | MessageInitShape | undefined, + input: MessageInitShape | undefined, { callOptions, transport, @@ -86,7 +88,6 @@ function createUnaryQueryFn( }, ): QueryFunction, ConnectQueryKey> { return async (context) => { - assert(input !== disableQuery, "Disabled query cannot be fetched"); return callUnaryMethod(schema, input, { callOptions: { ...callOptions, @@ -105,7 +106,7 @@ export function createUseQueryOptions< O extends DescMessage, >( schema: MethodUnaryDescriptor, - input: DisableQuery | MessageInitShape | undefined, + input: SkipToken | MessageInitShape | undefined, { transport, callOptions, @@ -114,19 +115,18 @@ export function createUseQueryOptions< }, ): { queryKey: ConnectQueryKey; - queryFn: QueryFunction, ConnectQueryKey>; - structuralSharing?: Exclude; - enabled?: false; + queryFn: QueryFunction, ConnectQueryKey> | SkipToken; + structuralSharing: Exclude; } { const queryKey = createConnectQueryKey(schema, input); const structuralSharing = createStructuralSharing(schema.output); + const queryFn = + input === skipToken + ? skipToken + : createUnaryQueryFn(schema, input, { transport, callOptions }); return { queryKey, - queryFn: createUnaryQueryFn(schema, input, { - transport, - callOptions, - }), + queryFn, structuralSharing, - ...(input === disableQuery ? { enabled: false } : undefined), }; } diff --git a/packages/connect-query/src/index.ts b/packages/connect-query/src/index.ts index 2fa0beb3..6b90089b 100644 --- a/packages/connect-query/src/index.ts +++ b/packages/connect-query/src/index.ts @@ -20,7 +20,7 @@ export { createConnectQueryKey, createConnectInfiniteQueryKey, } from "./connect-query-key.js"; -export { disableQuery, createProtobufSafeUpdater } from "./utils.js"; +export { createProtobufSafeUpdater } from "./utils.js"; export { useTransport, TransportProvider } from "./use-transport.js"; export type { CreateInfiniteQueryOptions as UseInfiniteQueryOptions } from "./create-use-infinite-query-options.js"; export { @@ -32,7 +32,7 @@ export { useQuery, useSuspenseQuery } from "./use-query.js"; export type { UseMutationOptions } from "./use-mutation.js"; export { useMutation } from "./use-mutation.js"; export { defaultOptions } from "./default-options.js"; -export type { DisableQuery, ConnectUpdater } from "./utils.js"; +export type { ConnectUpdater } from "./utils.js"; export { callUnaryMethod } from "./call-unary-method.js"; export type { MethodUnaryDescriptor } from "./method-unary-descriptor.js"; export { createUseInfiniteQueryOptions as createInfiniteQueryOptions } from "./create-use-infinite-query-options.js"; diff --git a/packages/connect-query/src/use-infinite-query.test.ts b/packages/connect-query/src/use-infinite-query.test.ts index 4907ded0..9017f05a 100644 --- a/packages/connect-query/src/use-infinite-query.test.ts +++ b/packages/connect-query/src/use-infinite-query.test.ts @@ -13,7 +13,7 @@ // limitations under the License. import { create } from "@bufbuild/protobuf"; -import { QueryCache } from "@tanstack/react-query"; +import { QueryCache, skipToken } from "@tanstack/react-query"; import { renderHook, waitFor } from "@testing-library/react"; import { describe, expect, it, vi } from "vitest"; @@ -29,7 +29,6 @@ import { useSuspenseInfiniteQuery, } from "./use-infinite-query.js"; import { useQuery } from "./use-query.js"; -import { disableQuery } from "./utils.js"; // TODO: maybe create a helper to take a service and method and generate this. const methodDescriptor = ListService.method.list; @@ -93,10 +92,10 @@ describe("useInfiniteQuery", () => { }); }); - it("can be disabled", () => { + it("can be disabled with skipToken", () => { const { result } = renderHook( () => { - return useInfiniteQuery(methodDescriptor, disableQuery, { + return useInfiniteQuery(methodDescriptor, skipToken, { getNextPageParam: (lastPage) => lastPage.page + 1n, pageParamKey: "page", }); diff --git a/packages/connect-query/src/use-infinite-query.ts b/packages/connect-query/src/use-infinite-query.ts index 496d5ae4..87844ec8 100644 --- a/packages/connect-query/src/use-infinite-query.ts +++ b/packages/connect-query/src/use-infinite-query.ts @@ -20,6 +20,7 @@ import type { import type { ConnectError, Transport } from "@connectrpc/connect"; import type { InfiniteData, + SkipToken, UseInfiniteQueryResult, UseSuspenseInfiniteQueryResult, } from "@tanstack/react-query"; @@ -35,7 +36,6 @@ import type { import { createUseInfiniteQueryOptions } from "./create-use-infinite-query-options.js"; import type { MethodUnaryDescriptor } from "./method-unary-descriptor.js"; import { useTransport } from "./use-transport.js"; -import type { DisableQuery } from "./utils.js"; /** * Query the method provided. Maps to useInfiniteQuery on tanstack/react-query @@ -47,7 +47,7 @@ export function useInfiniteQuery< >( schema: MethodUnaryDescriptor, input: - | DisableQuery + | SkipToken | (MessageInitShape & Required, ParamKey>>), { transport, @@ -67,8 +67,8 @@ export function useInfiniteQuery< callOptions, }); return tsUseInfiniteQuery({ - ...queryOptions, ...baseOptions, + ...queryOptions, }); } @@ -100,7 +100,7 @@ export function useSuspenseInfiniteQuery< callOptions, }); return tsUseSuspenseInfiniteQuery({ - ...queryOptions, ...baseOptions, + ...queryOptions, }); } diff --git a/packages/connect-query/src/use-mutation.ts b/packages/connect-query/src/use-mutation.ts index 95589e45..c9fffd49 100644 --- a/packages/connect-query/src/use-mutation.ts +++ b/packages/connect-query/src/use-mutation.ts @@ -36,9 +36,11 @@ export type UseMutationOptions< I extends DescMessage, O extends DescMessage, Ctx = unknown, -> = Omit< - TSUseMutationOptions, ConnectError, MessageInitShape, Ctx>, - "mutationFn" +> = TSUseMutationOptions< + MessageShape, + ConnectError, + MessageInitShape, + Ctx > & { transport?: Transport; callOptions?: Omit; diff --git a/packages/connect-query/src/use-query.test.ts b/packages/connect-query/src/use-query.test.ts index 0c730f1b..2e553d0a 100644 --- a/packages/connect-query/src/use-query.test.ts +++ b/packages/connect-query/src/use-query.test.ts @@ -13,13 +13,13 @@ // limitations under the License. import { create } from "@bufbuild/protobuf"; +import { skipToken } from "@tanstack/react-query"; import { renderHook, waitFor } from "@testing-library/react"; import { describe, expect, it } from "vitest"; import { ElizaService } from "./gen/eliza_pb.js"; import { mockEliza, wrapper } from "./test/test-utils.js"; import { useQuery, useSuspenseQuery } from "./use-query.js"; -import { disableQuery } from "./utils.js"; // TODO: maybe create a helper to take a service and method and generate this. const sayMethodDescriptor = ElizaService.method.say; @@ -49,7 +49,7 @@ describe("useQuery", () => { it("can be disabled", () => { const { result } = renderHook( () => { - return useQuery(sayMethodDescriptor, disableQuery); + return useQuery(sayMethodDescriptor, skipToken); }, wrapper(undefined, mockedElizaTransport), ); @@ -119,7 +119,7 @@ describe("useQuery", () => { expect(result.current.data).toBe(6); }); - it("can be disabled without explicit disableQuery", () => { + it("can be disabled with enabled: false", () => { const { result } = renderHook( () => { return useQuery( @@ -140,7 +140,7 @@ describe("useQuery", () => { expect(result.current.isFetching).toBeFalsy(); }); - it("can be disabled with QueryClient default options", () => { + it("can be disabled with enabled: false in QueryClient default options", () => { const { result } = renderHook( () => { return useQuery(sayMethodDescriptor, { @@ -164,34 +164,10 @@ describe("useQuery", () => { expect(result.current.isFetching).toBeFalsy(); }); - it("cannot be enabled with QueryClient default options with explicit disableQuery", () => { + it("can be disabled with skipToken", () => { const { result } = renderHook( () => { - return useQuery(sayMethodDescriptor, disableQuery); - }, - wrapper( - { - defaultOptions: { - queries: { - enabled: true, - }, - }, - }, - mockedElizaTransport, - ), - ); - - expect(result.current.data).toBeUndefined(); - expect(result.current.isPending).toBeTruthy(); - expect(result.current.isFetching).toBeFalsy(); - }); - - it("disableQuery will override explicit enabled", () => { - const { result } = renderHook( - () => { - return useQuery(sayMethodDescriptor, disableQuery, { - enabled: true, - }); + return useQuery(sayMethodDescriptor, skipToken); }, wrapper({}, mockedElizaTransport), ); diff --git a/packages/connect-query/src/use-query.ts b/packages/connect-query/src/use-query.ts index 434bd97c..d56a8455 100644 --- a/packages/connect-query/src/use-query.ts +++ b/packages/connect-query/src/use-query.ts @@ -19,6 +19,7 @@ import type { } from "@bufbuild/protobuf"; import type { ConnectError, Transport } from "@connectrpc/connect"; import type { + SkipToken, UseQueryResult, UseSuspenseQueryResult, } from "@tanstack/react-query"; @@ -34,7 +35,6 @@ import type { import { createUseQueryOptions } from "./create-use-query-options.js"; import type { MethodUnaryDescriptor } from "./method-unary-descriptor.js"; import { useTransport } from "./use-transport.js"; -import type { DisableQuery } from "./utils.js"; /** * Query the method provided. Maps to useQuery on tanstack/react-query @@ -45,7 +45,7 @@ export function useQuery< SelectOutData = MessageShape, >( schema: MethodUnaryDescriptor, - input?: DisableQuery | MessageInitShape, + input?: SkipToken | MessageInitShape, { transport, callOptions, @@ -60,8 +60,8 @@ export function useQuery< callOptions, }); return tsUseQuery({ - ...queryOptions, ...baseOptions, + ...queryOptions, }); } @@ -89,7 +89,7 @@ export function useSuspenseQuery< callOptions, }); return tsUseSuspenseQuery({ - ...queryOptions, ...baseOptions, + ...queryOptions, }); } diff --git a/packages/connect-query/src/utils.ts b/packages/connect-query/src/utils.ts index a4e8140f..ff1c569f 100644 --- a/packages/connect-query/src/utils.ts +++ b/packages/connect-query/src/utils.ts @@ -21,16 +21,6 @@ import { create } from "@bufbuild/protobuf"; import type { MethodUnaryDescriptor } from "./method-unary-descriptor.js"; -/** - * Pass this value as an input to signal that you want to disable the query. - */ -export const disableQuery = Symbol("disableQuery"); - -/** - * Use this type in situations where you want to disable a query from use. - */ -export type DisableQuery = typeof disableQuery; - /** * Throws an error with the provided message when the condition is `false` */ From 14229236cef485045018587dbf02b12b9d5a31fc Mon Sep 17 00:00:00 2001 From: Timo Stamm Date: Mon, 30 Sep 2024 14:49:24 +0200 Subject: [PATCH 2/4] Replace disableQuery in the docs Signed-off-by: Timo Stamm --- .eslintrc.js | 1 - README.md | 12 ++++++------ .../connect-query/src/use-infinite-query.test.ts | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index e3473cbe..1241202d 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -71,7 +71,6 @@ const config = { "@typescript-eslint/prefer-readonly-parameter-types": "off", // not realistic "@typescript-eslint/explicit-module-boundary-types": "off", // inference and conformance testing cover this well "@typescript-eslint/explicit-function-return-type": "off", // inference and conformance testing cover this well - "@typescript-eslint/no-unused-expressions": "off", // necessary component of some exports, e.g. DisableQuery "@typescript-eslint/no-type-alias": "off", // this rule turns off things that are absolutely required by this project such as conditional types and literals "@typescript-eslint/no-throw-literal": "off", // unfortunately this rule doesn't understand returns from `unreachableCase` "@typescript-eslint/no-magic-numbers": "off", // literal values are used in CSS-in-JS, tests, and library constants diff --git a/README.md b/README.md index 564ed323..b0cbc9bc 100644 --- a/README.md +++ b/README.md @@ -191,7 +191,7 @@ Use this helper to get the default transport that's currently attached to the Re ```ts function useQuery, O extends Message>( methodSig: MethodUnaryDescriptor, - input?: DisableQuery | PartialMessage, + input?: SkipToken | PartialMessage, options?: { transport?: Transport; callOptions?: CallOptions; @@ -217,7 +217,7 @@ function useInfiniteQuery< Input extends PartialMessage & Required, ParamKey>>, >( methodSig: MethodUnaryDescriptor, - input: DisableQuery | Input, + input: SkipToken | Input, options: { pageParamKey: ParamKey; transport?: Transport; @@ -256,7 +256,7 @@ Any additional `options` you pass to `useMutation` will be merged with the optio ```ts function createConnectQueryKey, O extends Message>( methodDescriptor: Pick, "I" | "name" | "service">, - input?: DisableQuery | PartialMessage | undefined, + input?: SkipToken | PartialMessage | undefined, ): ConnectQueryKey; ``` @@ -270,7 +270,7 @@ function createConnectInfiniteQueryKey< O extends Message, >( methodDescriptor: Pick, "I" | "name" | "service">, - input: DisableQuery | PartialMessage, + input: SkipToken | PartialMessage, pageParamKey: keyof PartialMessage, ): ConnectInfiniteQueryKey; ``` @@ -322,7 +322,7 @@ queryClient.setQueryData( ```ts function createQueryOptions, O extends Message>( methodSig: MethodUnaryDescriptor, - input: DisableQuery | PartialMessage | undefined, + input: SkipToken | PartialMessage | undefined, { transport, callOptions, @@ -365,7 +365,7 @@ function createInfiniteQueryOptions< Input extends PartialMessage & Required, ParamKey>>, >( methodSig: MethodUnaryDescriptor, - input: DisableQuery | Input, + input: SkipToken | Input, { transport, getNextPageParam, diff --git a/packages/connect-query/src/use-infinite-query.test.ts b/packages/connect-query/src/use-infinite-query.test.ts index 9017f05a..15132987 100644 --- a/packages/connect-query/src/use-infinite-query.test.ts +++ b/packages/connect-query/src/use-infinite-query.test.ts @@ -341,7 +341,7 @@ describe("useSuspenseInfiniteQuery", () => { }); }); - it("can be disabled without explicit disableQuery", () => { + it("can be disabled without explicit skipToken", () => { const { result } = renderHook( () => { return useInfiniteQuery( From dd029ca6850c7a3727a29b8f2f4ad5e4c7ddc8f3 Mon Sep 17 00:00:00 2001 From: Paul Sachs <11449728+paul-sachs@users.noreply.github.com> Date: Mon, 30 Sep 2024 10:51:34 -0400 Subject: [PATCH 3/4] Add typechecking to tests (#430) Enabling typechecking tests with vitest. --------- Signed-off-by: Paul Sachs --- examples/react/basic/tsconfig.json | 3 +-- examples/react/basic/tsconfig.node.json | 9 --------- examples/react/basic/{vite.config.js => vite.config.ts} | 5 +++++ packages/connect-query/vite.config.ts | 5 +++++ 4 files changed, 11 insertions(+), 11 deletions(-) delete mode 100644 examples/react/basic/tsconfig.node.json rename examples/react/basic/{vite.config.js => vite.config.ts} (82%) diff --git a/examples/react/basic/tsconfig.json b/examples/react/basic/tsconfig.json index 7c6ac058..7dc71941 100644 --- a/examples/react/basic/tsconfig.json +++ b/examples/react/basic/tsconfig.json @@ -19,6 +19,5 @@ "types": ["node"], "declaration": true // necessary to check if generated code can be published }, - "include": ["src", "./*.config.ts", "__mocks__"], - "references": [{ "path": "./tsconfig.node.json" }] + "include": ["src", "./*.config.ts", "__mocks__"] } diff --git a/examples/react/basic/tsconfig.node.json b/examples/react/basic/tsconfig.node.json deleted file mode 100644 index 9d31e2ae..00000000 --- a/examples/react/basic/tsconfig.node.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "compilerOptions": { - "composite": true, - "module": "ESNext", - "moduleResolution": "Node", - "allowSyntheticDefaultImports": true - }, - "include": ["vite.config.ts"] -} diff --git a/examples/react/basic/vite.config.js b/examples/react/basic/vite.config.ts similarity index 82% rename from examples/react/basic/vite.config.js rename to examples/react/basic/vite.config.ts index bcd8c9db..f1c1bc87 100644 --- a/examples/react/basic/vite.config.js +++ b/examples/react/basic/vite.config.ts @@ -20,5 +20,10 @@ export default defineConfig({ plugins: [react()], test: { environment: "jsdom", + typecheck: { + enabled: true, + // Mofidied to typecheck definition files as well as source files + include: ["**/*.{test,spec}?(-d).?(c|m)[jt]s?(x)"], + }, }, }); diff --git a/packages/connect-query/vite.config.ts b/packages/connect-query/vite.config.ts index 7b899208..18b51924 100644 --- a/packages/connect-query/vite.config.ts +++ b/packages/connect-query/vite.config.ts @@ -18,6 +18,11 @@ import { defineConfig } from "vitest/config"; export default defineConfig({ test: { environment: "jsdom", + typecheck: { + enabled: true, + // Mofidied to typecheck definition files as well as source files + include: ["**/*.{test,spec}?(-d).?(c|m)[jt]s?(x)"], + }, coverage: { provider: "istanbul", thresholds: { From 786fde43808967eda6690a4123e7e15a15a5dfa7 Mon Sep 17 00:00:00 2001 From: Timo Stamm Date: Mon, 30 Sep 2024 17:17:18 +0200 Subject: [PATCH 4/4] Undo allow overriding computed options Signed-off-by: Timo Stamm --- .../src/create-use-infinite-query-options.ts | 9 ++++----- packages/connect-query/src/create-use-query-options.ts | 9 ++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/connect-query/src/create-use-infinite-query-options.ts b/packages/connect-query/src/create-use-infinite-query-options.ts index 71c5f2f5..6fe652ea 100644 --- a/packages/connect-query/src/create-use-infinite-query-options.ts +++ b/packages/connect-query/src/create-use-infinite-query-options.ts @@ -21,7 +21,6 @@ import type { CallOptions, ConnectError, Transport } from "@connectrpc/connect"; import type { GetNextPageParamFunction, InfiniteData, - Optional, QueryFunction, SkipToken, UseInfiniteQueryOptions, @@ -68,7 +67,7 @@ export type CreateInfiniteQueryOptions< O extends DescMessage, ParamKey extends keyof MessageInitShape, > = ConnectInfiniteQueryOptions & - Optional< + Omit< UseInfiniteQueryOptions< MessageShape, ConnectError, @@ -77,7 +76,7 @@ export type CreateInfiniteQueryOptions< ConnectInfiniteQueryKey, MessageInitShape[ParamKey] >, - "queryKey" + "getNextPageParam" | "initialPageParam" | "queryFn" | "queryKey" >; /** @@ -88,7 +87,7 @@ export type CreateSuspenseInfiniteQueryOptions< O extends DescMessage, ParamKey extends keyof MessageInitShape, > = ConnectInfiniteQueryOptions & - Optional< + Omit< UseSuspenseInfiniteQueryOptions< MessageShape, ConnectError, @@ -97,7 +96,7 @@ export type CreateSuspenseInfiniteQueryOptions< ConnectInfiniteQueryKey, MessageInitShape[ParamKey] >, - "queryKey" + "getNextPageParam" | "initialPageParam" | "queryFn" | "queryKey" >; function createUnaryInfiniteQueryFn< diff --git a/packages/connect-query/src/create-use-query-options.ts b/packages/connect-query/src/create-use-query-options.ts index e113c8ae..5cfe8a87 100644 --- a/packages/connect-query/src/create-use-query-options.ts +++ b/packages/connect-query/src/create-use-query-options.ts @@ -19,7 +19,6 @@ import type { } from "@bufbuild/protobuf"; import type { CallOptions, ConnectError, Transport } from "@connectrpc/connect"; import type { - Optional, QueryFunction, SkipToken, UseQueryOptions, @@ -48,14 +47,14 @@ export type CreateQueryOptions< O extends DescMessage, SelectOutData = MessageShape, > = ConnectQueryOptions & - Optional< + Omit< UseQueryOptions< MessageShape, ConnectError, SelectOutData, ConnectQueryKey >, - "queryKey" + "queryFn" | "queryKey" >; /** @@ -66,14 +65,14 @@ export type CreateSuspenseQueryOptions< O extends DescMessage, SelectOutData = 0, > = ConnectQueryOptions & - Optional< + Omit< UseSuspenseQueryOptions< MessageShape, ConnectError, SelectOutData, ConnectQueryKey >, - "queryKey" + "queryFn" | "queryKey" >; function createUnaryQueryFn(