Skip to content

Commit e896c68

Browse files
committed
fix(react-client): added support for the --queryable-write-operations option for the useInfiniteQuery(), fetchQuery(), and other methods
1 parent 92ded42 commit e896c68

4 files changed

Lines changed: 144 additions & 5 deletions

File tree

packages/react-client/src/lib/callQueryClientFetchMethod.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,43 @@ export function callQueryClientMethodWithQueryKey<
5050
(requestFn
5151
? // @ts-expect-error - Too complex union to type
5252
function ({ queryKey: [, queryParams], signal, meta, pageParam }) {
53+
const { body, ...parameters } = queryParams as {
54+
body: BodyInit;
55+
} & typeof queryParams;
56+
5357
return requestFn(schema, {
5458
parameters: infinite
55-
? (shelfMerge(2, queryParams, pageParam) as never)
56-
: queryParams,
59+
? (shelfMerge(
60+
2,
61+
parameters,
62+
(function omitBodyFromParameters() {
63+
if (
64+
pageParam &&
65+
typeof pageParam === 'object' &&
66+
'body' in pageParam
67+
) {
68+
const { body: _body, ...pageParameters } = pageParam;
69+
return pageParameters;
70+
}
71+
72+
return pageParam;
73+
})()
74+
) as never)
75+
: parameters,
5776
baseUrl,
77+
body: infinite
78+
? (function shelfMergeBody() {
79+
if (
80+
pageParam &&
81+
typeof pageParam === 'object' &&
82+
'body' in pageParam
83+
) {
84+
return shelfMerge(1, body, pageParam.body) as BodyInit;
85+
}
86+
87+
return body;
88+
})()
89+
: body,
5890
signal,
5991
meta,
6092
}).then(requestFnResponseResolver, requestFnResponseRejecter);

packages/react-client/src/lib/useComposeUseQueryOptions.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,37 @@ export function useComposeUseQueryOptions(
3434
.requestFn(schema, {
3535
// @ts-expect-error - Too complex to type
3636
parameters: infinite
37-
? (shelfMerge(2, parameters, pageParam) as never)
37+
? (shelfMerge(
38+
2,
39+
parameters,
40+
(function omitBodyFromParameters() {
41+
if (
42+
pageParam &&
43+
typeof pageParam === 'object' &&
44+
'body' in pageParam
45+
) {
46+
const { body: _body, ...pageParameters } = pageParam;
47+
return pageParameters;
48+
}
49+
50+
return pageParam;
51+
})()
52+
) as never)
3853
: parameters,
3954
baseUrl: qraftOptions.baseUrl,
40-
body,
55+
body: infinite
56+
? (function shelfMergeBody() {
57+
if (
58+
pageParam &&
59+
typeof pageParam === 'object' &&
60+
'body' in pageParam
61+
) {
62+
return shelfMerge(1, body, pageParam.body) as BodyInit;
63+
}
64+
65+
return body;
66+
})()
67+
: body,
4168
signal,
4269
meta,
4370
})

packages/react-client/src/tests/msw/handlers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export const handlers = [
6969
name: body.name!,
7070
description: body.description!,
7171
id: path.approval_policy_id,
72+
trigger: body.trigger,
7273
});
7374
}
7475
),

packages/react-client/src/tests/qraftAPIClient.queryable-write-operations.test.tsx

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { createAPIClient } from './fixtures/queryable-write-operations-api/index
99
const baseUrl = 'https://api.sandbox.monite.com/v1';
1010

1111
describe('Queryable write operations', () => {
12-
it('queries writable operations with a specific body', async () => {
12+
it('queries writable operations with a specific body for useQuery', async () => {
1313
const queryClient = new QueryClient();
1414
const qraft = createAPIClient({
1515
requestFn,
@@ -48,6 +48,85 @@ describe('Queryable write operations', () => {
4848
});
4949
});
5050

51+
it('queries writable operations with a specific body for fetchQuery', async () => {
52+
const queryClient = new QueryClient();
53+
const qraft = createAPIClient({
54+
requestFn,
55+
baseUrl,
56+
queryClient,
57+
});
58+
59+
const result =
60+
await qraft.approvalPolicies.patchApprovalPoliciesId.fetchQuery({
61+
parameters: {
62+
header: {
63+
'x-monite-version': '1',
64+
'x-monite-entity-id': '2',
65+
},
66+
path: {
67+
approval_policy_id: '2',
68+
},
69+
body: {
70+
name: 'New Name',
71+
description: 'New Description',
72+
},
73+
},
74+
});
75+
76+
await waitFor(() => {
77+
expect(result).toEqual({
78+
id: '2',
79+
name: 'New Name',
80+
description: 'New Description',
81+
});
82+
});
83+
});
84+
85+
it('queries writable operations with a specific body for fetchInfiniteQuery', async () => {
86+
const queryClient = new QueryClient();
87+
const qraft = createAPIClient({
88+
requestFn,
89+
baseUrl,
90+
queryClient,
91+
});
92+
93+
const result =
94+
await qraft.approvalPolicies.patchApprovalPoliciesId.fetchInfiniteQuery({
95+
parameters: {
96+
header: {
97+
'x-monite-version': '1',
98+
'x-monite-entity-id': '2',
99+
},
100+
path: {
101+
approval_policy_id: '2',
102+
},
103+
body: {
104+
name: 'New Name',
105+
description: 'New Description',
106+
},
107+
},
108+
initialPageParam: {
109+
body: {
110+
trigger: true,
111+
},
112+
},
113+
});
114+
115+
await waitFor(() => {
116+
expect(result).toEqual({
117+
pageParams: [{ body: { trigger: true } }],
118+
pages: [
119+
{
120+
id: '2',
121+
name: 'New Name',
122+
description: 'New Description',
123+
trigger: true,
124+
},
125+
],
126+
});
127+
});
128+
});
129+
51130
it('queries writable operations without body', async () => {
52131
const queryClient = new QueryClient();
53132
const qraft = createAPIClient({

0 commit comments

Comments
 (0)