ash-project / ash_json_api

The JSON:API extension for the Ash Framework
https://hexdocs.pm/ash_json_api
MIT License
55 stars 41 forks source link

Allow automatic sorting/filtering to be disabled for a resource #183

Closed sevenseacat closed 1 month ago

sevenseacat commented 1 month ago

Is your feature request related to a problem? Please describe.

This would mirror a similar feature in AshGraphQL to disable the automatic input generation and processing.

This would be a bit different in AshJsonApi because I don't think we get the same automatic input checking as we do with Absinthe to not allow unpermitted values, like a filter if filtering is disabled for the resource.

I don't have any real technical reason for wanting this, other than it feels weird to me to give users a lot more control via an API than they would via a web app.

Also, for one action I've implemented my own filtering logic for the only filtering I want to support, and it looks like this:

    read :search do
      description "List artists, optionally filtering by partial name match."

      argument :query, :ci_string do
        description "Filters artist by name."
        constraints allow_empty?: true
        default ""
      end

      # ...

Any automatic filtering could stomp on that, eg. if mutually exclusive values are provided in the auto-filter and as a query argument.

Describe the solution you'd like

Another option for the AshJsonApi.Resource DSL, disable one or both of automatic sorting/filtering generation.

Express the feature either with a change to resource syntax, or with a change to the resource interface

  json_api
    type "artist"
    derive_filter? false
    derive_sort? true # This would be the default anyway but it could be set to false
  end
zachdaniel commented 1 month ago

I've implemented this on both the route and the resource level, which seems like a more flexible choice. I've got an issue in the up next category of the roadmap to include in the json_schema & open api specification a proper format for the derived filter as well. We have this logic in ash_graphql, it's really just a matter of copying it over to ash_json_api. The main thing that we need to figure out is that its actually a recursive type, because the format supports things like {or: [{name: {eq: "Fred"}}, {name: {eq: "George"}}]}.

With that said, json_schema supports references, so we should be able to define some kind of "type-filter-schema", and then type each route's filter parameter as a reference to it. Although...hopefully json_schema supports self referential types? We'll find out :)