从解析器向类型传递不仅仅是一个对象的更多数据

时间:2019-06-12 20:29:31

标签: ruby-on-rails ruby graphql graphql-ruby

我的客户类型上有预订字段,但是在预订类型中有一个字段,我不仅需要AR对象,还需要商品代码。我的第一个想法是仅装饰AR对象以了解代码,但我想知道是否有更好的方法。

我不想再次从预订中致电客户。也许有些类似于我可以向其中添加数据的上下文?

我当前的实现与此类似:

module Types
  class CustomerType < Types::BaseObject
    alias customer object

    field :bookings, [BookingType]
    def bookings
      offer_code = customer.offers.first&.code

      customer.bookings.map { |booking|
        BookingWithOfferCode.new(booking, offer_code)
      }
    end
  end

  class BookingType < Type::BaseObject
    alias booking object

    field :some_field_requiring_the_offer_code, SomeResultType
    def some_field_requiring_the_offer_code
      offer_code = booking.offer_code
      # doing something with the offer code
    end
  end
end

class BookingWithOfferCode < SimpleDelegator
  attr_reader :offer_code

  def initialize(booking, offer_code)
    super(booking)
    @offer_code = offer_code
  end
end

0 个答案:

没有答案