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 queryKeyHashFn provided by createQueryOptions #461

Merged
merged 2 commits into from
Oct 18, 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
1 change: 1 addition & 0 deletions packages/connect-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"build:esm": "tsc --project tsconfig.build.json",
"generate": "buf generate",
"test": "vitest --run",
"test:watch": "vitest --watch",
"format": "prettier . --write --ignore-path ./.eslintignore && eslint . --fix && license-header",
"attw": "attw --pack"
},
Expand Down
4 changes: 1 addition & 3 deletions packages/connect-query/src/create-query-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type {
} from "@bufbuild/protobuf";
import { create } from "@bufbuild/protobuf";
import type { Transport } from "@connectrpc/connect";
import type { QueryFunction, QueryKey, SkipToken } from "@tanstack/react-query";
import type { QueryFunction, SkipToken } from "@tanstack/react-query";
import { skipToken } from "@tanstack/react-query";

import { callUnaryMethod } from "./call-unary-method.js";
Expand Down Expand Up @@ -58,7 +58,6 @@ export function createQueryOptions<
queryKey: ConnectQueryKey;
queryFn: QueryFunction<MessageShape<O>, ConnectQueryKey> | SkipToken;
structuralSharing: (oldData: unknown, newData: unknown) => unknown;
queryKeyHashFn: (queryKey: QueryKey) => string;
} {
const queryKey = createConnectQueryKey({
schema,
Expand All @@ -75,6 +74,5 @@ export function createQueryOptions<
queryKey,
queryFn,
structuralSharing,
queryKeyHashFn: JSON.stringify,
};
}
45 changes: 44 additions & 1 deletion packages/connect-query/src/use-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ import { skipToken } from "@tanstack/react-query";
import { renderHook, waitFor } from "@testing-library/react";
import { describe, expect, it } from "vitest";

import { createConnectQueryKey } from "./connect-query-key.js";
import { BigIntService } from "./gen/bigint_pb.js";
import { ElizaService } from "./gen/eliza_pb.js";
import { mockEliza, wrapper } from "./test/test-utils.js";
import { mockBigInt, mockEliza, wrapper } from "./test/test-utils.js";
import { useQuery, useSuspenseQuery } from "./use-query.js";

// TODO: maybe create a helper to take a service and method and generate this.
const sayMethodDescriptor = ElizaService.method.say;

const mockedElizaTransport = mockEliza();

const bigintTransport = mockBigInt();

const elizaWithDelayTransport = mockEliza(undefined, true);

describe("useQuery", () => {
Expand Down Expand Up @@ -177,6 +181,45 @@ describe("useQuery", () => {
expect(result.current.isPending).toBeTruthy();
expect(result.current.isFetching).toBeFalsy();
});

it("supports schemas with bigint keys", async () => {
const { result } = renderHook(
() => {
return useQuery(BigIntService.method.count, {
add: 2n,
});
},
wrapper({}, bigintTransport),
);

await waitFor(() => {
expect(result.current.isSuccess).toBeTruthy();
});

expect(result.current.data?.count).toBe(1n);
});

it("data can be fetched from cache", async () => {
const { queryClient, ...rest } = wrapper({}, bigintTransport);
const { result } = renderHook(() => {
return useQuery(BigIntService.method.count, {});
}, rest);

await waitFor(() => {
expect(result.current.isSuccess).toBeTruthy();
});

expect(
queryClient.getQueryData(
createConnectQueryKey({
schema: BigIntService.method.count,
input: {},
transport: bigintTransport,
cardinality: "finite",
}),
),
).toBe(result.current.data);
});
});

describe("useSuspenseQuery", () => {
Expand Down