Skip to content

Instantly share code, notes, and snippets.

@masarakki
Last active June 9, 2022 14:57
Show Gist options
  • Save masarakki/b55b9c3ac5c745683a77422bc02008e7 to your computer and use it in GitHub Desktop.
Save masarakki/b55b9c3ac5c745683a77422bc02008e7 to your computer and use it in GitHub Desktop.
graphql-ruby with pundit policy_scope
# loader.rb

def scoped(policy_scope = nil)
  @policy_scope = policy_scope
  self
end

def preload_association(records)
  ActiveRecord::Associations::Preloader.new.preload(records, @association.name, @policy_scope)
end


# user_type.rb

def items
  policy_scope = ItemPolicy::Scope.new(context[:current_user], Item).resolve
  Association::Loader.for(object.class, :items).scoped(policy_scope).load(object)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment