Skip to content

Commit c543d50

Browse files
author
Lee Richmond
committed
Bypass required filters on .find
If there's a required filter, we should only require it when calling `.all` and not `.find` - because there's no filtering to do here, we already have the ID. Any auth would already be done via `base_scope`. The other thing special about `.find` is we should raise an error when a record is missing so we can render a 404. This follows that same pattern, passing a special flag for this use case.
1 parent a5c32ed commit c543d50

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

lib/graphiti/resource/interface.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ def _find(params = {}, base_scope = nil)
2828
params[:filter][:id] = id if id
2929

3030
runner = Runner.new(self, params)
31-
runner.proxy(base_scope, single: true, raise_on_missing: true)
31+
runner.proxy base_scope,
32+
single: true,
33+
raise_on_missing: true,
34+
bypass_required_filters: true
3235
end
3336

3437
def build(params, base_scope = nil)

lib/graphiti/runner.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ def proxy(base = nil, opts = {})
6363
:after_resolve,
6464
:sideload,
6565
:parent,
66-
:params
66+
:params,
67+
:bypass_required_filters
6768
scope = jsonapi_scope(base, scope_opts)
6869
ResourceProxy.new jsonapi_resource,
6970
scope,

lib/graphiti/scoping/filter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Scoping::Filter < Scoping::Base
33
include Scoping::Filterable
44

55
def apply
6-
if missing_required_filters.any?
6+
if missing_required_filters.any? && !@opts[:bypass_required_filters]
77
raise Errors::RequiredFilter.new(resource, missing_required_filters)
88
end
99

spec/filtering_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,19 @@ def admin?
17021702
records
17031703
}.to raise_error(Graphiti::Errors::RequiredFilter)
17041704
end
1705+
1706+
context 'because it came from .find' do
1707+
before do
1708+
resource.filter :id, :integer
1709+
end
1710+
1711+
it 'does not require the filter' do
1712+
expect {
1713+
proxy = resource.find(filter: { id: employee2.id })
1714+
expect(proxy.data.id).to eq(employee2.id)
1715+
}.to_not raise_error
1716+
end
1717+
end
17051718
end
17061719
end
17071720

0 commit comments

Comments
 (0)