graphiti-api / graphiti

Stylish Graph APIs
https://www.graphiti.dev/
MIT License
960 stars 138 forks source link

Deep Query Filter #459

Closed onwardmk closed 5 months ago

onwardmk commented 5 months ago

I'm trying out a deep query filter and it's not working as I expect.

Here are my resources:

class RideResource < ApplicationResource
  attribute :requested_start_time, :datetime
  attribute :ride_completion_status, :string
  attribute :ride_type, :string

  belongs_to :account
  belongs_to :transport_type
  belongs_to :rider
end

class TransportTypeResource < ApplicationResource
  attribute :name, :string
  attribute :short_name, :string
  attribute :full_name, :string

  has_many :rides
end

Assuming there is no transport type resource with name "test123," I would expect the following request to return no results, but it's returning all the ride resources: /api/v1/rides?include=transport_type&filter[transport_type.name]=test123

Is this a bug or do I need to configure my resources to behave the way I expect? I looked at the documentation and didn't see anything helpful.

onwardmk commented 5 months ago

The spec doesn't prescribe how to filter on attributes of a related resource. I ended up implementing the filter myself.

Related to this, it would be nice if filters matched on symbols with dots in them, so that the query parameters sent by the client match the spec regarding query parameter families:

filter :"transport_type.name", :string do
  eq do |scope, value|
     scope.joins(:transport_type).where("LOWER(transport_types.name) = :transport_type_name", transport_type_name: value.downcase)
  end
end

I had to use underscores instead:

filter :transport_type_name, :string do
  eq do |scope, value|
     scope.joins(:transport_type).where("LOWER(transport_types.name) = :transport_type_name", transport_type_name: value.downcase)
  end
end