Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON API filter included resources

The question is about JSON API specification and how properly do a request (I'm using ruby on rails and the json api resources gem but that's a general question anyway, I know how to implement it, I just want to follow the rules of JSON API at: http://jsonapi.org/format/)

Situation 1:

  • I want to get all shelves
  • I want to include all books that are on those shelves
  • The get I'm supposed to use in this case is: www.library.com/shelves?include=books

Situation 2:

  • I want to get all books but only books that are marked as unread

  • The get I'm supposed to use is: www.library.com/books?filter[unread]=true

What would be correct way of designing request for all shelves with included unread books?

Can't figure this one out

www.library.com/shelves?include=books&filter[books.unread]=true ?

www.library.com/shelves?include=unread_books ? <- would have to specify another resource, books that are unread

www.library.com/shelves?filter[books.unread]=true ?

What's the most correct way of doing this?

EDIT

After speaking with my tech lead and a few other programmers, the first options is favoured the most in such cases

like image 974
beniutek Avatar asked Oct 23 '16 15:10

beniutek


2 Answers

I would bet on the first one:

www.library.com/shelves?include=books&filter[books.unread]=true
like image 53
brian Avatar answered Nov 18 '22 02:11

brian


JSON API currently does not support filtering includes, but this doesn't mean you have to be strict on the definition (check https://github.com/cerebris/jsonapi-resources/issues/314)

I would go with the same approach as brian: www.library.com/shelves?include=books&filter[books.unread]=true

I just wanted to give some more background to the answer.

like image 2
Luiz Sotero Avatar answered Nov 18 '22 01:11

Luiz Sotero