Sha256: 7bddb8157b6b84a230d847e1ed6fcf2d9320abb1951446c909d3e19948f3e76d

Contents?: true

Size: 1.78 KB

Versions: 119

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true
module GraphQL
  module Relay
    # This provides some isolation from `GraphQL::Relay` internals.
    #
    # Given a list of items and a new item, it will provide a connection and an edge.
    #
    # The connection doesn't receive outside arguments, so the list of items
    # should be ordered and paginated before providing it here.
    #
    # @example Adding a comment to list of comments
    #   post = Post.find(args[:postId])
    #   comments = post.comments
    #   new_comment = comments.build(body: args[:body])
    #   new_comment.save!
    #
    #   range_add = GraphQL::Relay::RangeAdd.new(
    #     parent: post,
    #     collection: comments,
    #     item: new_comment,
    #     context: ctx,
    #   )
    #
    #   response = {
    #     post: post,
    #     commentsConnection: range_add.connection,
    #     newCommentEdge: range_add.edge,
    #   }
    class RangeAdd
      attr_reader :edge, :connection, :parent

      # @param collection [Object] The list of items to wrap in a connection
      # @param item [Object] The newly-added item (will be wrapped in `edge_class`)
      # @param parent [Object] The owner of `collection`, will be passed to the connection if provided
      # @param context [GraphQL::Query::Context] The surrounding `ctx`, will be passed to the connection if provided (this is required for cursor encoders)
      # @param edge_class [Class] The class to wrap `item` with
      def initialize(collection:, item:, parent: nil, context: nil, edge_class: Relay::Edge)
        connection_class = BaseConnection.connection_for_nodes(collection)
        @parent = parent
        @connection = connection_class.new(collection, {}, parent: parent, context: context)
        @edge = edge_class.new(item, @connection)
      end
    end
  end
end

Version data entries

119 entries across 119 versions & 1 rubygems

Version Path
graphql-1.8.18 lib/graphql/relay/range_add.rb
graphql-1.11.5 lib/graphql/relay/range_add.rb
graphql-1.11.4 lib/graphql/relay/range_add.rb
graphql-1.11.3 lib/graphql/relay/range_add.rb
graphql-1.11.2 lib/graphql/relay/range_add.rb
graphql-1.10.14 lib/graphql/relay/range_add.rb
graphql-1.11.1 lib/graphql/relay/range_add.rb
graphql-1.10.13 lib/graphql/relay/range_add.rb
graphql-1.11.0 lib/graphql/relay/range_add.rb
graphql-1.10.12 lib/graphql/relay/range_add.rb
graphql-1.9.21 lib/graphql/relay/range_add.rb
graphql-1.10.11 lib/graphql/relay/range_add.rb
graphql-1.9.20 lib/graphql/relay/range_add.rb
graphql-1.10.10 lib/graphql/relay/range_add.rb
graphql-1.10.9 lib/graphql/relay/range_add.rb
graphql-1.10.8 lib/graphql/relay/range_add.rb
graphql-1.10.7 lib/graphql/relay/range_add.rb
graphql-1.10.6 lib/graphql/relay/range_add.rb
graphql-1.10.5 lib/graphql/relay/range_add.rb
graphql-1.10.4 lib/graphql/relay/range_add.rb