Filtering parent rows by last child row - Query

I am trying to create a list query that filters its rows by a value on the last created child (one to many relation). To put an example, imagine a books and categories entity with a historical changes on those categories as well.

Books
id | name
1 | book1
2 | book2
3 | book3

Categories
id | name
1 | category1
2 | category2
3 | category3

BookCategoryHistory
book_id | category_id | created_at …
1 | 1 | 01-02-2020
1 | 3 | 02-04-2020
2 | 1 | 03-02-2020

Given those tables, imagine that i would like to find the books that has as the latest category the category3, how could i do that ? Maybe i am struggling too much with this an there is an easy solution, but right now i find myself unable to do it.

Hey @gerardoagl96

Wouldn’t you just do the following?

query {
  categoriesList(last: 1) {
    items {
      books {
        count
        items {
          title
        }
      }
    }
  }
}

Hey @sebastian.scholl thanks for your response,

And no :sweat_smile: i think that query would fail because there is no relation between categories and book, the link is through BookCategoryHistory.

Hey @gerardoagl96 - I understand that, though am curious as to why you’d have that intermediary table/link there?