-
-
Notifications
You must be signed in to change notification settings - Fork 451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for skip to Query and useQuery. #237
Changes from 3 commits
8f8907a
ed6cffa
2f6e57e
0c38be6
7214b59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,30 +19,36 @@ jest.mock('../client', () => { | |
|
||
import React, { FC } from 'react'; | ||
import renderer, { act } from 'react-test-renderer'; | ||
// @ts-ignore - data is imported from mock only | ||
import { createClient } from '../client'; | ||
import { useQuery } from './useQuery'; | ||
import { OperationContext } from '../types'; | ||
import { useQuery, UseQueryArgs, UseQueryState } from './useQuery'; | ||
|
||
// @ts-ignore | ||
const client = createClient() as { executeQuery: jest.Mock }; | ||
const props = { | ||
const props: UseQueryArgs<{ myVar: number }> = { | ||
query: '{ example }', | ||
variables: { | ||
myVar: 1234, | ||
}, | ||
skip: false, | ||
}; | ||
let state: any; | ||
let execute: any; | ||
|
||
const QueryUser: FC<typeof props> = ({ query, variables }) => { | ||
const [s, e] = useQuery({ query, variables }); | ||
let state: UseQueryState<any> | undefined; | ||
let execute: ((opts?: Partial<OperationContext>) => void) | undefined; | ||
|
||
const QueryUser: FC<UseQueryArgs<{ myVar: number }>> = ({ | ||
query, | ||
variables, | ||
skip, | ||
}) => { | ||
const [s, e] = useQuery({ query, variables, skip }); | ||
state = s; | ||
execute = e; | ||
return <p>{s.data}</p>; | ||
}; | ||
|
||
beforeAll(() => { | ||
// tslint:disable-next-line | ||
// tslint:disable-next-line no-console | ||
console.log( | ||
'supressing console.error output due to react-test-renderer spam (hooks related)' | ||
); | ||
|
@@ -158,7 +164,25 @@ describe('on change', () => { | |
describe('execute query', () => { | ||
it('triggers query execution', () => { | ||
renderer.create(<QueryUser {...props} />); | ||
act(() => execute()); | ||
act(() => execute!()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-nullary operator for tests, since |
||
expect(client.executeQuery).toBeCalledTimes(2); | ||
}); | ||
}); | ||
|
||
describe('skip', () => { | ||
it('skips executing the query if skip is true', () => { | ||
renderer.create(<QueryUser {...props} skip={true} />); | ||
expect(client.executeQuery).not.toBeCalled(); | ||
}); | ||
|
||
it('skips executing queries if skip updates to true', () => { | ||
const wrapper = renderer.create(<QueryUser {...props} />); | ||
|
||
/** | ||
* Call update twice for the change to be detected. | ||
*/ | ||
wrapper.update(<QueryUser {...props} skip={true} />); | ||
wrapper.update(<QueryUser {...props} skip={true} />); | ||
expect(client.executeQuery).toBeCalledTimes(1); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,10 @@ export interface UseQueryArgs<V> { | |
query: string | DocumentNode; | ||
variables?: V; | ||
requestPolicy?: RequestPolicy; | ||
skip?: boolean; | ||
} | ||
|
||
interface UseQueryState<T> { | ||
export interface UseQueryState<T> { | ||
fetching: boolean; | ||
data?: T; | ||
error?: CombinedError; | ||
|
@@ -39,27 +40,34 @@ export const useQuery = <T = any, V = object>( | |
const executeQuery = useCallback( | ||
(opts?: Partial<OperationContext>) => { | ||
unsubscribe(); | ||
|
||
setState(s => ({ ...s, fetching: true })); | ||
|
||
const [teardown] = pipe( | ||
client.executeQuery(request, { | ||
requestPolicy: args.requestPolicy, | ||
...opts, | ||
}), | ||
subscribe(({ data, error }) => | ||
setState({ fetching: false, data, error }) | ||
) | ||
); | ||
let teardown = noop; | ||
|
||
if (!args.skip) { | ||
[teardown] = pipe( | ||
client.executeQuery(request, { | ||
requestPolicy: args.requestPolicy, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at this now, shouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup I believe so. We're currently allowing users to specify the request policy on a manual query execution but I believe this goes against the way hooks work. If the request policy changes, that should be reflected in the components state and flow all the way down to the useQuery hook. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pushed! |
||
...opts, | ||
}), | ||
subscribe(({ data, error }) => | ||
setState({ fetching: false, data, error }) | ||
) | ||
); | ||
} else { | ||
setState(s => ({ ...s, fetching: false })); | ||
} | ||
|
||
unsubscribe = teardown; | ||
}, | ||
[request.key] | ||
[request.key, args.skip] | ||
); | ||
|
||
useEffect(() => { | ||
executeQuery(); | ||
return unsubscribe; | ||
}, [request.key]); | ||
}, [request.key, args.skip]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Last question here for both of you @andyrichardson and @kitten -> should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hooks FAQ on functions as dependencies: https://reactjs.org/docs/hooks-faq.html#is-it-safe-to-omit-functions-from-the-list-of-dependencies There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, didn't get to this 😱 Including it into the array here will probably mean that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🆒 that'll come up in the next PR, gonna merge this as is 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @parkerziegler - yeah I think that is necessary 👍 |
||
|
||
return [state, executeQuery]; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving the check here ensures that both
this.unsubscribe
will get executed and redefined in preparation for the next query. No more futzing withcomponentDidUpdate
😅