Skip to content

Commit 3233e94

Browse files
committed
add suggestions for re-implementing the previous default behavior
1 parent 33a154b commit 3233e94

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

CHANGELOG-pro.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,50 @@
2323
- add `scope: false` to any fields that return arrays to get the previous behavior (no authorization applied to the array; each item authorized on its own)
2424
- Or, apply [scoping](https://graphql-ruby.org/authorization/scoping.html) by manually configuring a `pundit_policy_class` in the field's return type, then adding a `class Scope ...` inside that policy class. See the Pundit docs for the scope class API: https://github.com/varvet/pundit#scopes.
2525

26+
If you want to continue passing _all_ arrays through without scoping (for example, if you know they've already been authorized another way, or if you're OK with them being authorized one-at-a-time later), you can implement this in your base `Scope` class, for example:
27+
28+
```ruby
29+
class BasePolicy
30+
class Scope
31+
def initialize(user, items)
32+
@user = user
33+
@items = items
34+
end
35+
36+
def resolve
37+
if items.is_a?(Array)
38+
items
39+
else
40+
raise "Implement #{self.class}#resolve to filter these items: #{items.inspect}"
41+
end
42+
end
43+
end
44+
45+
# Pass this scope class along to subclasses:
46+
def self.inherited(child_class)
47+
child_class.const_set(:Scope, Class.new(BasePolicy::Scope))
48+
super
49+
end
50+
end
51+
```
52+
53+
Alternatively, you could implement `def self.scope_items(items, context)` to skip arrays, for example:
54+
55+
```ruby
56+
module SkipScopingOnArrays
57+
def scope_items(items, context)
58+
if items.is_a?(Array)
59+
items # return these as-is
60+
else
61+
super
62+
end
63+
end
64+
end
65+
66+
# Then, in type definitions which should skip scoping on arrays:
67+
extend SkipScopingOnArrays
68+
```
69+
2670
# 1.25.2 (29 Dec 2023)
2771

2872
### New Features

0 commit comments

Comments
 (0)