Skip to content

Commit ffbaace

Browse files
authored
fix: issue with multiple on query plugins with args override (#169)
1 parent ddd5b4d commit ffbaace

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

packages/runtime/src/client/client-impl.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,9 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
358358
throwIfNoResult = false,
359359
) => {
360360
return createZenStackPromise(async (txClient?: ClientContract<Schema>) => {
361-
let proceed = async (_args?: unknown) => {
361+
let proceed = async (_args: unknown) => {
362362
const _handler = txClient ? handler.withClient(txClient) : handler;
363-
const r = await _handler.handle(operation, _args ?? args);
363+
const r = await _handler.handle(operation, _args);
364364
if (!r && throwIfNoResult) {
365365
throw new NotFoundError(model);
366366
}
@@ -379,7 +379,16 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
379379
const onQuery = plugin.onQuery;
380380
if (onQuery) {
381381
const _proceed = proceed;
382-
proceed = () => onQuery({ client, model, operation, args, proceed: _proceed }) as Promise<unknown>;
382+
proceed = (_args: unknown) =>
383+
onQuery({
384+
client,
385+
model,
386+
operation,
387+
// reflect the latest override if provided
388+
args: _args,
389+
// ensure inner overrides are propagated to the previous proceed
390+
proceed: (nextArgs: unknown) => _proceed(nextArgs),
391+
}) as Promise<unknown>;
383392
}
384393
}
385394

packages/runtime/test/plugin/on-query-hooks.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,42 @@ describe('On query hooks tests', () => {
211211
).resolves.toMatchObject(user);
212212
expect(findHookCalled).toBe(true);
213213
});
214+
215+
it('propagates overridden args across multiple onQuery plugins', async () => {
216+
const user = await _client.user.create({ data: { email: '[email protected]' } });
217+
218+
let earlierSawOverridden = false;
219+
220+
// Plugin A (registered first) should see the overridden args from Plugin B
221+
const clientA = _client.$use({
222+
id: 'plugin-a',
223+
onQuery: (ctx) => {
224+
if (ctx.model === 'User' && ctx.operation === 'findFirst') {
225+
// expect overridden where clause from Plugin B
226+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
227+
earlierSawOverridden = (ctx.args as any)?.where?.id === 'non-exist';
228+
}
229+
return ctx.proceed(ctx.args);
230+
},
231+
});
232+
233+
// Plugin B (registered second) overrides args
234+
const client = clientA.$use({
235+
id: 'plugin-b',
236+
onQuery: (ctx) => {
237+
if (ctx.model === 'User' && ctx.operation === 'findFirst') {
238+
return ctx.proceed({ where: { id: 'non-exist' } });
239+
}
240+
return ctx.proceed(ctx.args);
241+
},
242+
});
243+
244+
await expect(
245+
client.user.findFirst({
246+
where: { id: user.id },
247+
}),
248+
).toResolveNull();
249+
250+
expect(earlierSawOverridden).toBe(true);
251+
});
214252
});

0 commit comments

Comments
 (0)