From 6107570abe55d79b4f58a786ea462e8e5c5780e5 Mon Sep 17 00:00:00 2001 From: Igor Kamyshev Date: Sat, 17 Aug 2024 15:08:40 +0700 Subject: [PATCH] Fix types in `connectQuery` in case of source _Query_ with `initialData` --- .changeset/strong-hotels-hope.md | 5 ++++ .../query/__tests__/connect_query.test-d.ts | 29 +++++++++++++++++++ packages/core/src/query/connect_query.ts | 4 +-- 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 .changeset/strong-hotels-hope.md diff --git a/.changeset/strong-hotels-hope.md b/.changeset/strong-hotels-hope.md new file mode 100644 index 00000000..def264a8 --- /dev/null +++ b/.changeset/strong-hotels-hope.md @@ -0,0 +1,5 @@ +--- +"@farfetched/core": patch +--- + +Fix types in `connectQuery` in case of source _Query_ with `initialData` diff --git a/packages/core/src/query/__tests__/connect_query.test-d.ts b/packages/core/src/query/__tests__/connect_query.test-d.ts index edf88c39..aaa6bcd8 100644 --- a/packages/core/src/query/__tests__/connect_query.test-d.ts +++ b/packages/core/src/query/__tests__/connect_query.test-d.ts @@ -226,4 +226,33 @@ describe('connectQuery', () => { target: query2, }); }); + + test('allows query with initialData to be used as source', () => { + const query1 = createQuery({ + initialData: [], + async handler(ids: number[]) { + return Promise.resolve([1]); + }, + }); + + const query2 = createQuery({ + initialData: [], + async handler(ids: number[]) { + return Promise.resolve([1]); + }, + }); + + connectQuery({ + source: query1, + fn: (a) => { + a.params.forEach; + a.result.forEach; + + return { + params: [1], + }; + }, + target: query2, + }); + }); }); diff --git a/packages/core/src/query/connect_query.ts b/packages/core/src/query/connect_query.ts index 8308f396..1ee98bce 100644 --- a/packages/core/src/query/connect_query.ts +++ b/packages/core/src/query/connect_query.ts @@ -19,7 +19,7 @@ export function connectQuery>( } & (Target extends Query ? P extends void ? { fn?: undefined; filter?: () => boolean } - : Sources extends Query + : Sources extends Query ? { fn: (sources: { result: RemoteOperationResult; @@ -32,7 +32,7 @@ export function connectQuery>( params: RemoteOperationParams; }) => boolean; } - : Sources extends Record> + : Sources extends Record> ? { fn: (sources: { [index in keyof Sources]: {