Skip to content

Commit 2422a58

Browse files
authored
Merge pull request #403 from OpenAPI-Qraft/fix/use-queries-body
fix(react-client): `@openapi-qraft/react` for the `--queryable-write-operations` option
2 parents 85623a3 + 6fa60c4 commit 2422a58

4 files changed

Lines changed: 162 additions & 2 deletions

File tree

.changeset/vast-rules-relax.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openapi-qraft/react': patch
3+
---
4+
5+
Fix `body` handling for queryable write operations in `useQueries` and `useSuspenseQueries`.

packages/react-client/src/callbacks/useQueries.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { DefaultError, QueriesResults } from '@tanstack/react-query';
88
import type { CreateAPIQueryClientOptions } from '../qraftAPIClient.js';
99
import { useQueries as useQueriesTanstack } from '@tanstack/react-query';
1010
import { composeQueryKey } from '../lib/composeQueryKey.js';
11+
import { prepareRequestFnParameters } from '../lib/prepareRequestFnParameters.js';
1112
import { requestFnResponseRejecter } from '../lib/requestFnResponseRejecter.js';
1213
import { requestFnResponseResolver } from '../lib/requestFnResponseResolver.js';
1314

@@ -48,10 +49,17 @@ export const useQueries: (
4849
queryFn:
4950
optionsWithQueryKey.queryFn ??
5051
function ({ queryKey: [, queryParams], signal, meta }) {
52+
const { parameters, body } = prepareRequestFnParameters(
53+
queryParams,
54+
undefined,
55+
false
56+
);
57+
5158
return qraftOptions
5259
.requestFn(schema, {
53-
parameters: queryParams as never,
60+
parameters: parameters as never,
5461
baseUrl: qraftOptions.baseUrl,
62+
body,
5563
signal,
5664
meta,
5765
})

packages/react-client/src/callbacks/useSuspenseQueries.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
import type { CreateAPIQueryClientOptions } from '../qraftAPIClient.js';
1212
import { useSuspenseQueries as useSuspenseQueriesTanstack } from '@tanstack/react-query';
1313
import { composeQueryKey } from '../lib/composeQueryKey.js';
14+
import { prepareRequestFnParameters } from '../lib/prepareRequestFnParameters.js';
1415
import { requestFnResponseRejecter } from '../lib/requestFnResponseRejecter.js';
1516
import { requestFnResponseResolver } from '../lib/requestFnResponseResolver.js';
1617

@@ -52,10 +53,17 @@ export const useSuspenseQueries: (
5253
queryFn:
5354
optionsWithQueryKey.queryFn ??
5455
function ({ queryKey: [, queryParams], signal, meta }) {
56+
const { parameters, body } = prepareRequestFnParameters(
57+
queryParams,
58+
undefined,
59+
false
60+
);
61+
5562
return qraftOptions
5663
.requestFn(schema, {
57-
parameters: queryParams as never,
64+
parameters: parameters as never,
5865
baseUrl: qraftOptions.baseUrl,
66+
body,
5967
signal,
6068
meta,
6169
})

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

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,145 @@ describe('Queryable write operations', () => {
4848
});
4949
});
5050

51+
it('queries writable operations with a specific body for useQueries', async () => {
52+
const queryClient = new QueryClient();
53+
const qraft = createAPIClient({
54+
requestFn,
55+
baseUrl,
56+
queryClient,
57+
});
58+
59+
const parameters = {
60+
header: {
61+
'x-monite-version': '1',
62+
'x-monite-entity-id': '2',
63+
},
64+
path: {
65+
approval_policy_id: '2',
66+
},
67+
body: {
68+
name: 'New Name',
69+
description: 'New Description',
70+
},
71+
};
72+
73+
const queryKeyParameters = {
74+
...parameters,
75+
body: {
76+
name: 'Another Name',
77+
description: 'Another Description',
78+
},
79+
};
80+
81+
const queryKey =
82+
qraft.approvalPolicies.patchApprovalPoliciesId.getQueryKey(
83+
queryKeyParameters
84+
);
85+
86+
const { result } = renderHook(
87+
() =>
88+
qraft.approvalPolicies.patchApprovalPoliciesId.useQueries({
89+
queries: [{ parameters }, { queryKey }],
90+
}),
91+
{
92+
wrapper: (props) => (
93+
<QueryClientProvider client={queryClient} {...props} />
94+
),
95+
}
96+
);
97+
98+
await waitFor(() => {
99+
expect(result.current[0]?.data).toEqual({
100+
id: '2',
101+
name: 'New Name',
102+
description: 'New Description',
103+
});
104+
expect(result.current[1]?.data).toEqual({
105+
id: '2',
106+
name: 'Another Name',
107+
description: 'Another Description',
108+
});
109+
});
110+
});
111+
112+
it('queries writable operations with a specific body for useSuspenseQueries', async () => {
113+
const queryClient = new QueryClient();
114+
const qraft = createAPIClient({
115+
requestFn,
116+
baseUrl,
117+
queryClient,
118+
});
119+
120+
const parameters = {
121+
header: {
122+
'x-monite-version': '1',
123+
'x-monite-entity-id': '2',
124+
},
125+
path: {
126+
approval_policy_id: '2',
127+
},
128+
body: {
129+
name: 'New Name',
130+
description: 'New Description',
131+
},
132+
};
133+
134+
const queryKeyParameters = {
135+
...parameters,
136+
body: {
137+
name: 'Another Name',
138+
description: 'Another Description',
139+
},
140+
};
141+
142+
const queryKey =
143+
qraft.approvalPolicies.patchApprovalPoliciesId.getQueryKey(
144+
queryKeyParameters
145+
);
146+
147+
const hook = () => {
148+
try {
149+
return qraft.approvalPolicies.patchApprovalPoliciesId.useSuspenseQueries(
150+
{
151+
queries: [{ parameters }, { queryKey }],
152+
}
153+
);
154+
} catch (error) {
155+
return error as Promise<void>;
156+
}
157+
};
158+
159+
const { result: resultWithErrorPromise } = renderHook(hook, {
160+
wrapper: (props) => (
161+
<QueryClientProvider client={queryClient} {...props} />
162+
),
163+
});
164+
165+
expect(resultWithErrorPromise.current).toBeInstanceOf(Promise);
166+
await resultWithErrorPromise.current;
167+
168+
const { result: resultWithData } = renderHook(hook, {
169+
wrapper: (props) => (
170+
<QueryClientProvider client={queryClient} {...props} />
171+
),
172+
});
173+
174+
if (resultWithData.current instanceof Promise) {
175+
throw new Error('Promise should be resolved');
176+
}
177+
178+
expect(resultWithData.current[0].data).toEqual({
179+
id: '2',
180+
name: 'New Name',
181+
description: 'New Description',
182+
});
183+
expect(resultWithData.current[1].data).toEqual({
184+
id: '2',
185+
name: 'Another Name',
186+
description: 'Another Description',
187+
});
188+
});
189+
51190
it('queries writable operations with a specific body for fetchQuery', async () => {
52191
const queryClient = new QueryClient();
53192
const qraft = createAPIClient({

0 commit comments

Comments
 (0)