Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/graphql-hooks/src/useClientRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ function useClientRequest(query, initialOpts = {}) {
// arguments to fetchData override the useClientRequest arguments
const fetchData = useDeepCompareCallback(
newOpts => {
if (!isMounted.current) return Promise.resolve()
const revisedOpts = {
...initialOpts,
...newOpts
Expand All @@ -145,6 +144,18 @@ function useClientRequest(query, initialOpts = {}) {
operationName: revisedOpts.operationName
}

if (!isMounted.current) {
return Promise.resolve({
error: {
fetchError: new Error(
'fetchData should not be called after hook unmounted'
)
},
loading: false,
cacheHit: false
})
}

const revisedCacheKey = client.getCacheKey(revisedOperation, revisedOpts)

// NOTE: There is a possibility of a race condition whereby
Expand Down
14 changes: 11 additions & 3 deletions packages/graphql-hooks/test/unit/useClientRequest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ describe('useClientRequest', () => {
expect(state).toEqual({ cacheHit: false, loading: true })
})

it('returns undefined instantly if not mounted', async () => {
it('returns result with error instantly if not mounted', async () => {
let fetchData, state
const { unmount } = renderHook(
() =>
Expand All @@ -487,8 +487,16 @@ describe('useClientRequest', () => {
)

unmount()
const result = await act(fetchData)
expect(result).toBe(undefined)

let result
await act(async () => {
result = await fetchData()
})

expect(result.data).toBe(undefined)
expect(result.loading).toBe(false)
expect(result.cacheHit).toBe(false)
expect(result.error.fetchError).toBeInstanceOf(Error)
expect(mockClient.request).not.toHaveBeenCalled()
expect(state).toEqual({ cacheHit: false, loading: true })
})
Expand Down