Skip to content

Instantly share code, notes, and snippets.

View knshiro's full-sized avatar
🍷
Creating a network for international Food and Beverage professionals

Ugo Bataillard knshiro

🍷
Creating a network for international Food and Beverage professionals
View GitHub Profile
@knshiro
knshiro / association_loader.rb
Created May 2, 2019 05:34 — forked from theorygeek/association_loader.rb
Preloading Associations with graphql-batch
# frozen_string_literal: true
class AssociationLoader < GraphQL::Batch::Loader
attr_reader :klass, :association
def initialize(klass, association)
raise ArgumentError, "association to load must be a symbol (got #{association.inspect})" unless association.is_a?(Symbol)
raise ArgumentError, "cannot load associations for class #{klass.name}" unless klass < ActiveRecord::Base
raise TypeError, "association #{association} does not exist on #{klass.name}" unless klass.reflect_on_association(association)
@klass = klass