Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/server/src/api/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,9 @@ class RequestHandler extends APIHandlerBase {
}
}

// include IDs of relation fields so that they can be serialized.
this.includeRelationshipIds(type, createPayload, 'include');

const entity = await prisma[type].create(createPayload);
return {
status: 201,
Expand Down Expand Up @@ -970,6 +973,9 @@ class RequestHandler extends APIHandlerBase {
}
}

// include IDs of relation fields so that they can be serialized.
this.includeRelationshipIds(type, updatePayload, 'include');

const entity = await prisma[type].update(updatePayload);
return {
status: 200,
Expand Down
98 changes: 97 additions & 1 deletion packages/server/tests/api/rest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,40 @@ describe('REST server tests', () => {
});
});

it('returns an empty data array when loading empty related resources', async () => {
// Create a user first
await prisma.user.create({
data: { myId: 'user1', email: 'user1@abc.com' },
});

const r = await handler({
method: 'get',
path: '/user/user1',
prisma,
});

expect(r.status).toBe(200);
expect(r.body).toMatchObject({
data: {
type: 'user',
id: 'user1',
attributes: { email: 'user1@abc.com' },
links: {
self: 'http://localhost/api/user/user1',
},
relationships: {
posts: {
links: {
self: 'http://localhost/api/user/user1/relationships/posts',
related: 'http://localhost/api/user/user1/posts',
},
data: [],
},
},
},
});
});

it('fetches a related resource with a compound ID', async () => {
await prisma.user.create({
data: {
Expand Down Expand Up @@ -1427,7 +1461,21 @@ describe('REST server tests', () => {
expect(r.status).toBe(201);
expect(r.body).toMatchObject({
jsonapi: { version: '1.1' },
data: { type: 'user', id: 'user1', attributes: { email: 'user1@abc.com' } },
data: {
type: 'user',
id: 'user1',
attributes: { email: 'user1@abc.com' },
relationships: {
posts: {
links: {
self: 'http://localhost/api/user/user1/relationships/posts',
related: 'http://localhost/api/user/user1/posts',
},
data: [],
},
},
links: { self: 'http://localhost/api/user/user1' },
},
});
});

Expand Down Expand Up @@ -1785,6 +1833,54 @@ describe('REST server tests', () => {
});
});

it("returns an empty data list in relationships if it's empty", async () => {
await prisma.user.create({
data: {
myId: 'user1',
email: 'user1@abc.com',
},
});

const r = await handler({
method: 'put',
path: '/user/user1',
query: {},
requestBody: {
data: {
type: 'user',
attributes: { email: 'user2@abc.com' },
},
},
prisma,
});

expect(r.status).toBe(200);
expect(r.body).toMatchObject({
links: {
self: 'http://localhost/api/user/user1',
},
data: {
type: 'user',
id: 'user1',
attributes: {
email: 'user2@abc.com',
},
links: {
self: 'http://localhost/api/user/user1',
},
relationships: {
posts: {
links: {
self: 'http://localhost/api/user/user1/relationships/posts',
related: 'http://localhost/api/user/user1/posts',
},
data: [],
},
},
},
});
});

it('returns 404 if the user does not exist', async () => {
const r = await handler({
method: 'put',
Expand Down
Loading