Skip to content

Commit

Permalink
Remove query fn provided by createQueryOptions
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Sachs <[email protected]>
  • Loading branch information
paul-sachs committed Oct 17, 2024
1 parent eed6fa8 commit 162bd94
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 22 deletions.
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
8 changes: 3 additions & 5 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 All @@ -31,7 +31,7 @@ import { createStructuralSharing } from "./structural-sharing.js";
function createUnaryQueryFn<I extends DescMessage, O extends DescMessage>(
transport: Transport,
schema: DescMethodUnary<I, O>,
input: MessageInitShape<I> | undefined,
input: MessageInitShape<I> | undefined
): QueryFunction<MessageShape<O>, ConnectQueryKey> {
return async (context) => {
return callUnaryMethod(transport, schema, input, {
Expand All @@ -53,12 +53,11 @@ export function createQueryOptions<
transport,
}: {
transport: Transport;
},
}
): {
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,
};
}
77 changes: 60 additions & 17 deletions 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 All @@ -36,7 +40,7 @@ describe("useQuery", () => {
sentence: "hello",
});
},
wrapper({}, mockedElizaTransport),
wrapper({}, mockedElizaTransport)
);

await waitFor(() => {
Expand All @@ -51,7 +55,7 @@ describe("useQuery", () => {
() => {
return useQuery(sayMethodDescriptor, skipToken);
},
wrapper(undefined, mockedElizaTransport),
wrapper(undefined, mockedElizaTransport)
);
expect(result.current.isPending).toBeTruthy();
expect(result.current.isFetching).toBeFalsy();
Expand All @@ -68,10 +72,10 @@ describe("useQuery", () => {
{},
{
transport,
},
}
);
},
wrapper(undefined, mockedElizaTransport),
wrapper(undefined, mockedElizaTransport)
);
await waitFor(() => {
expect(result.current.isSuccess).toBeTruthy();
Expand All @@ -91,10 +95,10 @@ describe("useQuery", () => {
placeholderData: create(sayMethodDescriptor.output, {
sentence: "placeholder!",
}),
},
}
);
},
wrapper(undefined, mockedElizaTransport),
wrapper(undefined, mockedElizaTransport)
);
expect(result.current.data?.sentence).toBe("placeholder!");
});
Expand All @@ -107,10 +111,10 @@ describe("useQuery", () => {
{},
{
select: (data) => data.sentence.length,
},
}
);
},
wrapper(undefined, mockedElizaTransport),
wrapper(undefined, mockedElizaTransport)
);

await waitFor(() => {
Expand All @@ -130,10 +134,10 @@ describe("useQuery", () => {
},
{
enabled: false,
},
}
);
},
wrapper({}, mockedElizaTransport),
wrapper({}, mockedElizaTransport)
);

expect(result.current.data).toBeUndefined();
Expand All @@ -156,8 +160,8 @@ describe("useQuery", () => {
},
},
},
mockedElizaTransport,
),
mockedElizaTransport
)
);

expect(result.current.data).toBeUndefined();
Expand All @@ -170,13 +174,52 @@ describe("useQuery", () => {
() => {
return useQuery(sayMethodDescriptor, skipToken);
},
wrapper({}, mockedElizaTransport),
wrapper({}, mockedElizaTransport)
);

expect(result.current.data).toBeUndefined();
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 All @@ -187,7 +230,7 @@ describe("useSuspenseQuery", () => {
sentence: "hello",
});
},
wrapper({}, mockedElizaTransport),
wrapper({}, mockedElizaTransport)
);

await waitFor(() => {
Expand All @@ -207,10 +250,10 @@ describe("useSuspenseQuery", () => {
},
{
select: (data) => data.sentence.length,
},
}
);
},
wrapper({}, mockedElizaTransport),
wrapper({}, mockedElizaTransport)
);

await waitFor(() => {
Expand Down

0 comments on commit 162bd94

Please sign in to comment.