Sha256: b33675e94e8a535be0c3e1753aa3c7aae28028c23cee4a4ac32f008ef2ae8027

Contents?: true

Size: 1.13 KB

Versions: 55

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true
module GraphQL
  module Relay
    module EdgesInstrumentation
      def self.instrument(type, field)
        if field.edges?
          edges_resolve = EdgesResolve.new(edge_class: field.edge_class, resolve: field.resolve_proc)
          edges_lazy_resolve = EdgesResolve.new(edge_class: field.edge_class, resolve: field.lazy_resolve_proc)

          field.redefine(
            resolve: edges_resolve,
            lazy_resolve: edges_lazy_resolve,
          )
        else
          field
        end
      end


      class EdgesResolve
        def initialize(edge_class:, resolve:)
          @edge_class = edge_class
          @resolve_proc = resolve
        end

        # A user's custom Connection may return a lazy object,
        # if so, handle it later.
        def call(obj, args, ctx)
          parent = ctx.object
          nodes = @resolve_proc.call(obj, args, ctx)
          if ctx.schema.lazy?(nodes)
            nodes
          else
            nodes.map { |item| item.is_a?(GraphQL::Pagination::Connection::Edge) ? item : @edge_class.new(item, parent) }
          end
        end
      end
    end
  end
end

Version data entries

55 entries across 55 versions & 1 rubygems

Version Path
graphql-1.10.10 lib/graphql/relay/edges_instrumentation.rb
graphql-1.10.9 lib/graphql/relay/edges_instrumentation.rb
graphql-1.10.8 lib/graphql/relay/edges_instrumentation.rb
graphql-1.10.7 lib/graphql/relay/edges_instrumentation.rb
graphql-1.10.6 lib/graphql/relay/edges_instrumentation.rb
graphql-1.10.5 lib/graphql/relay/edges_instrumentation.rb
graphql-1.10.4 lib/graphql/relay/edges_instrumentation.rb
graphql-1.10.3 lib/graphql/relay/edges_instrumentation.rb
graphql-1.10.2 lib/graphql/relay/edges_instrumentation.rb
graphql-1.10.1 lib/graphql/relay/edges_instrumentation.rb
graphql-1.10.0 lib/graphql/relay/edges_instrumentation.rb
graphql-1.10.0.pre4 lib/graphql/relay/edges_instrumentation.rb
graphql-1.10.0.pre3 lib/graphql/relay/edges_instrumentation.rb
graphql-1.10.0.pre2 lib/graphql/relay/edges_instrumentation.rb
graphql-1.10.0.pre1 lib/graphql/relay/edges_instrumentation.rb