Extract query attributes from an ecto query

Hello all!

I’m wondering if anyone is aware of a way to obtain the various attributes from a variable of type Ecto.Query, using only the query itself. For example, if I have the variable query = #Ecto.Query<from c0 in App.Context.Sub, order_by: [asc: c0.column1]>, I am hoping to be able to do something like Ecto.Query.get_order_by(query) => {asc: :column1}.

For additional context, we currently have function that return paginated resources. We are thinking that we probably want to extract out pagination into its own module, and in order to do this cleanly, we would need to be able to determine how a query is sorted from only the query itself. This way we are able to properly assign cursor fields for cursor-based pagination.

Thanks in advance!

The order_by clauses are in the Ecto.Query struct, but the format may be tricky to deal with (with table aliases, computed fields, etc).

2 Likes

Ecto querys have only opaque field values. They may change at any given time. Therefore inspecting ecto queries is not possible in a safe manner.

2 Likes