diff --git a/guides/relay/range_add.md b/guides/relay/range_add.md new file mode 100644 index 0000000000..fd81f922e8 --- /dev/null +++ b/guides/relay/range_add.md @@ -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. diff --git a/guides/schema/sdl.md b/guides/schema/sdl.md index 25149cd2c5..db2884cf9b 100644 --- a/guides/schema/sdl.md +++ b/guides/schema/sdl.md @@ -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: { diff --git a/lib/graphql/pagination/connection.rb b/lib/graphql/pagination/connection.rb index 7619878ff9..95a5ee7a31 100644 --- a/lib/graphql/pagination/connection.rb +++ b/lib/graphql/pagination/connection.rb @@ -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 diff --git a/lib/graphql/relay/range_add.rb b/lib/graphql/relay/range_add.rb index 9d6d521820..f04ae90ffa 100644 --- a/lib/graphql/relay/range_add.rb +++ b/lib/graphql/relay/range_add.rb @@ -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! @@ -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 @@ -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)