Coder Social home page Coder Social logo

Comments (1)

nicolabba avatar nicolabba commented on June 10, 2024

Hi, I modified the loader: ActiveStorageLoader to work with has_many_attached.
The main difference is an additional parameter in the initialize method, that by default is set to :has_one_attached but can be set to :has_many_attached for your use case.

####
# This is a loader for has_one_attached and has_many_attached Active Storage attachments
# To load a variant for an attachment, 2 queries are required
# Using preloading via the includes method.
####

####
# The model with an attached image and many attached pictures
####

# class Event < ApplicationRecord
#   has_one_attached :image
#   has_many_attached :pictures
# end

####
# An example data type using the AttachmentLoader
####

# class Types::EventType < Types::BaseObject
#   graphql_name 'Event'
#
#   field :id, ID, null: false
#   field :image, String, null: true
#   field :pictures, String, null: true
#
#   def image
#     AttachmentLoader.for(:Event, :image).load(object.id).then do |image|
#       Rails.application.routes.url_helpers.url_for(
#         image.variant({ quality: 75 })
#       )
#     end
#   end
#
#   def pictures
#     AttachmentLoader.for(:Event, :pictures, association_type: :has_many_attached).load(object.id).then do |pictures|
#       pictures.map do |picture|
#         Rails.application.routes.url_helpers.url_for(
#           picture.variant({ quality: 75 })
#         )
#       end
#     end
#   end
# end
module Loaders
  class ActiveStorageLoader < GraphQL::Batch::Loader
    attr_reader :record_type, :attachment_name, :association_type # should be has_one_attached or has_many_attached

    def initialize(record_type, attachment_name, association_type: :has_one_attached)
      super()
      @record_type = record_type
      @attachment_name = attachment_name
      @association_type = association_type
    end

    def perform(record_ids)
      # find records and fulfill promises
      attachments = ActiveStorage::Attachment.includes(:blob).where(record_type: record_type, record_id: record_ids, name: attachment_name)

      if @association_type == :has_one_attached
        attachments.each do |attachment|
          fulfill(attachment.record_id, attachment)
        end

        record_ids.each { |id| fulfill(id, nil) unless fulfilled?(id) }
      else
        record_ids.each do |record_id|
          fulfill(record_id, attachments.select { |attachment| attachment.record_id == record_id })
        end
      end
    end
  end
end

from graphql-batch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.