Skip to content
This repository was archived by the owner on Sep 24, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def find_by_id_field(type, model)
resolve ->(_, args, _) do
gid = GlobalID.parse(args[:id])

return unless gid
return unless gid.model_name == type.name

Graph::FindLoader.for(model).load(gid.model_id.to_i)
Expand Down
49 changes: 49 additions & 0 deletions app/models/graph/association_loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# From: https://github.com/Shopify/graphql-batch/issues/24#issuecomment-277331157
class Graph::AssociationLoader < GraphQL::Batch::Loader
def self.validate(model, association_name)
new(model, association_name)
nil
end

def initialize(model, association_name)
@model = model
@association_name = association_name
validate
end

def load(record)
raise TypeError, "#{@model} loader can't load association for #{record.class}" unless record.is_a?(@model)
return Promise.resolve(read_association(record)) if association_loaded?(record)
super
end

# We want to load the associations on all records, even if they have the same id
def cache_key(record)
record.object_id
end

def perform(records)
preload_association(records)
records.each { |record| fulfill(record, read_association(record)) }
end

private

def validate
unless @model.reflect_on_association(@association_name)
raise ArgumentError, "No association #{@association_name} on #{@model}"
end
end

def preload_association(records)
::ActiveRecord::Associations::Preloader.new.preload(records, @association_name)
end

def read_association(record)
record.public_send(@association_name)
end

def association_loaded?(record)
record.association(@association_name).loaded?
end
end
8 changes: 6 additions & 2 deletions app/models/graph/types/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ module Types
"The eye color of this person. Will be \"unknown\" if not known or \"n/a\" if the person does not have an eye.",
property: :eye_color

field :gender, GenderEnum, "The gender of this person."
field :gender, GenderEnum, "The gender of this person."
field :hairColor, types.String,
"The hair color of this person. Will be \"unknown\" if not known or \"n/a\" if the person does not have hair.",
property: :hair_color

field :height, types.Int, "The height of the person in centimeters."
field :homeworld, Planet, "A planet that this person was born on or inhabits."
field :homeworld, Planet, "A planet that this person was born on or inhabits." do
resolve -> (person, _, _) do
Graph::AssociationLoader.for(::Person, :homeworld).load(person)
end
end
field :mass, types.Int, "The mass of the person in kilograms."
field :name, !types.String, "The name of this person."
field :skinColor, types.String, "The skin color of this person.", property: :skin_color
Expand Down
6 changes: 5 additions & 1 deletion app/models/graph/types/species.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ module Types
end

field :language, types.String, "The language commonly spoken by this species."
field :homeworld, Graph::Types::Planet, "A planet that this species originates from type."
field :homeworld, Planet, "A planet that this species originates from type." do
resolve -> (species, _, _) do
Graph::AssociationLoader.for(::Species, :homeworld).load(species)
end
end
end
end
end