Skip to content

Commit

Permalink
Allow useLazyQuery trigger fn to change query (#10499)
Browse files Browse the repository at this point in the history
* Allow `useLazyQuery` trigger fn to change `query`
fixes #10496

* bump bundle size limit by a smidgen

* Update .changeset/seven-cameras-kiss.md

Co-authored-by: Jerel Miller <[email protected]>

---------

Co-authored-by: Jerel Miller <[email protected]>
  • Loading branch information
phryneas and jerelmiller authored Feb 1, 2023
1 parent 2408fd3 commit 9e54f5d
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-cameras-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Allow the execution function returned by `useLazyQuery` to change the query.
2 changes: 1 addition & 1 deletion config/bundlesize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join } from "path";
import { gzipSync } from "zlib";
import bytes from "bytes";

const gzipBundleByteLengthLimit = bytes("32.37KB");
const gzipBundleByteLengthLimit = bytes("32.42KB");
const minFile = join("dist", "apollo-client.min.cjs");
const minPath = join(__dirname, "..", minFile);
const gzipByteLen = gzipSync(readFileSync(minPath)).byteLength;
Expand Down
71 changes: 71 additions & 0 deletions src/react/hooks/__tests__/useLazyQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,77 @@ describe('useLazyQuery Hook', () => {
});
});


it("changing queries", async () => {
const query1 = gql`
query {
hello
}
`;
const query2 = gql`
query {
name
}
`;
const mocks = [
{
request: { query: query1 },
result: { data: { hello: "world" } },
},
{
request: { query: query2 },
result: { data: { name: "changed" } },
},
];

const cache = new InMemoryCache();
const { result } = renderHook(() => useLazyQuery(query1), {
wrapper: ({ children }) => (
<MockedProvider mocks={mocks} cache={cache}>
{children}
</MockedProvider>
),
});

expect(result.current[1].loading).toBe(false);
expect(result.current[1].data).toBe(undefined);
const execute = result.current[0];

setTimeout(() => execute());

await waitFor(
() => {
expect(result.current[1].loading).toBe(true);
},
{ interval: 1 }
);

await waitFor(
() => {
expect(result.current[1].loading).toBe(false);
},
{ interval: 1 }
);
expect(result.current[1].data).toEqual({ hello: "world" });

setTimeout(() => execute({ query: query2 }));

await waitFor(
() => {
expect(result.current[1].loading).toBe(true);
},
{ interval: 1 }
);

await waitFor(
() => {
expect(result.current[1].loading).toBe(false);
},
{ interval: 1 }
);
expect(result.current[1].data).toEqual({ name: "changed" });
});

it('should fetch data each time the execution function is called, when using a "network-only" fetch policy', async () => {
const mocks = [
{
Expand Down
13 changes: 6 additions & 7 deletions src/react/hooks/useLazyQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ export function useLazyQuery<TData = any, TVariables extends OperationVariables
options?: LazyQueryHookOptions<TData, TVariables>
): LazyQueryResultTuple<TData, TVariables> {
const abortControllersRef = useRef(new Set<AbortController>());
const internalState = useInternalState(
useApolloClient(options && options.client),
query,
);

const execOptionsRef = useRef<Partial<LazyQueryHookOptions<TData, TVariables>>>();
const merged = execOptionsRef.current
? mergeOptions(options, execOptionsRef.current)
: options;
const merged = execOptionsRef.current ? mergeOptions(options, execOptionsRef.current) : options;

const internalState = useInternalState<TData, TVariables>(
useApolloClient(options && options.client),
merged?.query ?? query
);

const useQueryResult = internalState.useQuery({
...merged,
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/common/mergeOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type OptionsUnion<TData, TVariables extends OperationVariables, TContext> =
| MutationOptions<TData, TVariables, TContext>;

export function mergeOptions<
TOptions extends OptionsUnion<any, any, any>
TOptions extends Partial<OptionsUnion<any, any, any>>
>(
defaults: TOptions | Partial<TOptions> | undefined,
options: TOptions | Partial<TOptions>,
Expand Down

0 comments on commit 9e54f5d

Please sign in to comment.