Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtering / Sorting a Parent Resource based off its associations value #430

Open
Reizar opened this issue Aug 15, 2022 · 1 comment
Open

Comments

@Reizar
Copy link

Reizar commented Aug 15, 2022

I couldn't quite work out if this can be done by reading the docs / playing around with some requests.

I'm looking for a way to filter / sort a parent resource based off a child association's field.

I think an example might be the easiest way to illustrate what I'm wanting to do.

Say you have 2 graphiti resources:

class ParentResource < ApplicationResource
  self.model = Parent
  attribute :something, :string

  has_one :child, resource: ChildResource
end

class ChildResource < ApplicationResource
  self.model = Child
  attribute :other_thing, :string

  attribute :parent_id, :integer_id

  belongs_to :parent, resource: ParentResource
end

Now on the frontend, using Spraypaint to query.
What I would like to do, is something like this:

Parent.includes('child').where({'child.other_thing': 'some_value'}).all()

This will return Parents, but set the child as undefined if the other_thing value doesn't match. However what I want, is some way to exclude the parent if it can't find that child.

I understand that the easiest way to achieve this would be to flip how I query the resources and query for the child. Eg:

Child.includes('parent').where({'other_thing': 'some_value'}).all()

The reason that I want to have some way of filtering / sorting the parent based off the child, is because I have a dropdown with a whole bunch of fields that a user can choose to have their results sorted by. This includes fields from the parent and the child.

For sorting I guess I could change the resource thats queried based off the sort field. Although it does complicate the UI having to accept different models when rendering certain components.
However when it comes to filtering, there are situations where I want to stack multiple filters and that wouldn't work.

@Reizar
Copy link
Author

Reizar commented Aug 15, 2022

Aha, ok I think I worked this one out myself.

I just defined attributes on the parent resource and used a join. eg:

class ParentResource < ApplicationResource
  # .... Other code
  sort :other_thing, :string do |scope, direction|
    scope.joins(:child).order("child.other_thing" => direction).select("parent.*", "child.other_thing")
  end
end

Same idea should work with filters I believe.

Man Graphiti is awesome!

I would close this issue, but I just have one last question with this approach. The select seems to be required as I'm referencing the child value in the order clause. I had to have the parent.* added in to make sure it pulls the parent fields. However this ignores the fieldset requested by the client if the client wants sparse fields.
Is there anyway to pull the select fields within the sort block?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant