Skip to content
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

V2: Remove defaultOptions #445

Merged
merged 1 commit into from
Oct 8, 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
Remove defaultOptions
Signed-off-by: Timo Stamm <ts@timostamm.de>
  • Loading branch information
timostamm committed Oct 8, 2024
commit 3a2e9b3a33d8fcd3e4af88ec7132829a0e659205
15 changes: 0 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,21 +455,6 @@ export const Example: FC = () => {
>
> Originally, all we did was pass options to TanStack Query. This was done as an intentional way to keep ourselves separate from TanStack Query. However, as usage increased, it became obvious that were still tied to the API of TanStack Query, and it only meant that we increased the burden on the developer to understand that underlying connection. This new API removes most of that burden and reduces the surface area of the API significantly.

### Is this ready for production?

Buf has been using Connect-Query in production for some time. Also, there is 100% mandatory test coverage in this project which covers quite a lot of edge cases.

### Using BigInt with RPC inputs

Since Connect-Query use the inputs as keys for the query, if you have a field with type `int64`, those fields will cause serialization problems. For this reason, Connect-Query ships with defaultOptions that can be passed to the QueryClient to make sure serializing BigInt fields is done properly:

```ts
import { defaultOptions } from "@connectrpc/connect-query";
import { QueryClient } from "@tanstack/react-query";

const queryClient = new QueryClient({ defaultOptions });
```

### What is Connect-Query's relationship to Connect-Web and Protobuf-ES?

Here is a high-level overview of how Connect-Query fits in with Connect-Web and Protobuf-ES:
Expand Down
66 changes: 30 additions & 36 deletions packages/connect-query/src/call-unary-method.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { describe, expect, it } from "vitest";
import { callUnaryMethod } from "./call-unary-method.js";
import type { ConnectQueryKey } from "./connect-query-key.js";
import { createConnectQueryKey } from "./connect-query-key.js";
import { defaultOptions } from "./default-options.js";
import type { SayRequest } from "./gen/eliza_pb.js";
import { ElizaService, SayRequestSchema } from "./gen/eliza_pb.js";
import { mockEliza, wrapper } from "./test/test-utils.js";
Expand All @@ -31,43 +30,38 @@ describe("callUnaryMethod", () => {
const transport = mockEliza({
sentence: "Response 1",
});
const { result } = renderHook(
() => {
const input: SayRequest = create(SayRequestSchema, {
sentence: "query 1",
});
const [query1] = useQueries({
queries: [
{
queryKey: createConnectQueryKey({
schema: ElizaService.method.say,
input,
const { result } = renderHook(() => {
const input: SayRequest = create(SayRequestSchema, {
sentence: "query 1",
});
const [query1] = useQueries({
queries: [
{
queryKey: createConnectQueryKey({
schema: ElizaService.method.say,
input,
transport,
}),
queryFn: async ({
signal,
}: QueryFunctionContext<ConnectQueryKey>) => {
const res = await callUnaryMethod(
transport,
}),
queryFn: async ({
signal,
}: QueryFunctionContext<ConnectQueryKey>) => {
const res = await callUnaryMethod(
transport,
ElizaService.method.say,
input,
{
signal,
},
);
return res;
},
ElizaService.method.say,
input,
{
signal,
},
);
return res;
},
],
});
return {
query1,
};
},
wrapper({
defaultOptions,
}),
);
},
],
});
return {
query1,
};
}, wrapper());

await waitFor(() => {
expect(result.current.query1.isSuccess).toBeTruthy();
Expand Down
28 changes: 0 additions & 28 deletions packages/connect-query/src/default-options.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/connect-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export {
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 { ConnectUpdater } from "./utils.js";
export { callUnaryMethod } from "./call-unary-method.js";
export type { MethodUnaryDescriptor } from "./method-unary-descriptor.js";
Expand Down
6 changes: 1 addition & 5 deletions packages/connect-query/src/test/test-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import type { QueryClientConfig } from "@tanstack/react-query";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import type { JSXElementConstructor, PropsWithChildren } from "react";

import { defaultOptions } from "../default-options.js";
import {
BigIntService,
type CountRequest,
Expand Down Expand Up @@ -49,10 +48,7 @@ export const wrapper = (
transport: Transport;
queryClientWrapper: JSXElementConstructor<PropsWithChildren>;
} => {
const queryClient = new QueryClient({
defaultOptions,
...config,
});
const queryClient = new QueryClient(config);
return {
wrapper: ({ children }) => (
<TransportProvider transport={transport}>
Expand Down
64 changes: 10 additions & 54 deletions packages/connect-query/src/use-infinite-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { renderHook, waitFor } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";

import { createConnectQueryKey } from "./connect-query-key.js";
import { defaultOptions } from "./default-options.js";
import { ListResponseSchema, ListService } from "./gen/list_pb.js";
import { mockPaginatedTransport, wrapper } from "./test/test-utils.js";
import {
Expand Down Expand Up @@ -47,12 +46,7 @@ describe("useInfiniteQuery", () => {
},
);
},
wrapper(
{
defaultOptions,
},
mockedPaginatedTransport,
),
wrapper({}, mockedPaginatedTransport),
);

await waitFor(() => {
Expand Down Expand Up @@ -122,12 +116,7 @@ describe("useInfiniteQuery", () => {
},
);
},
wrapper(
{
defaultOptions,
},
mockedPaginatedTransport,
),
wrapper({}, mockedPaginatedTransport),
);
await waitFor(() => {
expect(result.current.isSuccess).toBeTruthy();
Expand Down Expand Up @@ -160,21 +149,14 @@ describe("useInfiniteQuery", () => {
},
);
},
wrapper(
{
defaultOptions,
},
mockedPaginatedTransport,
),
wrapper({}, mockedPaginatedTransport),
);
expect(result.current.data?.pages[0].page).toEqual(-1n);
});

it("page param doesn't persist to the query cache", async () => {
const { queryClient, ...remainingWrapper } = wrapper(
{
defaultOptions,
},
{},
mockedPaginatedTransport,
);
const { result } = renderHook(() => {
Expand Down Expand Up @@ -211,12 +193,7 @@ describe("useInfiniteQuery", () => {
});

it("doesn't share data with a similar non-infinite query", async () => {
const remainingWrapper = wrapper(
{
defaultOptions,
},
mockedPaginatedTransport,
);
const remainingWrapper = wrapper({}, mockedPaginatedTransport);
const { result } = renderHook(() => {
return useInfiniteQuery(
methodDescriptor,
Expand Down Expand Up @@ -255,10 +232,7 @@ describe("useInfiniteQuery", () => {
onSuccess: onSuccessSpy,
});
const { queryClient, ...remainingWrapper } = wrapper(
{
defaultOptions,
queryCache: spiedQueryCache,
},
{ queryCache: spiedQueryCache },
mockedPaginatedTransport,
);
const { result } = renderHook(() => {
Expand Down Expand Up @@ -301,10 +275,7 @@ describe("useInfiniteQuery", () => {
onSuccess: onSuccessSpy,
});
const { queryClient, ...remainingWrapper } = wrapper(
{
defaultOptions,
queryCache: spiedQueryCache,
},
{ queryCache: spiedQueryCache },
mockedPaginatedTransport,
);
const { result } = renderHook(() => {
Expand Down Expand Up @@ -353,12 +324,7 @@ describe("useSuspenseInfiniteQuery", () => {
},
);
},
wrapper(
{
defaultOptions,
},
mockedPaginatedTransport,
),
wrapper({}, mockedPaginatedTransport),
);

await waitFor(() => {
Expand Down Expand Up @@ -410,12 +376,7 @@ describe("useSuspenseInfiniteQuery", () => {
},
);
},
wrapper(
{
defaultOptions,
},
mockedPaginatedTransport,
),
wrapper({}, mockedPaginatedTransport),
);

expect(result.current.data).toBeUndefined();
Expand All @@ -440,12 +401,7 @@ describe("useSuspenseInfiniteQuery", () => {
},
);
},
wrapper(
{
defaultOptions,
},
mockedPaginatedTransport,
),
wrapper({}, mockedPaginatedTransport),
);
});
});
22 changes: 3 additions & 19 deletions packages/connect-query/src/use-mutation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { create } from "@bufbuild/protobuf";
import { renderHook, waitFor } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";

import { defaultOptions } from "./default-options.js";
import { ListResponseSchema, ListService } from "./gen/list_pb.js";
import { mockPaginatedTransport, wrapper } from "./test/test-utils.js";
import { useMutation } from "./use-mutation.js";
Expand All @@ -35,12 +34,7 @@ describe("useMutation", () => {
onSuccess,
});
},
wrapper(
{
defaultOptions,
},
mockedPaginatedTransport,
),
wrapper({}, mockedPaginatedTransport),
);

result.current.mutate({
Expand Down Expand Up @@ -73,12 +67,7 @@ describe("useMutation", () => {
}),
});
},
wrapper(
{
defaultOptions,
},
mockedPaginatedTransport,
),
wrapper({}, mockedPaginatedTransport),
);

result.current.mutate({
Expand Down Expand Up @@ -109,12 +98,7 @@ describe("useMutation", () => {
},
});
},
wrapper(
{
defaultOptions,
},
mockedPaginatedTransport,
),
wrapper({}, mockedPaginatedTransport),
);

result.current.mutate({
Expand Down