Skip to content

Commit b34531d

Browse files
authored
fix(delegate): make sure concrete fields are included when a polymorphic model field is included in deep nesting (#1524)
1 parent a14bf29 commit b34531d

File tree

2 files changed

+98
-8
lines changed

2 files changed

+98
-8
lines changed

packages/runtime/src/enhancements/delegate.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,13 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
194194

195195
if (this.injectBaseFieldSelect(model, field, value, args, kind)) {
196196
delete args[kind][field];
197-
} else {
198-
if (fieldInfo && this.isDelegateOrDescendantOfDelegate(fieldInfo.type)) {
199-
let nextValue = value;
200-
if (nextValue === true) {
201-
// make sure the payload is an object
202-
args[kind][field] = nextValue = {};
203-
}
204-
this.injectSelectIncludeHierarchy(fieldInfo.type, nextValue);
197+
} else if (fieldInfo.isDataModel) {
198+
let nextValue = value;
199+
if (nextValue === true) {
200+
// make sure the payload is an object
201+
args[kind][field] = nextValue = {};
205202
}
203+
this.injectSelectIncludeHierarchy(fieldInfo.type, nextValue);
206204
}
207205
}
208206
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
describe('issue 1522', () => {
3+
it('regression', async () => {
4+
const { enhance } = await loadSchema(
5+
`
6+
model Course {
7+
id String @id @default(uuid())
8+
title String
9+
description String
10+
sections Section[]
11+
activities Activity[]
12+
@@allow('all', true)
13+
}
14+
15+
model Section {
16+
id String @id @default(uuid())
17+
title String
18+
courseId String
19+
idx Int @default(0)
20+
course Course @relation(fields: [courseId], references: [id])
21+
activities Activity[]
22+
}
23+
24+
model Activity {
25+
id String @id @default(uuid())
26+
title String
27+
courseId String
28+
sectionId String
29+
idx Int @default(0)
30+
type String
31+
course Course @relation(fields: [courseId], references: [id])
32+
section Section @relation(fields: [sectionId], references: [id])
33+
@@delegate(type)
34+
}
35+
36+
model UrlActivity extends Activity {
37+
url String
38+
}
39+
40+
model TaskActivity extends Activity {
41+
description String
42+
}
43+
`,
44+
{ enhancements: ['delegate'] }
45+
);
46+
47+
const db = enhance();
48+
const course = await db.course.create({
49+
data: {
50+
title: 'Test Course',
51+
description: 'Description of course',
52+
sections: {
53+
create: {
54+
id: '00000000-0000-0000-0000-000000000002',
55+
title: 'Test Section',
56+
idx: 0,
57+
},
58+
},
59+
},
60+
include: {
61+
sections: true,
62+
},
63+
});
64+
65+
const section = course.sections[0];
66+
await db.taskActivity.create({
67+
data: {
68+
title: 'Test Activity',
69+
description: 'Description of task',
70+
idx: 0,
71+
courseId: course.id,
72+
sectionId: section.id,
73+
},
74+
});
75+
76+
const found = await db.course.findFirst({
77+
where: { id: course.id },
78+
include: {
79+
sections: {
80+
orderBy: { idx: 'asc' },
81+
include: {
82+
activities: { orderBy: { idx: 'asc' } },
83+
},
84+
},
85+
},
86+
});
87+
88+
expect(found.sections[0].activities[0]).toMatchObject({
89+
description: 'Description of task',
90+
});
91+
});
92+
});

0 commit comments

Comments
 (0)