Skip to content

Commit

Permalink
Merge pull request #1 from hoverinc/active-storage-association-support
Browse files Browse the repository at this point in the history
active storage attachment association support
  • Loading branch information
glytch authored Aug 6, 2020
2 parents 82fa808 + 6703353 commit 2e029ed
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
graphql-eager_loader (0.1.0)
graphql-eager_loader (0.2.0)

GEM
remote: https://rubygems.org/
Expand Down
17 changes: 14 additions & 3 deletions lib/graphql/eager_loader/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def self.call(selections:, model:)
builder = new(selection: selection, model: model)

if builder.association?
includes[builder.field_name] = builder.includes
includes[builder.association_name] = builder.includes
else
includes.merge!(builder.includes)
end
Expand All @@ -56,24 +56,31 @@ def association?
association.present?
end

def field_name
selection.name
def association_name
association.name
end

private

attr_reader :selection, :model

def includes_model
return ActiveStorage::Attachment if active_storage_attachment?

association&.klass || model
end

def association
return if use_custom_method?
return model.reflect_on_association("#{field_name}_attachment") if active_storage_attachment?

model.reflect_on_association(field_name)
end

def active_storage_attachment?
model.reflect_on_attachment(field_name).present?
end

def use_custom_method?
custom_method_defined? && !ignore_custom_method?
end
Expand All @@ -91,6 +98,10 @@ def custom_method_defined?
def field_owner
selection.field.owner
end

def field_name
selection.name
end
end
end
end
2 changes: 1 addition & 1 deletion lib/graphql/eager_loader/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Graphql
module EagerLoader
VERSION = '0.1.0'
VERSION = '0.2.0'
end
end
6 changes: 4 additions & 2 deletions spec/graphql/eager_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@
end
end

describe 'cursor based pagination, has_many, belongs_to' do
describe 'cursor based pagination, has_many, belongs_to, attachment' do
let(:model) { ::User }
let(:query_string) do
<<-QUERY
query {
users {
nodes {
id
photo { filename }
jobs {
id
user { id }
Expand All @@ -89,7 +90,8 @@
it do
expect(options).to eq(
proposal_documents: {},
jobs: { user: {} }
jobs: { user: {} },
photo_attachment: {}
)
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/internal/app/graphql/types/file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module Types
class File < GraphQL::Schema::Object
field :filename, String, null: false
end
end
9 changes: 9 additions & 0 deletions spec/internal/app/graphql/types/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ class User < GraphQL::Schema::Object
field :jobs, [Job], null: false
field :proposal_documents, [ProposalDocument], null: false
field :order, Order, null: false
field :photo, Types::File, null: true

def order
{ code: SecureRandom.uuid }
end

def photo
object.photo if object.photo.attached?
end

def self.allow_include_builder_fields
[:photo]
end
end
end
2 changes: 2 additions & 0 deletions spec/internal/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
class User < ActiveRecord::Base
has_many :jobs
has_many :proposal_documents

has_one_attached :photo
end

0 comments on commit 2e029ed

Please sign in to comment.