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

Version Path
graphiti_gql-0.2.38 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.37 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.36 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.35 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.34 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.33 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.32 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.31 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.30 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.29 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.28 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.27 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.26 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.25 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.24 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.23 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.22 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.21 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.20 lib/graphiti_gql/schema/fields/to_one.rb
graphiti_gql-0.2.19 lib/graphiti_gql/schema/fields/to_one.rb