Sha256: 6e25cfdbe42c41ab1083ff8102fddb5a6347d3d3e601a2a9a6ad3b895158be4d

Contents?: true

Size: 1.77 KB

Versions: 13

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true
module GraphQL
  module Relay
    class ConnectionResolve
      def initialize(field, underlying_resolve, lazy:)
        @field = field
        @underlying_resolve = underlying_resolve
        @max_page_size = field.connection_max_page_size
        @lazy = lazy
      end

      def call(obj, args, ctx)
        if @lazy && obj.is_a?(LazyNodesWrapper)
          parent = obj.parent
          obj = obj.lazy_object
        else
          parent = obj
        end

        nodes = @underlying_resolve.call(obj, args, ctx)

        if nodes.nil?
          nil
        elsif ctx.schema.lazy?(nodes)
          if !@lazy
            LazyNodesWrapper.new(obj, nodes)
          else
            nodes
          end
        else
          build_connection(nodes, args, parent, ctx)
        end
      end

      private

      def build_connection(nodes, args, parent, ctx)
        if nodes.is_a? GraphQL::ExecutionError
          ctx.add_error(nodes)
          nil
        else
          connection_class = GraphQL::Relay::BaseConnection.connection_for_nodes(nodes)
          connection_class.new(nodes, args, field: @field, max_page_size: @max_page_size, parent: parent, context: ctx)
        end
      end

      # A container for the proper `parent` of connection nodes.
      # Without this wrapper, the lazy object _itself_ is passed into `build_connection`
      # and it becomes the parent, which is wrong.
      #
      # We can get away with it because we know that this instrumentation will be applied last.
      # That means its code after `underlying_resolve` will be _last_ on the way in.
      # And, its code before `underlying_resolve` will be _first_ during lazy resolution.
      # @api private
      LazyNodesWrapper = Struct.new(:parent, :lazy_object)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
graphql-1.7.1 lib/graphql/relay/connection_resolve.rb
graphql-1.7.0 lib/graphql/relay/connection_resolve.rb
graphql-1.6.8 lib/graphql/relay/connection_resolve.rb
graphql-1.6.7 lib/graphql/relay/connection_resolve.rb
graphql-1.6.6 lib/graphql/relay/connection_resolve.rb
graphql-1.6.5 lib/graphql/relay/connection_resolve.rb
graphql-1.6.4 lib/graphql/relay/connection_resolve.rb
graphql-1.5.15 lib/graphql/relay/connection_resolve.rb
graphql-1.6.3 lib/graphql/relay/connection_resolve.rb
graphql-1.6.2 lib/graphql/relay/connection_resolve.rb
graphql-1.6.1 lib/graphql/relay/connection_resolve.rb
graphql-1.6.0 lib/graphql/relay/connection_resolve.rb
graphql-1.5.14 lib/graphql/relay/connection_resolve.rb