Skip to content

Commit

Permalink
fix: actually not fixed issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jeiea committed Nov 8, 2024
1 parent 5857d4b commit f056e57
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
12 changes: 9 additions & 3 deletions packages/openapi-react-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type QueryKey<
Paths extends Record<string, Record<HttpMethod, {}>>,
Method extends HttpMethod,
Path extends PathsWithMethod<Paths, Method>,
> = readonly [FetchClient<Paths>, Method, Path, MaybeOptionalInit<Paths[Path], Method>];
> = readonly [number, Method, Path, MaybeOptionalInit<Paths[Path], Method>];

export type QueryOptionsFunction<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
Method extends HttpMethod,
Expand Down Expand Up @@ -102,12 +102,18 @@ export interface OpenapiQueryClient<Paths extends {}, Media extends MediaType =
useMutation: UseMutationMethod<Paths, Media>;
}

const clientIds = new WeakMap<FetchClient<object, MediaType>, number>();
let lastId = 0;

// TODO: Add the ability to bring queryClient as argument
export default function createClient<Paths extends {}, Media extends MediaType = MediaType>(
client: FetchClient<Paths, Media>,
): OpenapiQueryClient<Paths, Media> {
const clientId = clientIds.get(client) ?? lastId++;
clientIds.set(client, clientId);

const queryFn = async <Method extends HttpMethod, Path extends PathsWithMethod<Paths, Method>>({
queryKey: [client, method, path, init],
queryKey: [_clientId, method, path, init],
signal,
}: QueryFunctionContext<QueryKey<Paths, Method, Path>>) => {
const mth = method.toUpperCase() as Uppercase<typeof method>;
Expand All @@ -120,7 +126,7 @@ export default function createClient<Paths extends {}, Media extends MediaType =
};

const queryOptions: QueryOptionsFunction<Paths, Media> = (method, path, ...[init, options]) => ({
queryKey: [client, method, path, init as InitWithUnknowns<typeof init>] as const,
queryKey: [clientId, method, path, init as InitWithUnknowns<typeof init>] as const,
queryFn,
...options,
});
Expand Down
30 changes: 21 additions & 9 deletions packages/openapi-react-query/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,28 @@ describe("client", () => {
expectTypeOf(result.current.error).toEqualTypeOf<false | null>();
});

it("discriminates different fetch clients", async () => {
const createNewClient = () =>
createClient(
createFetchClient<minimalGetPaths>({ baseUrl, fetch: () => Promise.resolve(Response.json(true)) }),
);
const client1 = createNewClient();
const client2 = createNewClient();
it("should treat different fetch clients as separate instances", async () => {
const fetchClient1 = createFetchClient<minimalGetPaths>({ baseUrl, fetch: fetchInfinite });
const fetchClient2 = createFetchClient<minimalGetPaths>({ baseUrl, fetch: fetchInfinite });
const client1 = createClient(fetchClient1);
const client11 = createClient(fetchClient1);
const client2 = createClient(fetchClient2);

renderHook(
() => {
useQueries({
queries: [
client1.queryOptions("get", "/foo"),
client11.queryOptions("get", "/foo"),
client2.queryOptions("get", "/foo"),
],
});
},
{ wrapper },
);

expect(client1.queryOptions("get", "/foo").queryKey).toEqual(client1.queryOptions("get", "/foo").queryKey);
expect(client1.queryOptions("get", "/foo").queryKey).not.toEqual(client2.queryOptions("get", "/foo").queryKey);
// client1 and client11 have the same fetch client, so they share the same query key
expect(queryClient.isFetching()).toBe(2);
});
});

Expand Down

0 comments on commit f056e57

Please sign in to comment.