LukasK
LukasK
PPrisma
Created by LukasK on 1/8/2025 in #help-and-questions
Fluent API doesn't work as expected with client extensions
Hello, I am building a GraphQL API using NestJs and Prisma ORM. The Prisma client extension is used to manage the RLS policies as follows:
client.$extends({
query: {
$allModels: {
async $allOperations({ args, query }) {
const user = store.get('user');
const userType = store.get('userType');

let configSql: Sql;

switch (userType) {
case UserTypeEnum.ADMIN:
configSql = Prisma.sql`SELECT set_config('app.bypass_rls', 'on', TRUE)`;
break;
case UserTypeEnum.CUSTOMER:
configSql = Prisma.sql`SELECT set_config('app.current_customer_id', ${user.id}, TRUE)`;
break;
}

const [, result] = await client.$transaction([
client.$executeRaw(configSql),
query(args),
]);
return result;
},
},
},
});
client.$extends({
query: {
$allModels: {
async $allOperations({ args, query }) {
const user = store.get('user');
const userType = store.get('userType');

let configSql: Sql;

switch (userType) {
case UserTypeEnum.ADMIN:
configSql = Prisma.sql`SELECT set_config('app.bypass_rls', 'on', TRUE)`;
break;
case UserTypeEnum.CUSTOMER:
configSql = Prisma.sql`SELECT set_config('app.current_customer_id', ${user.id}, TRUE)`;
break;
}

const [, result] = await client.$transaction([
client.$executeRaw(configSql),
query(args),
]);
return result;
},
},
},
});
For all ResolveFields, I am leveraging the Fluent API to address the n+1 problem as described here.
@ResolveField(() => User)
private async author(@Parent() { id }: BlogPost): Promise<User> {
return this._prisma.blogPost.findUnique({ where: { id }}).author();
}
@ResolveField(() => User)
private async author(@Parent() { id }: BlogPost): Promise<User> {
return this._prisma.blogPost.findUnique({ where: { id }}).author();
}
However, I have observed that the Fluent API does not work as expected when using the client extension. When I comment out the client extension, the Fluent API behaves correctly. I'm using @prisma/client: ^5.16.1 and prisma: ^5.16.1. If you need further information or code snippets, let me know. I hope someone can help me to find a solution. Best regards.
6 replies