Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 12 additions & 3 deletions packages/runtime/src/client/client-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,9 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
throwIfNoResult = false,
) => {
return createZenStackPromise(async (txClient?: ClientContract<Schema>) => {
let proceed = async (_args?: unknown) => {
let proceed = async (_args: unknown) => {
const _handler = txClient ? handler.withClient(txClient) : handler;
const r = await _handler.handle(operation, _args ?? args);
const r = await _handler.handle(operation, _args);
if (!r && throwIfNoResult) {
throw new NotFoundError(model);
}
Expand All @@ -379,7 +379,16 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
const onQuery = plugin.onQuery;
if (onQuery) {
const _proceed = proceed;
proceed = () => onQuery({ client, model, operation, args, proceed: _proceed }) as Promise<unknown>;
proceed = (_args: unknown) =>
onQuery({
client,
model,
operation,
// reflect the latest override if provided
args: _args,
// ensure inner overrides are propagated to the previous proceed
proceed: (nextArgs: unknown) => _proceed(nextArgs),
}) as Promise<unknown>;
}
}

Expand Down
38 changes: 38 additions & 0 deletions packages/runtime/test/plugin/on-query-hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,42 @@ describe('On query hooks tests', () => {
).resolves.toMatchObject(user);
expect(findHookCalled).toBe(true);
});

it('propagates overridden args across multiple onQuery plugins', async () => {
const user = await _client.user.create({ data: { email: '[email protected]' } });

let earlierSawOverridden = false;

// Plugin A (registered first) should see the overridden args from Plugin B
const clientA = _client.$use({
id: 'plugin-a',
onQuery: (ctx) => {
if (ctx.model === 'User' && ctx.operation === 'findFirst') {
// expect overridden where clause from Plugin B
// eslint-disable-next-line @typescript-eslint/no-explicit-any
earlierSawOverridden = (ctx.args as any)?.where?.id === 'non-exist';
}
return ctx.proceed(ctx.args);
},
});

// Plugin B (registered second) overrides args
const client = clientA.$use({
id: 'plugin-b',
onQuery: (ctx) => {
if (ctx.model === 'User' && ctx.operation === 'findFirst') {
return ctx.proceed({ where: { id: 'non-exist' } });
}
return ctx.proceed(ctx.args);
},
});

await expect(
client.user.findFirst({
where: { id: user.id },
}),
).toResolveNull();

expect(earlierSawOverridden).toBe(true);
});
});