Skip to content

Commit

Permalink
Merge pull request #3341 from rmosolgo/range-add-improvements
Browse files Browse the repository at this point in the history
Improve RangeAdd and add a guide
  • Loading branch information
Robert Mosolgo authored Feb 18, 2021
2 parents 63ae830 + 0c483c0 commit 16ceb91
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
11 changes: 11 additions & 0 deletions guides/relay/range_add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
layout: guide
doc_stub: false
search: true
section: Relay
title: RangeAdd helper for mutations
desc: A helper for Relay's RANGE_ADD operations
index: 1
---

Relay specifies `RANGE_ADD` operations for adding items to connections. GraphQL-Ruby ships with {{ "GraphQL::Relay::RangeAdd" | api_doc }} to help implement this. Check the API docs for a usage example.
2 changes: 1 addition & 1 deletion guides/schema/sdl.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ The hash may contain:

## Plugins

{ "GraphQL::Schema.from_definition" | api_doc }} accepts a `using:` argument, which may be given as a map of `plugin => args` pairs. For example:
{{ "GraphQL::Schema.from_definition" | api_doc }} accepts a `using:` argument, which may be given as a map of `plugin => args` pairs. For example:

```ruby
MySchema = GraphQL::Schema.from_definition("path/to/schema.graphql", using: {
Expand Down
9 changes: 9 additions & 0 deletions lib/graphql/pagination/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ def first
end
end

# This is called by `Relay::RangeAdd` -- it can be overridden
# when `item` needs some modifications based on this connection's state.
#
# @param item [Object] An item newly added to `items`
# @return [Edge]
def range_add_edge(item)
edge_class.new(item, self)
end

attr_writer :last
# @return [Integer, nil] A clamped `last` value. (The underlying instance variable doesn't have limits on it)
def last
Expand Down
15 changes: 10 additions & 5 deletions lib/graphql/relay/range_add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Relay
# should be ordered and paginated before providing it here.
#
# @example Adding a comment to list of comments
# post = Post.find(args[:postId])
# post = Post.find(args[:post_id])
# comments = post.comments
# new_comment = comments.build(body: args[:body])
# new_comment.save!
Expand All @@ -18,13 +18,13 @@ module Relay
# parent: post,
# collection: comments,
# item: new_comment,
# context: ctx,
# context: context,
# )
#
# response = {
# post: post,
# commentsConnection: range_add.connection,
# newCommentEdge: range_add.edge,
# comments_connection: range_add.connection,
# new_comment_edge: range_add.edge,
# }
class RangeAdd
attr_reader :edge, :connection, :parent
Expand All @@ -39,7 +39,12 @@ def initialize(collection:, item:, parent: nil, context: 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)
@edge = @connection.edge_class.new(item, @connection)
# 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
else
connection_class = BaseConnection.connection_for_nodes(collection)
@connection = connection_class.new(collection, {}, parent: parent, context: context)
Expand Down

0 comments on commit 16ceb91

Please sign in to comment.