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

(feat) - allow passing operationContext to useQuery #351

Merged
merged 10 commits into from
Aug 1, 2019
21 changes: 21 additions & 0 deletions src/hooks/useQuery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ describe('useQuery', () => {
});
});

it('should set fetching to true and run effect on first mount', () => {
const context = { url: 'test' };
renderHook(
({ query, variables }) => useQuery({ query, variables, context }),
{ initialProps: { query: mockQuery, variables: mockVariables } }
);

expect(client.executeQuery).toBeCalledWith(
{
key: expect.any(Number),
query: expect.any(Object),
variables: mockVariables,
},
{
meta: { source: 'TestHook' },
requestPolicy: undefined,
url: 'test',
}
);
});

it('should execute the subscription', async () => {
const { waitForNextUpdate } = renderHook(
({ query, variables }) => useQuery({ query, variables }),
Expand Down
11 changes: 10 additions & 1 deletion src/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface UseQueryArgs<V> {
query: string | DocumentNode;
variables?: V;
requestPolicy?: RequestPolicy;
context?: Partial<OperationContext>;
pause?: boolean;
}

Expand Down Expand Up @@ -55,6 +56,7 @@ export const useQuery = <T = any, V = object>(
[unsubscribe.current] = pipe(
client.executeQuery(request, {
requestPolicy: args.requestPolicy,
...args.context,
...opts,
...devtoolsContext,
}),
Expand All @@ -63,7 +65,14 @@ export const useQuery = <T = any, V = object>(
})
);
},
[args.requestPolicy, client, devtoolsContext, request, setState]
[
args.context,
args.requestPolicy,
client,
devtoolsContext,
request,
setState,
]
);

useImmediateEffect(() => {
Expand Down