Sha256: 9d64b3166d0701e0cd6e9831b35cffa6dc5082666ec8404dadd2d008f745d2a9
Contents?: true
Size: 1.53 KB
Versions: 35
Compression:
Stored size: 1.53 KB
Contents
module GraphitiGql class Schema module Fields class ToOne def initialize(sideload, sideload_type) @sideload = sideload @sideload_type = sideload_type end def apply(type) field = type.field @sideload.name, @sideload_type, null: true, extras: [:lookahead] _sideload = @sideload type.define_method(@sideload.name) do |**arguments| Util.is_readable_sideload!(_sideload) if _sideload.type == :has_one id = object.send(_sideload.primary_key) params = { filter: { _sideload.foreign_key => { eq: id } } } resource = Schema.registry.get(@sideload.resource.class)[:resource] return resource.all(params).data[0] end lookahead = arguments[:lookahead] id = object.send(_sideload.foreign_key) if id.nil? Loaders::BelongsTo.for(_sideload, {}).load(nil) else params = Util.params_from_args(arguments) if _sideload.type == :polymorphic_belongs_to id = { id: id, type: object.send(_sideload.grouper.field_name) } end selections = lookahead.selections.map(&:name).sort if selections == [:id] || selections == [:__typename, :id] params[:simpleid] = true end Loaders::BelongsTo.for(_sideload, params).load(id) end end end end end end end
Version data entries
35 entries across 35 versions & 1 rubygems