@@ -233,7 +233,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
233
233
}
234
234
}
235
235
236
- private async buildSelectIncludeHierarchy ( model : string , args : any ) {
236
+ private async buildSelectIncludeHierarchy ( model : string , args : any , includeConcreteFields = true ) {
237
237
args = clone ( args ) ;
238
238
const selectInclude : any = this . extractSelectInclude ( args ) || { } ;
239
239
@@ -257,7 +257,10 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
257
257
258
258
if ( ! selectInclude . select ) {
259
259
this . injectBaseIncludeRecursively ( model , selectInclude ) ;
260
- await this . injectConcreteIncludeRecursively ( model , selectInclude ) ;
260
+
261
+ if ( includeConcreteFields ) {
262
+ await this . injectConcreteIncludeRecursively ( model , selectInclude ) ;
263
+ }
261
264
}
262
265
return selectInclude ;
263
266
}
@@ -342,19 +345,9 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
342
345
for ( const subModel of subModels ) {
343
346
// include sub model relation field
344
347
const subRelationName = this . makeAuxRelationName ( subModel ) ;
345
- const includePayload : any = { } ;
346
348
347
- if ( this . options . processIncludeRelationPayload ) {
348
- // use the callback in options to process the include payload, so enhancements
349
- // like 'policy' can do extra work (e.g., inject policy rules)
350
- await this . options . processIncludeRelationPayload (
351
- this . prisma ,
352
- subModel . name ,
353
- includePayload ,
354
- this . options ,
355
- this . context
356
- ) ;
357
- }
349
+ // create a payload to include the sub model relation
350
+ const includePayload = await this . createConcreteRelationIncludePayload ( subModel . name ) ;
358
351
359
352
if ( selectInclude . select ) {
360
353
selectInclude . include = { [ subRelationName ] : includePayload , ...selectInclude . select } ;
@@ -366,6 +359,23 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
366
359
}
367
360
}
368
361
362
+ private async createConcreteRelationIncludePayload ( model : string ) {
363
+ let result : any = { } ;
364
+
365
+ if ( this . options . processIncludeRelationPayload ) {
366
+ // use the callback in options to process the include payload, so enhancements
367
+ // like 'policy' can do extra work (e.g., inject policy rules)
368
+ await this . options . processIncludeRelationPayload ( this . prisma , model , result , this . options , this . context ) ;
369
+
370
+ // the callback may directly reference fields from polymorphic bases, we need to fix it
371
+ // into a proper hierarchy by moving base field references to the base layer relations
372
+ const properHierarchy = await this . buildSelectIncludeHierarchy ( model , result , false ) ;
373
+ result = { ...result , ...properHierarchy } ;
374
+ }
375
+
376
+ return result ;
377
+ }
378
+
369
379
// #endregion
370
380
371
381
// #region create
0 commit comments