Skip to content

Commit

Permalink
New "customPages" option on useInfiniteQuery (#147)
Browse files Browse the repository at this point in the history
* New "customPages" option on useInfiniteQuery

* dont' reset entity store

* expand changeset
  • Loading branch information
PabloSzx authored Mar 5, 2024
1 parent 58c7ef4 commit 7f9c006
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
10 changes: 10 additions & 0 deletions .changeset/olive-geese-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@soundxyz/graphql-react-query': minor
---

New "customPages" option on useInfiniteQuery

This option can be used to be able to add a list of arbitrary pages of data into the resulting "orderedList" data.
One use-case is for optimistic operations to be able to have temporary pages that are going to be in the pagination ordered results.

This list is recommended to be memoized or live outside of the React life-cycle using solutions like Valtio.
19 changes: 14 additions & 5 deletions packages/graphql-react-query/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,8 @@ export function GraphQLReactQueryClient<

enabled = true,

customPages,

...options
}: Omit<
UseInfiniteQueryOptions<ExecutionResultWithData<ResultOf<Doc>>, Error>,
Expand Down Expand Up @@ -712,6 +714,8 @@ export function GraphQLReactQueryClient<
list(result: ResultOf<Doc>): Entity[] | null | undefined | false | '' | 0;
uniq(entity: Entity): string;
order?: readonly [AtLeastOne<(entity: Entity) => unknown>, AtLeastOne<'asc' | 'desc'>];

customPages?: Array<ExecutionResultWithData<ResultOf<Doc>>>;
},
) {
const entityStore = (infiniteQueryStores[query + JSON.stringify(filterQueryKey)] ||= proxy({
Expand All @@ -734,8 +738,6 @@ export function GraphQLReactQueryClient<
>,
options?: SetDataOptions,
) => {
entityStore.nodes = {};

setInfiniteQueryData(
{
query,
Expand Down Expand Up @@ -822,7 +824,10 @@ export function GraphQLReactQueryClient<
const currentUniq = latestUniq.current;
const currentOrder = latestOrder.current;

const values = data.pages.reduce((acc: Record<string, Entity>, page) => {
function reduceAccumulatedPages(
acc: Record<string, Entity>,
page: ExecutionResultWithData<ResultOf<Doc>>,
) {
const listValues = page.data ? currentListFn(page.data) || [] : [];

for (const entity of listValues) {
Expand All @@ -833,12 +838,16 @@ export function GraphQLReactQueryClient<
}

return acc;
}, {});
}

const initialValues = customPages ? customPages.reduce(reduceAccumulatedPages, {}) : {};

const values = data.pages.reduce(reduceAccumulatedPages, initialValues);

if (currentOrder) return orderBy(values, currentOrder, stableOrderType);

return Object.values(values);
}, [stableOrderType, data, entityStoreNodesSnapshot]);
}, [stableOrderType, data, entityStoreNodesSnapshot, customPages]);

return {
...result,
Expand Down

0 comments on commit 7f9c006

Please sign in to comment.