Skip to content

Add utility helper to update infinite data #518

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

Closed
wants to merge 6 commits into from
Closed
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Connect-Query is an wrapper around [TanStack Query](https://tanstack.com/query)
- [`createConnectQueryKey`](#createconnectquerykey)
- [`callUnaryMethod`](#callunarymethod)
- [`createProtobufSafeUpdater`](#createprotobufsafeupdater)
- [`createProtobufSafeInfiniteUpdater`](#createprotobufsafeinfiniteupdater)
- [`createQueryOptions`](#createqueryoptions)
- [`createInfiniteQueryOptions`](#createinfinitequeryoptions)
- [`addStaticKeyToTransport`](#addstatickeytotransport)
Expand Down Expand Up @@ -387,6 +388,10 @@ queryClient.setQueryData(

```

### `createProtobufSafeInfiniteUpdater`

Creates a typesafe updater for infinite queries. Identical to `createProtobufSafeUpdater` except the data received must be of the shape expected for infinite data.

### `createQueryOptions`

```ts
Expand Down
82 changes: 82 additions & 0 deletions packages/connect-query-core/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { describe, expect, it } from "vitest";

import {
assert,
createProtobufSafeInfiniteUpdater,
createProtobufSafeUpdater,
isAbortController,
} from "./utils.js";
Expand Down Expand Up @@ -184,3 +185,84 @@ describe("createProtobufSafeUpdater", () => {
});
});
});

describe("createProtobufSafeInfiniteUpdater", () => {
describe("with update message", () => {
const schema = { output: Proto2MessageSchema };
const update = {
pageParams: [],
pages: [
{
int32Field: 999,
},
],
};
const safeUpdater = createProtobufSafeInfiniteUpdater(schema, update);
it("returns update message for previous value undefined", () => {
const next = safeUpdater(undefined);
expect(next?.pages[0].$typeName).toBe("test.Proto2Message");
});
});

describe("with update message init", () => {
const schema = { output: Proto2MessageSchema };
const update = {
pageParams: [],
pages: [
{
int32Field: 999,
},
],
};
const safeUpdater = createProtobufSafeInfiniteUpdater(schema, update);
it("returns update message for previous value undefined", () => {
const next = safeUpdater(undefined);
expect(next?.pages[0].int32Field).toBe(999);
});
it("returns update message for previous value", () => {
const prev = {
pageParams: [],
pages: [
create(Proto2MessageSchema, {
int32Field: 123,
}),
],
};
const next = safeUpdater(prev);
expect(next?.pages[0].$typeName).toBe(Proto2MessageSchema.typeName);
expect(next?.pages[0].int32Field).toBe(999);
});
});

describe("with updater function", () => {
const schema = { output: Proto2MessageSchema };
const safeUpdater = createProtobufSafeInfiniteUpdater(schema, (prev) => {
if (prev === undefined) {
return undefined;
}
return {
pageParams: [...prev.pageParams, 44],
pages: [
...prev.pages,
{
int32Field: 33,
stringField: "whatever",
},
],
};
});
it("accepts undefined", () => {
const next = safeUpdater(undefined);
expect(next).toBeUndefined();
});
it("can add a new page", () => {
const next = safeUpdater({
pageParams: [],
pages: [],
});

expect(next?.pageParams).toHaveLength(1);
expect(next?.pages).toHaveLength(1);
});
});
});
38 changes: 38 additions & 0 deletions packages/connect-query-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
MessageShape,
} from "@bufbuild/protobuf";
import { create, isMessage } from "@bufbuild/protobuf";
import type { InfiniteData } from "@tanstack/query-core";

/**
* Throws an error with the provided message when the condition is `false`
Expand Down Expand Up @@ -58,6 +59,16 @@ export type ConnectUpdater<O extends DescMessage> =
| undefined
| ((prev?: MessageShape<O>) => MessageShape<O> | undefined);

/**
* @see `Updater` from `@tanstack/react-query`
*/
export type ConnectInfiniteUpdater<O extends DescMessage> =
| InfiniteData<MessageInitShape<O>>
| undefined
| ((
prev?: InfiniteData<MessageShape<O>>,
) => InfiniteData<MessageInitShape<O>> | undefined);

/**
* This helper makes sure that the type for the original response message is returned.
*/
Expand All @@ -78,3 +89,30 @@ export const createProtobufSafeUpdater =
}
return updater(prev);
};

export const createProtobufSafeInfiniteUpdater =
<O extends DescMessage>(
schema: Pick<DescMethodUnary<never, O>, "output">,
updater: ConnectInfiniteUpdater<O>,
) =>
(
prev?: InfiniteData<MessageShape<O>>,
): InfiniteData<MessageShape<O>> | undefined => {
if (typeof updater !== "function") {
if (updater === undefined) {
return undefined;
}
return {
pageParams: updater.pageParams,
pages: updater.pages.map((i) => create(schema.output, i)),
};
}
const result = updater(prev);
if (result === undefined) {
return undefined;
}
return {
pageParams: result.pageParams,
pages: result.pages.map((i) => create(schema.output, i)),
};
};