Skip to content
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

Allow passing a custom client instance. #446

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ export const Consumer = Context.Consumer;

let hasWarnedAboutDefault = false;

export const useClient = (): Client => {
export const useClient = (overrideClient?: Client): Client => {
const client = useContext(Context);

if (overrideClient) {
return overrideClient;
}

if (
process.env.NODE_ENV !== 'production' &&
client === defaultClient &&
Expand Down
20 changes: 20 additions & 0 deletions src/hooks/useQuery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,24 @@ describe('useQuery', () => {
rerender({ query: mockQuery, variables: mockVariables, pause: true });
expect(client.executeQuery).toBeCalledTimes(1);
});

it('should support passing a custom client instance', async () => {
const mock = {
executeQuery: jest.fn(() =>
pipe(
interval(1),
map(() => ({ data: {} }))
)
),
} as any;

const { waitForNextUpdate } = renderHook(
({ query, variables }) => useQuery({ query, variables, client: mock }),
{ initialProps: { query: mockQuery, variables: mockVariables } }
);

await waitForNextUpdate();
expect(mock.executeQuery).toBeCalledTimes(1);
expect(client.executeQuery).toBeCalledTimes(0);
});
});
4 changes: 3 additions & 1 deletion src/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { pipe, onEnd, subscribe } from 'wonka';
import { useClient } from '../context';
import { OperationContext, RequestPolicy } from '../types';
import { CombinedError, noop } from '../utils';
import { Client } from '../client';
import { useRequest } from './useRequest';
import { useImmediateEffect } from './useImmediateEffect';
import { useImmediateState } from './useImmediateState';
Expand All @@ -14,6 +15,7 @@ export interface UseQueryArgs<V> {
requestPolicy?: RequestPolicy;
pollInterval?: number;
context?: Partial<OperationContext>;
client?: Client;
pause?: boolean;
}

Expand All @@ -33,7 +35,7 @@ export const useQuery = <T = any, V = object>(
args: UseQueryArgs<V>
): UseQueryResponse<T> => {
const unsubscribe = useRef(noop);
const client = useClient();
const client = useClient(args.client);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh before I forget 😅 let’s get this added to the other hooks as well.

useSubscription will just need another arg, just like useQuery. For useMutation we can probably use an optional second argument.


// This is like useState but updates the state object
// immediately, when we're still before the initial mount
Expand Down