P
Prisma2mo ago
LukasK

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.
Query optimization | Prisma Documentation
How Prisma optimizes queries under the hood
4 Replies
RaphaelEtim
RaphaelEtim2mo ago
Hi @LukasK Client Extension doesn't currently support Fluent API as can be seen in this open issue
GitHub
Client extensions cannot support Fluent API · Issue #24653 · prisma...
Problem Putting this together because it's been mentioned a bunch of places but without the root issue being logged, see prisma/extension-read-replicas#12 and the Accelerate limitations page. W...
LukasK
LukasKOP2mo ago
Hi @RaphaelEtim thank you for your fast reply. Is there no way to use the Fluent API with the client extension? Are there any updates on when this issue will be resolved?
RaphaelEtim
RaphaelEtim2mo ago
Is there no way to use the Fluent API with the client extension?
At the moment none officially but a user shared a workaround here
Are there any updates on when this issue will be resolved?
I don't have an ETA at the moment for when it would be implemented.
LukasK
LukasKOP2mo ago
Ok, thank you. Have a nice day!

Did you find this page helpful?