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:

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,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of 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)

  • parent (Object)

    The owner of collection, will be passed to the connection if provided

  • context (GraphQL::Query::Context)

    The surrounding ctx, will be passed to the connection if provided (this is required for cursor encoders)

  • edge_class (Class)

    The class to wrap item with


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

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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection


30
31
32
# File 'lib/graphql/relay/range_add.rb', line 30

def connection
  @connection
end

#edgeObject (readonly)

Returns the value of attribute edge


30
31
32
# File 'lib/graphql/relay/range_add.rb', line 30

def edge
  @edge
end

#parentObject (readonly)

Returns the value of attribute parent


30
31
32
# File 'lib/graphql/relay/range_add.rb', line 30

def parent
  @parent
end