miragejs / discuss

Ask questions, get help, and share what you've built with Mirage
MIT License
2 stars 0 forks source link

routes for related resources #53

Closed rvmourik closed 4 years ago

rvmourik commented 4 years ago

Hi there,

Started using MirageJS in one of our Vue projects, but I am having problems with setting up routes to related resources.

I have a message and comments model, with a belongsTo and hasMany, the data is there and with alwaysIncludeLinkageData: true I see the data in the response.

      this.get('/messages');
      this.get('/messages/:id');

These routes work just fine, but the one below:

  `this.get('/messages/:id/comments');`

Does not work.

How do I achieve this set-up?

Thanks in advance

samselikoff commented 4 years ago

Shorthands don't work for nested resources, only top-level, so for this one I would just write it out.

Something like:

this.get('/messages/:id/comments', (schema, request) => {
  return schema.messages.find(request.params.id).comments
})

should do the trick!

rvmourik commented 4 years ago

Thanks for the quick reply and solution @samselikoff