Sha256: 9d41f453aee32029ca291213afb732718b49cb9073b9a22e5b60cc0a92f7aa0a

Contents?: true

Size: 1.07 KB

Versions: 41

Compression:

Stored size: 1.07 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| @edge_class.new(item, parent) }
          end
        end
      end
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
graphql-1.8.4 lib/graphql/relay/edges_instrumentation.rb