Skip to content

Commit

Permalink
Update typing for Client.paginate()
Browse files Browse the repository at this point in the history
  • Loading branch information
ptpaterson committed Jul 23, 2024
1 parent 5a37964 commit 97f7b12
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
38 changes: 31 additions & 7 deletions __tests__/integration/query-typings.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fql, QueryCheckError } from "../../src";
import { fql, Page, QueryCheckError } from "../../src";
import { getClient } from "../client";

// added in a junk property that is not part of QueryValue
Expand All @@ -16,12 +16,25 @@ describe.each`
${"paginate"}
`("$method typings", ({ method }: { method: string }) => {
it("allows customers to use their own types in queries", async () => {
expect.assertions(1);
const query = fql`{ "x": 123 }`;
const paginatedQuery = fql`[{ "x": 123}].toSet()`;

if ("query" === method) {
const result = (await client.query<MyType>(fql`{ "x": 123}`)).data;
expect.assertions(1);
const result = (await client.query<MyType>(query)).data;
expect(result).toEqual({ x: 123 });
} else {
for await (const page of client.paginate<MyType>(fql`{ "x": 123}`)) {
expect.assertions(2);
for await (const page of client.paginate<MyType>(paginatedQuery)) {
for (const result of page) {
expect(result).toEqual({ x: 123 });
}
}

// It is also allowed to provide a query that does not return a page.
// When this happenes, the driver treats the result as if a page with
// exactly one item is returned.
for await (const page of client.paginate<MyType>(query)) {
for (const result of page) {
expect(result).toEqual({ x: 123 });
}
Expand All @@ -35,15 +48,26 @@ describe.each`
const noopToValidateInferredType = (value: MyType) => {};

const query = fql<MyType>`{ "x": 123 }`;
const q2 = fql`{ "x": ${query} }`;
const paginatedQuery = fql<Page<MyType>>`[{ "x": 123}].toSet()`;

expect.assertions(1);
if ("query" === method) {
expect.assertions(1);
const result = (await client.query(query)).data;
noopToValidateInferredType(result);
expect(result).toEqual({ x: 123 });
} else {
for await (const page of client.paginate<MyType>(fql`{ "x": 123}`)) {
expect.assertions(2);
for await (const page of client.paginate(paginatedQuery)) {
for (const result of page) {
noopToValidateInferredType(result);
expect(result).toEqual({ x: 123 });
}
}

// It is also allowed to provide a query that does not return a page.
// When this happenes, the driver treats the result as if a page with
// exactly one item is returned.
for await (const page of client.paginate(query)) {
for (const result of page) {
noopToValidateInferredType(result);
expect(result).toEqual({ x: 123 });
Expand Down
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export class Client {
* ```
*/
paginate<T extends QueryValue>(
iterable: Page<T> | EmbeddedSet | Query<T>,
iterable: Page<T> | EmbeddedSet | Query<Page<T>> | Query<T>,
options?: QueryOptions,
): SetIterator<T> {
if (iterable instanceof Query) {
Expand Down

0 comments on commit 97f7b12

Please sign in to comment.