Skip to content

Commit fade4c9

Browse files
Working compound id include read
1 parent 07bb093 commit fade4c9

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

packages/server/src/api/rest/index.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -532,12 +532,12 @@ class RequestHandler extends APIHandlerBase {
532532

533533
const args: any = {
534534
where: this.makeIdFilter(typeInfo.idFields, resourceId),
535-
select: this.makeIdSelect(type, modelMeta),
535+
select: this.makeIdSelect(typeInfo.idFields),
536536
};
537537

538538
// include IDs of relation fields so that they can be serialized
539539
// this.includeRelationshipIds(type, args, 'select');
540-
args.select = { ...args.select, [relationship]: { select: this.makeIdSelect(relationInfo.type, modelMeta) } };
540+
args.select = { ...args.select, [relationship]: { select: this.makeIdSelect(relationInfo.idFields) } };
541541

542542
let paginator: Paginator<any> | undefined;
543543

@@ -1230,15 +1230,11 @@ class RequestHandler extends APIHandlerBase {
12301230
}
12311231
}
12321232

1233-
private makeIdSelect(model: string, modelMeta: ModelMeta) {
1234-
const idFields = getIdFields(modelMeta, model);
1233+
private makeIdSelect(idFields: FieldInfo[]) {
12351234
if (idFields.length === 0) {
12361235
throw this.errors.noId;
1237-
} else if (idFields.length === 1) {
1238-
return { [idFields[0].name]: true };
1239-
} else {
1240-
return { [idFields.map((idf) => idf.name).join(',')]: true };
12411236
}
1237+
return idFields.reduce((acc, curr) => ({ ...acc, [curr.name]: true }), {});
12421238
}
12431239

12441240
private makeIdKey(idFields: FieldInfo[]) {
@@ -1255,7 +1251,7 @@ class RequestHandler extends APIHandlerBase {
12551251
return;
12561252
}
12571253
for (const [relation, relationInfo] of Object.entries(typeInfo.relationships)) {
1258-
args[mode] = { ...args[mode], [relation]: { select: { [this.makeIdKey(relationInfo.idFields)]: true } } };
1254+
args[mode] = { ...args[mode], [relation]: { select: this.makeIdSelect(relationInfo.idFields) } };
12591255
}
12601256
}
12611257

packages/server/tests/api/rest.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe('REST server tests', () => {
2727
updatedAt DateTime @updatedAt
2828
email String @unique @email
2929
posts Post[]
30+
likes PostLike[]
3031
profile Profile?
3132
}
3233
@@ -48,6 +49,7 @@ describe('REST server tests', () => {
4849
publishedAt DateTime?
4950
viewCount Int @default(0)
5051
comments Comment[]
52+
likes PostLike[]
5153
setting Setting?
5254
}
5355
@@ -69,6 +71,8 @@ describe('REST server tests', () => {
6971
postId Int
7072
userId String
7173
superLike Boolean
74+
post Post @relation(fields: [postId], references: [id])
75+
user User @relation(fields: [userId], references: [myId])
7276
@@id([postId, userId])
7377
}
7478
`;
@@ -133,6 +137,7 @@ describe('REST server tests', () => {
133137
path: '/user',
134138
prisma,
135139
});
140+
console.log('yufail', JSON.stringify(r));
136141
expect(r.status).toBe(200);
137142
expect(r.body).toMatchObject({
138143
data: [],
@@ -299,6 +304,37 @@ describe('REST server tests', () => {
299304
});
300305
});
301306

307+
it('fetches a related resource with a compound ID', async () => {
308+
await prisma.user.create({
309+
data: {
310+
myId: 'user1',
311+
email: 'user1@abc.com',
312+
posts: {
313+
create: { id: 1, title: 'Post1' },
314+
},
315+
},
316+
});
317+
await prisma.postLike.create({
318+
data: { postId: 1, userId: 'user1', superLike: true },
319+
});
320+
321+
const r = await handler({
322+
method: 'get',
323+
path: '/post/1/relationships/likes',
324+
prisma,
325+
});
326+
327+
console.log('yufail', JSON.stringify(r));
328+
329+
expect(r.status).toBe(200);
330+
expect(r.body).toMatchObject({
331+
links: {
332+
self: 'http://localhost/api/post/1/relationships/likes',
333+
},
334+
data: [{ type: 'postLike', id: '1_user1' }],
335+
});
336+
});
337+
302338
it('fetch a relationship', async () => {
303339
// Create a user first
304340
await prisma.user.create({

packages/server/tests/api/test.zmodel

Whitespace-only changes.

0 commit comments

Comments
 (0)