Class: GraphQL::Relay::RangeAdd

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/relay/range_add.rb

Overview

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.

Examples

Example: Adding a comment to list of comments

post = Post.find(args[:post_id])
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: context,
)

response = {
  post: post,
  comments_connection: range_add.connection,
  new_comment_edge: range_add.edge,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection:, item:, context:, parent: nil, edge_class: nil) ⇒ RangeAdd

Parameters

  • collection (Object) — The list of items to wrap in a connection
  • item (Object) — The newly-added item (will be wrapped in edge_class)
  • context (GraphQL::Query::Context) — The surrounding ctx, will be passed to the connection
  • parent (Object) — The owner of collection, will be passed to the connection if provided
  • edge_class (Class) — The class to wrap item with (defaults to the connection's edge class)

:call-seq:

initialize(Object collection:, Object item:, GraphQL::Query::Context context:, Object parent:, Class edge_class:)


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/graphql/relay/range_add.rb', line 49

def initialize(collection:, item:, context:, parent: nil, edge_class: nil)
  conn_class = context.schema.connections.wrapper_for(collection)
  # The rest will be added by ConnectionExtension
  @connection = conn_class.new(collection, parent: parent, context: context, edge_class: edge_class)
  # Check if this connection supports it, to support old versions of GraphQL-Pro
  @edge = if @connection.respond_to?(:range_add_edge)
    @connection.range_add_edge(item)
  else
    @connection.edge_class.new(item, @connection)
  end

  @parent = parent
end

Instance Attribute Details

#connection ⇒ Object (readonly)

Returns the value of attribute connection.



37
38
39
# File 'lib/graphql/relay/range_add.rb', line 37

def connection
  @connection
end

#edge ⇒ Object (readonly)

Returns the value of attribute edge.



37
38
39
# File 'lib/graphql/relay/range_add.rb', line 37

def edge
  @edge
end

#parent ⇒ Object (readonly)

Returns the value of attribute parent.



37
38
39
# File 'lib/graphql/relay/range_add.rb', line 37

def parent
  @parent
end