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 useLazyQuery trigger fn to change query #10499

Merged
merged 4 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
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 useLazyQuery trigger fn to change query
phryneas marked this conversation as resolved.
Show resolved Hide resolved
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>>
Copy link
Member Author

@phryneas phryneas Feb 1, 2023

Choose a reason for hiding this comment

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

This was necessary since LazyQueryHookOptions does not 100% match any member of OptionsUnion and this way it felt cleaner than adding LazyQueryHookOptions to the union (which is in the shared utilities folder and has no direct relation to the React hooks). This actually fixes a few things where before, mergeOptions would return a OptionsUnion instead of the correct type.

>(
defaults: TOptions | Partial<TOptions> | undefined,
options: TOptions | Partial<TOptions>,
Expand Down