Can we improve how we guard collections of items in the schema? #6038
Replies: 3 comments 11 replies
-
So, this may be news to some (it is for me) but, I was totally misunderstanding the
I initially understood
So, for queries that return an array of items we should be using I think this makes sense if we see policies like this:
So, the |
Beta Was this translation helpful? Give feedback.
-
One more point to make about scoping out items the user is unauthorized to view: This wouldn't really reduce the amount of runtime variability, just move it around. Instead of Policy behaviour depending on what's in the collection (and not just who's asking), we'll now have query behaviour depend on who's asking (and not just what they're asking for). |
Beta Was this translation helpful? Give feedback.
-
I did a bit of googling over the weekend and found it quite difficult to find much about this. On https://graphql.org/learn/authorization/ they actually have the exact example of "only owners can see their drafts". Their recommendation is to have the graphql layer pass a fully hydrated user object down and let the business logic layer handle it. The reason is that you might have multiple apis/consumers of the service, not just the graphql api. In our case I guess that means handling it in Eloquent policies and scopes and not in schema directives? This would be a really large change for us since we extensively rely on directives for authorization right now. This is a flimsy resource but in rmosolgo/graphql-ruby#2169 the issue reporter says they expect the Ruby library to simply remove unauthorized items from the array. The author agrees and mentions that they can use scoping in addition to authorization checks to remove unauthorized items before they get to the checks. For us, this confirms to me that removing unauthorized items from an array with a scope instead of just unauthorizing the entire field array is an accepted pattern and using a scope to this is an accepted approach. The linked page about authorized scoping: https://graphql-ruby.org/authorization/scoping.html . I wanted to find a big in-production graphql api to compare with and it seems like maybe Github itself is the main one since the Facebook one is not public. Unfortunately, skimming their docs last night, I couldn't find anything about this topic. |
Beta Was this translation helpful? Give feedback.
-
Using the
@can
directive withresolve: true
allows us to run a policy on every item in a collection. This will cause a query to fail if the collection contains an item which fails the policy.This is nice and easy in some ways - we can think about the policy in terms of individual items, which may be simpler, and blanket-apply it to collections without thinking about it. However, this can lead to inconsistent errors, since a query may fail or not depending on the contents of the collection. (See #6005.)
Some suggested alternatives:
@canOnParent
instead of@can
on collections. Since this will only apply to a single item, not a collection, any errors may be more consistent.1 vote ·
Beta Was this translation helpful? Give feedback.
All reactions