Fetch target/related of through association

I have a case where I need to do something based on the schema that a given association targets. In every case except for through associations, there is a related key on the association reflection struct. For through associations, it seems you need to recursively traverse the through assoc list until you get the final non-through assoc:

  defp get_assoc_schema(schema, assoc_name) do
    schema.__schema__(:association, assoc_name)
    |> case do
      %{related: schema} -> schema
      %{through: assocs} -> traverse_through_assoc(schema, assocs)

  defp traverse_through_assoc(schema, [final_assoc]) do
    get_assoc_schema(schema, final_assoc)

  defp traverse_through_assoc(schema, [next_assoc | remaining_assocs]) do
    get_assoc_schema(schema, next_assoc)
    |> traverse_through_assoc(remaining_assocs)

I haven’t even fully tested this to know with confidence whether it covers every case, but I figured I should check whether I’m missing something obvious because this feels a bit more onerous than it should be. Is there an easier/more direct way to get the related schema for a through assoc definition?