Skip to content

Commit

Permalink
fix(use-data-query): enable swr by default
Browse files Browse the repository at this point in the history
BREAKING CHANGE: loading will only be set to true when fetching and if there is no data. If there
is data, loading will be false during fetching. This means that stale data will be shown during
fetches by default. If you'd like to opt out of showing stale data during loading you can use the
new `fetching` attribute that is now returned by the useDataQuery hook instead.
  • Loading branch information
ismay committed Sep 7, 2021
1 parent a1a1f54 commit 76cb34a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
6 changes: 4 additions & 2 deletions services/data/src/react/hooks/useDataQuery.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { renderHook, act } from '@testing-library/react-hooks'
import React from 'react'
import * as React from 'react'
import { CustomDataProvider } from '../components/CustomDataProvider'
import { useDataQuery } from './useDataQuery'

Expand Down Expand Up @@ -471,7 +471,8 @@ describe('useDataQuery', () => {

expect(mockSpy).toHaveBeenCalledTimes(2)
expect(result.current).toMatchObject({
loading: true,
loading: false,
fetching: true,
called: true,
data: { x: 42 },
})
Expand All @@ -481,6 +482,7 @@ describe('useDataQuery', () => {
expect(mockSpy).toHaveBeenCalledTimes(2)
expect(result.current).toMatchObject({
loading: false,
fetching: false,
called: true,
data: { x: 43 },
})
Expand Down
24 changes: 14 additions & 10 deletions services/data/src/react/hooks/useDataQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,18 @@ export const useDataQuery = (
const queryKey = [staticQuery, variables]
const queryFn = () => engine.query(staticQuery, { variables })

const { isIdle, isFetching, error, data, refetch: queryRefetch } = useQuery(
queryKey,
queryFn,
{
enabled,
onSuccess,
onError,
}
)
const {
isIdle,
isFetching,
isLoading,
error,
data,
refetch: queryRefetch,
} = useQuery(queryKey, queryFn, {
enabled,
onSuccess,
onError,
})

/**
* Refetch allows a user to update the variables or just
Expand Down Expand Up @@ -143,7 +146,8 @@ export const useDataQuery = (
engine,
// A query is idle if it is lazy and no initial data is available.
called: !isIdle,
loading: isFetching,
loading: isLoading,
fetching: isFetching,
error: ourError,
data,
refetch,
Expand Down
1 change: 1 addition & 0 deletions services/data/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface ExecuteHookResult<ReturnType> {
export interface QueryState {
called: boolean
loading: boolean
fetching: boolean
error?: FetchError
data?: QueryResult
}
Expand Down

0 comments on commit 76cb34a

Please sign in to comment.