Sha256: 02f9f0003e844f95aa92ef04b2f6c901c554d7cfb7577841294bfaa8252b2387

Contents?: true

Size: 1.54 KB

Versions: 36

Compression:

Stored size: 1.54 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,
            lazy_resolve: field.lazy_resolve_proc,
          )

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


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

        # A user's custom Connection may return a lazy object,
        # if so, handle it later.
        def resolve(obj, args, ctx)
          nodes = @resolve_proc.call(obj, args, ctx)
          if ctx.schema.lazy?(nodes)
            ConnectionResolve::LazyNodesWrapper.new(obj, nodes)
          else
            build_edges(nodes, obj)
          end
        end

        # If we get this far, unwrap the wrapper,
        # resolve the lazy object and make the edges as usual
        def lazy_resolve(obj, args, ctx)
          items = @lazy_resolve_proc.call(obj.lazy_object, args, ctx)
          build_edges(items, obj.parent)
        end

        private

        def build_edges(items, connection)
          items.map { |item| @edge_class.new(item, connection) }
        end
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
graphql-1.8.3 lib/graphql/relay/edges_instrumentation.rb
graphql-1.8.2 lib/graphql/relay/edges_instrumentation.rb
graphql-1.8.1 lib/graphql/relay/edges_instrumentation.rb
graphql-1.8.0 lib/graphql/relay/edges_instrumentation.rb
graphql-1.8.0.pre11 lib/graphql/relay/edges_instrumentation.rb
graphql-1.8.0.pre10 lib/graphql/relay/edges_instrumentation.rb
graphql-1.7.14 lib/graphql/relay/edges_instrumentation.rb
graphql-1.8.0.pre9 lib/graphql/relay/edges_instrumentation.rb
graphql-1.8.0.pre8 lib/graphql/relay/edges_instrumentation.rb
graphql-1.7.13 lib/graphql/relay/edges_instrumentation.rb
graphql-1.8.0.pre7 lib/graphql/relay/edges_instrumentation.rb
graphql-1.7.12 lib/graphql/relay/edges_instrumentation.rb
graphql-1.7.11 lib/graphql/relay/edges_instrumentation.rb
graphql-1.7.10 lib/graphql/relay/edges_instrumentation.rb
graphql-1.8.0.pre6 lib/graphql/relay/edges_instrumentation.rb
graphql-1.8.0.pre5 lib/graphql/relay/edges_instrumentation.rb
graphql-1.7.9 lib/graphql/relay/edges_instrumentation.rb
graphql-1.8.0.pre4 lib/graphql/relay/edges_instrumentation.rb
graphql-1.8.0.pre3 lib/graphql/relay/edges_instrumentation.rb
graphql-1.7.8 lib/graphql/relay/edges_instrumentation.rb