React Queries
#
useQueryReact hook that uses the main design of gqless
.
This hook returns the core query
object, and it will detect the usage of it, and suspend (if enabled) when the data is being fetched for the first time, and update the component when any of the requested data changes.
Check API Reference
#
Features- Composability
- Advanced 'stale-while-revalidate' behavior, allowing you to set any value to re-trigger re-validation.
- Extra
$state
in the returned value, which is extra valuable forNon-Suspense
usage.
#
Suspense Example#
Non-Suspense ExampleSee Non-Suspense usage for more details
#
prepareuseQuery
offers a nice helper that allows for good optimizations, allowing you to do a prepass over specific parts of your expected query, before your component ends rendering the first time.
It's basically a function that is called right before the useQuery
returns, allowing your component to either Suspend immediately
or add a conditional render using the $state.isLoading
property.
The function receives as parameters:
- The
prepass
helper. - Your auto-generated
query
object.
caution
Since it's a function called before the hook itself returned, the returned value/values are undefined
, and unfortunately TypeScript
can't know that.
#
Suspense example#
Non-Suspense example#
graphql HOCThe graphql HOC
is a Higher-Order Component alternative to useQuery, designed specially for Suspense usage.
For this function it is expected to use the query
object exported by the core client, and it will detect the usage of it, and suspend (if enabled) when the data is being fetched for the first time, and update the component when any of the requested data changes.
#
Features- Specify
Suspense fallback
directly - Intercept the data requests faster than useQuery, allowing it to prevent an extra render.
- Basic 'stale-while-revalidate' behavior
#
ExampleCheck API Reference
#
Difference between "useQuery" and "graphql"If you read both, you could see that both do similar stuff, but enable different things based on it's nature and React constraints.
The graphql HOC is targeted specially for Suspense usage, since it can intercept the data requests before the render phase and Suspend right away, and also specify the fallback directly, but since it's a HOC
, it's not as nice to use as hooks.
On the other hand, useQuery enables very nice composability, but it lacks the ability to intercept the Render Phase if not using useQuery "prepare" helper, which in practice it only means 1 extra render, which most of the time it doesn't matter.
And it's important to note that for Non-Suspense
usage we encourage to always use useQuery.
#
usePaginatedQueryPaginated focused queries, in which you specify a function and initial arguments, which is going to be automatically called on first-render or via custom fetchMore
calls.
#
Features- Suspense support
- Partial Fetch policy support (
'cache-first'
,'cache-and-network'
or'network-only'
) - Custom data merge function, with included
uniqBy
&sortBy
helpers. - If no new args are defined in your
fetchMore
call, the previous or initial args are used. - You can override the existing
fetchPolicy
in the second parameter offetchMore
, for example, to force arefetch
. - Core helpers included in third parameter of main function.
#
ExampleCheck API Reference
#
useTransactionQueryAlternative to graphql
and useQuery
that works via pre-defined functions, which allows for extra features that are not available in useQuery
& graphql HOC
.
#
Features- Polling
- Conditional skipping
- Automatic call on variable change
- Lifecycle functions
onCompleted
&onError
- Suspense support
- Fetch policy support
#
ExampleCheck API Reference
#
useLazyQueryQueries meant to be called in response of events, like button clicks.
#
Features- Lifecycle functions
onCompleted
&onError
- Suspense support
- Partial Fetch policy support (
'network-only'
,'cache-and-network'
or'no-cache'
)
#
ExampleCheck API Reference
#
useRefetchRefetch giving specific parts of the schema or via functions
Check API Reference
#
Example with functions#
Example with objectsIn this case, you have to specify an object type, scalars won't work (for those, you have to use functions).
It will automatically refetch everything previously requested under that tree
#
prepareQueryPrepare queries on module-level.
#
Features- Enable Render as you fetch pattern
- Re-using the callbacks in useTransactionQuery & useLazyQuery
- Composition
- Prefetching
- Refetching
- Suspense Support
#
ExampleCheck API Reference