Skip to content

Commit 8638b26

Browse files
Include relationships in REST result from creates and updates (#1798)
1 parent 58d9d91 commit 8638b26

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,9 @@ class RequestHandler extends APIHandlerBase {
799799
}
800800
}
801801

802+
// include IDs of relation fields so that they can be serialized.
803+
this.includeRelationshipIds(type, createPayload, 'include');
804+
802805
const entity = await prisma[type].create(createPayload);
803806
return {
804807
status: 201,
@@ -970,6 +973,9 @@ class RequestHandler extends APIHandlerBase {
970973
}
971974
}
972975

976+
// include IDs of relation fields so that they can be serialized.
977+
this.includeRelationshipIds(type, updatePayload, 'include');
978+
973979
const entity = await prisma[type].update(updatePayload);
974980
return {
975981
status: 200,

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

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,40 @@ describe('REST server tests', () => {
305305
});
306306
});
307307

308+
it('returns an empty data array when loading empty related resources', async () => {
309+
// Create a user first
310+
await prisma.user.create({
311+
data: { myId: 'user1', email: '[email protected]' },
312+
});
313+
314+
const r = await handler({
315+
method: 'get',
316+
path: '/user/user1',
317+
prisma,
318+
});
319+
320+
expect(r.status).toBe(200);
321+
expect(r.body).toMatchObject({
322+
data: {
323+
type: 'user',
324+
id: 'user1',
325+
attributes: { email: '[email protected]' },
326+
links: {
327+
self: 'http://localhost/api/user/user1',
328+
},
329+
relationships: {
330+
posts: {
331+
links: {
332+
self: 'http://localhost/api/user/user1/relationships/posts',
333+
related: 'http://localhost/api/user/user1/posts',
334+
},
335+
data: [],
336+
},
337+
},
338+
},
339+
});
340+
});
341+
308342
it('fetches a related resource with a compound ID', async () => {
309343
await prisma.user.create({
310344
data: {
@@ -1427,7 +1461,21 @@ describe('REST server tests', () => {
14271461
expect(r.status).toBe(201);
14281462
expect(r.body).toMatchObject({
14291463
jsonapi: { version: '1.1' },
1430-
data: { type: 'user', id: 'user1', attributes: { email: '[email protected]' } },
1464+
data: {
1465+
type: 'user',
1466+
id: 'user1',
1467+
attributes: { email: '[email protected]' },
1468+
relationships: {
1469+
posts: {
1470+
links: {
1471+
self: 'http://localhost/api/user/user1/relationships/posts',
1472+
related: 'http://localhost/api/user/user1/posts',
1473+
},
1474+
data: [],
1475+
},
1476+
},
1477+
links: { self: 'http://localhost/api/user/user1' },
1478+
},
14311479
});
14321480
});
14331481

@@ -1785,6 +1833,54 @@ describe('REST server tests', () => {
17851833
});
17861834
});
17871835

1836+
it("returns an empty data list in relationships if it's empty", async () => {
1837+
await prisma.user.create({
1838+
data: {
1839+
myId: 'user1',
1840+
1841+
},
1842+
});
1843+
1844+
const r = await handler({
1845+
method: 'put',
1846+
path: '/user/user1',
1847+
query: {},
1848+
requestBody: {
1849+
data: {
1850+
type: 'user',
1851+
attributes: { email: '[email protected]' },
1852+
},
1853+
},
1854+
prisma,
1855+
});
1856+
1857+
expect(r.status).toBe(200);
1858+
expect(r.body).toMatchObject({
1859+
links: {
1860+
self: 'http://localhost/api/user/user1',
1861+
},
1862+
data: {
1863+
type: 'user',
1864+
id: 'user1',
1865+
attributes: {
1866+
1867+
},
1868+
links: {
1869+
self: 'http://localhost/api/user/user1',
1870+
},
1871+
relationships: {
1872+
posts: {
1873+
links: {
1874+
self: 'http://localhost/api/user/user1/relationships/posts',
1875+
related: 'http://localhost/api/user/user1/posts',
1876+
},
1877+
data: [],
1878+
},
1879+
},
1880+
},
1881+
});
1882+
});
1883+
17881884
it('returns 404 if the user does not exist', async () => {
17891885
const r = await handler({
17901886
method: 'put',

0 commit comments

Comments
 (0)