JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Automatic include directives from filtering on a relationship column?

Open
#1,017 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.3k
Forks
546
PR merge metrics
No merged PRs in 30d

Description

In the apply filters function:


    def apply_filters(records, filters, options = {})
      required_includes = []

      if filters
        filters.each do |filter, value|
          if _resource_klass._relationships.include?(filter)
            if _resource_klass._relationships[filter].belongs_to?
              records = apply_filter(records, _resource_klass._relationships[filter].foreign_key, value, options)
            else
              required_includes.push(filter.to_s)
              records = apply_filter(records, "#{_resource_klass._relationships[filter].table_name}.#{_resource_klass._relationships[filter].primary_key}", value, options)
            end
          else
            records = apply_filter(records, filter, value, options)
          end
        end
      end

      if required_includes.any?
        records = apply_includes(records, options.merge(include_directives: IncludeDirectives.new(_resource_klass, required_includes, force_eager_load: true)))
      end

      records
    end

I noticed there is a line where the filtered relationships are then added as includes which then get returned.

 if required_includes.any?
        records = apply_includes(records, options.merge(include_directives: IncludeDirectives.new(_resource_klass, required_includes, force_eager_load: true)))
      end

This means that I can never just filter on a relationship column but just return the top level object, I have to return the whole filtered relationship as well.

Is that right? Is that in accordance with the JSON API spec?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the apply_filters function shown in the issue and trace how apply_includes and IncludeDirectives handle required relationship includes. Compare the resulting response behavior with the JSON:API specification, then determine and document whether filtering by a relationship column should return only the top-level resource or also the filtered relationship.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.