-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Defer evaluation of the variables if query is paused, add atomWithLaz…
…yQuery atom (#15) * fix: defer valuation of the variables if paused feat: add atomWithLazyQuery that allows for promisifying of a singular query result the same way `atomWithMutation` does * feat: add fetching state for mutations and lazy query fix: make sure that lazy query gets reset after all listeners are unmounted
- Loading branch information
Showing
6 changed files
with
218 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type { AnyVariables } from '@urql/core' | ||
import { Client, DocumentInput, OperationContext } from '@urql/core' | ||
import { WritableAtom, atom } from 'jotai/vanilla' | ||
import type { Getter } from 'jotai/vanilla' | ||
import { filter, pipe, subscribe } from 'wonka' | ||
import { clientAtom } from './clientAtom' | ||
import { | ||
InitialOperationResultLazy, | ||
urqlReactCompatibleInitialStateLazy, | ||
} from './common' | ||
|
||
export type AtomWithLazyQuery< | ||
Data, | ||
Variables extends AnyVariables | ||
> = WritableAtom< | ||
InitialOperationResultLazy<Data, Variables>, | ||
[Variables, Partial<OperationContext>] | [Variables], | ||
Promise<InitialOperationResultLazy<Data, Variables>> | ||
> | ||
|
||
export function atomWithLazyQuery< | ||
Data = unknown, | ||
Variables extends AnyVariables = AnyVariables | ||
>( | ||
query: DocumentInput<Data, Variables>, | ||
getClient: (get: Getter) => Client = (get) => get(clientAtom) | ||
): AtomWithLazyQuery<Data, Variables> { | ||
const atomDataBase = atom<InitialOperationResultLazy<Data, Variables>>( | ||
urqlReactCompatibleInitialStateLazy | ||
) | ||
atomDataBase.onMount = (setAtom) => { | ||
return () => { | ||
// Clean up the atom cache on unmount | ||
setAtom(urqlReactCompatibleInitialStateLazy) | ||
} | ||
} | ||
const atomData = atom< | ||
InitialOperationResultLazy<Data, Variables>, | ||
[Variables, Partial<OperationContext>] | [Variables], | ||
Promise<InitialOperationResultLazy<Data, Variables>> | ||
>( | ||
(get) => { | ||
return get(atomDataBase) | ||
}, | ||
(get, set, ...args) => { | ||
const source = getClient(get).query(query, args[0], { | ||
requestPolicy: 'network-only', | ||
...(args[1] ? args[1] : {}), | ||
}) | ||
pipe( | ||
source, | ||
// This is needed so that the atom gets updated with loading states etc., but not with the final result that will be set by the promise | ||
filter((result) => result?.data === undefined), | ||
subscribe((result) => set(atomDataBase, { ...result, fetching: true })) | ||
) | ||
|
||
set(atomDataBase, { | ||
...urqlReactCompatibleInitialStateLazy, | ||
fetching: true, | ||
}) | ||
return source.toPromise().then((result) => { | ||
const mergedResult = { ...result, fetching: false } | ||
set(atomDataBase, mergedResult) | ||
return mergedResult | ||
}) | ||
} | ||
) | ||
|
||
return atomData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters