Skip to content

Commit 9ceaa78

Browse files
Test and fix relationship update
1 parent fade4c9 commit 9ceaa78

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ class RequestHandler extends APIHandlerBase {
830830
where: this.makeIdFilter(typeInfo.idFields, resourceId),
831831
select: {
832832
...typeInfo.idFields.reduce((acc, field) => ({ ...acc, [field.name]: true }), {}),
833-
[relationship]: { select: { [this.makeIdKey(relationInfo.idFields)]: true } },
833+
[relationship]: { select: this.makeIdSelect(relationInfo.idFields) },
834834
},
835835
};
836836

@@ -885,9 +885,9 @@ class RequestHandler extends APIHandlerBase {
885885

886886
updateArgs.data = {
887887
[relationship]: {
888-
[relationVerb]: enumerate(parsed.data.data).map((item: any) => ({
889-
[this.makeIdKey(relationInfo.idFields)]: item.id,
890-
})),
888+
[relationVerb]: enumerate(parsed.data.data).map((item: any) =>
889+
this.makeIdFilter(relationInfo.idFields, item.id)
890+
),
891891
},
892892
};
893893
}

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { loadSchema, run } from '@zenstackhq/testtools';
66
import { Decimal } from 'decimal.js';
77
import SuperJSON from 'superjson';
88
import makeHandler, { idDivider } from '../../src/api/rest';
9-
import e from 'express';
109

1110
describe('REST server tests', () => {
1211
let prisma: any;
@@ -137,7 +136,7 @@ describe('REST server tests', () => {
137136
path: '/user',
138137
prisma,
139138
});
140-
console.log('yufail', JSON.stringify(r));
139+
141140
expect(r.status).toBe(200);
142141
expect(r.body).toMatchObject({
143142
data: [],
@@ -324,8 +323,6 @@ describe('REST server tests', () => {
324323
prisma,
325324
});
326325

327-
console.log('yufail', JSON.stringify(r));
328-
329326
expect(r.status).toBe(200);
330327
expect(r.body).toMatchObject({
331328
links: {
@@ -1641,6 +1638,27 @@ describe('REST server tests', () => {
16411638
expect(r.status).toBe(404);
16421639
});
16431640

1641+
it('create relation with compound id', async () => {
1642+
await prisma.user.create({ data: { myId: 'user1', email: 'user1@abc.com' } });
1643+
await prisma.post.create({ data: { id: 1, title: 'Post1' } });
1644+
1645+
const r = await handler({
1646+
method: 'post',
1647+
path: '/postLike',
1648+
query: {},
1649+
requestBody: {
1650+
data: {
1651+
type: 'postLike',
1652+
id: `1${idDivider}user1`,
1653+
attributes: { userId: 'user1', postId: 1, superLike: false },
1654+
},
1655+
},
1656+
prisma,
1657+
});
1658+
1659+
expect(r.status).toBe(201);
1660+
});
1661+
16441662
describe('compound id', () => {
16451663
beforeEach(async () => {
16461664
await prisma.user.create({ data: { myId: 'user1', email: 'user1@abc.com' } });
@@ -1909,6 +1927,24 @@ describe('REST server tests', () => {
19091927
});
19101928
});
19111929

1930+
it('update a collection of relations with compound id', async () => {
1931+
await prisma.user.create({ data: { myId: 'user1', email: 'user1@abc.com' } });
1932+
await prisma.post.create({ data: { id: 1, title: 'Post1' } });
1933+
await prisma.postLike.create({ data: { userId: 'user1', postId: 1, superLike: false } });
1934+
1935+
const r = await handler({
1936+
method: 'patch',
1937+
path: '/post/1/relationships/likes',
1938+
query: {},
1939+
requestBody: {
1940+
data: [{ type: 'postLike', id: '1_user1', attributes: { superLike: true } }],
1941+
},
1942+
prisma,
1943+
});
1944+
1945+
expect(r.status).toBe(200);
1946+
});
1947+
19121948
it('update a collection of relations to empty', async () => {
19131949
await prisma.user.create({
19141950
data: { myId: 'user1', email: 'user1@abc.com', posts: { create: { id: 1, title: 'Post1' } } },

0 commit comments

Comments
 (0)