-
Hey, I wrote a small abstraction over If you're wondering what abstraction I've done, I wrapped export type UrqlStates<Data> =
| FetchingState
| DoneState<Data>
| ErrorState
| IdleState
| StaleState<Data>
| PartialState<Data>
| PartialStaleState<Data>
export type FetchingState = {
state: 'fetching'
}
export type DoneState<Data> = {
state: 'done'
data: Data
stale: false
}
export type StaleState<Data> = {
state: 'stale'
data: Data
stale: true
}
export type IdleState = {
state: 'idle'
}
export type ErrorState = {
state: 'error'
error: CombinedError
}
export type PartialState<Data> = { state: 'partial'; error: CombinedError; data: Data }
export type PartialStaleState<Data> = {
state: 'partial-stale'
error: CombinedError
data: Data
stale: true
} So then I can branch on it using const [responseMachine] = useEnchanceQuery(useSomeCodegeneratedQuery())
switch (responseMachine.state) {
case 'idle':
case 'fetching':
case 'stale':
return <GenericSpinner />
case 'partial':
case 'partial-stale':
case 'error':
return <GenericError />
case 'done': {
return <SomeUi data={responseMachine.data} /> // here data us non-nullable
}
default:
return impossibleState(responseMachine) Just want to limit the number of cases to avoid having some useless one here in switch :) Same for |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hiya 👋 Mutations cannot carry a This stands in contrast with mutations. Mutations are often "fire and forget" and can be executed multiple times, each resulting in a new independent result. You can see this guarantee in action here too: https://github.com/FormidableLabs/urql/blob/e5ea77e836b11be379327bee7abe74109d0adabc/packages/core/src/client.ts#L244-L251 Basically what you should keep in mind is that an algebraic data-like state isn't best suited to deal with query results; in essence all fields on an |
Beta Was this translation helpful? Give feedback.
Hiya 👋
Mutations cannot carry a
stale
flag. Thestale
flag signfies thaturql
is expecting another result for a query. Hence this flag only currently applies to queries. The important point here is that it's implemented currently by both@urql/core
and the cache exchanges. Whenever we know that a new network result will come in for queries we can set this flag.This stands in contrast with mutations. Mutations are often "fire and forget" and can be executed multiple times, each resulting in a new independent result.
You can see this guarantee in action here too: https://github.com/FormidableLabs/urql/blob/e5ea77e836b11be379327bee7abe74109d0adabc/packages/core/src/client.ts#L244-L251
Basic…