jsonapi icon indicating copy to clipboard operation
jsonapi copied to clipboard

belongs to relationship won't show without preloading, but preloading is not necessary

Open strzibny opened this issue 6 years ago • 2 comments

Hello all,

I am trying to implement JSON:API endpoints where I have belongs_to relationship so the ID is directly in the loaded table, but this library (probably because of https://github.com/jeregrine/jsonapi/pull/103) won't show these relationships unless they are explicitely loaded.

I think preloading the belongs_to relationship makes no sense as we only display the ID and type. For has_many relationship, the current behaviour is of course desirable.

Here is a workaround by setting the relationship by hand:

def show(conn, %{"id" => id}) do
    object = Document.get_document{(%{login_id: conn.assigns[:login_id], id: id})

    object = %{object | folder: %{id: object.folder_id}}
    render(conn, "show.json", data: object)
end

After this the folder relationship will be included and no other SQL queries (or joins) need to happen...

What do you think? Am I missing something?

strzibny avatar Apr 04 '19 16:04 strzibny

I totally agree with you. I've been trying to come up with a resolution to this in #174 , but I'm a bit drained for time at the moment. The biggest challenge as I see it is changing how relationships work whilst providing a clean upgrade path.

jherdman avatar Apr 04 '19 18:04 jherdman