Skip to content

Commit

Permalink
Merge pull request #3195 from rmosolgo/connection-wrapping-fix
Browse files Browse the repository at this point in the history
Update RangeAdd to use new connections when available
  • Loading branch information
Robert Mosolgo authored Oct 23, 2020
2 parents 97a0a06 + e7349e2 commit bb11c05
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
16 changes: 11 additions & 5 deletions lib/graphql/pagination/connections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,24 @@ def all_wrappers
all_wrappers
end

# Used by the runtime to wrap values in connection wrappers.
# @api Private
def wrap(field, parent, items, arguments, context, wrappers: all_wrappers)
return items if GraphQL::Execution::Interpreter::RawValue === items

def wrapper_for(items, wrappers: all_wrappers)
impl = nil

items.class.ancestors.each { |cls|
impl = wrappers[cls]
break if impl
}

impl
end

# Used by the runtime to wrap values in connection wrappers.
# @api Private
def wrap(field, parent, items, arguments, context, wrappers: all_wrappers)
return items if GraphQL::Execution::Interpreter::RawValue === items

impl = wrapper_for(items, wrappers: wrappers)

if impl.nil?
raise ImplementationMissingError, "Couldn't find a connection wrapper for #{items.class} during #{field.path} (#{items.inspect})"
end
Expand Down
19 changes: 14 additions & 5 deletions lib/graphql/relay/range_add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,21 @@ class RangeAdd
# @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)
# @param edge_class [Class] The class to wrap `item` with (defaults to the connection's edge class)
def initialize(collection:, item:, parent: nil, context: nil, edge_class: nil)
if context && context.schema.new_connections?
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)
else
connection_class = BaseConnection.connection_for_nodes(collection)
@connection = connection_class.new(collection, {}, parent: parent, context: context)
edge_class ||= Relay::Edge
@edge = edge_class.new(item, @connection)
end

@parent = parent
@connection = connection_class.new(collection, {}, parent: parent, context: context)
@edge = edge_class.new(item, @connection)
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion spec/integration/rails/graphql/relay/range_add_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def self.decode(encoded_text, nonce: false)
argument :menu_idx, Integer, required: true

field :item_edge, item.edge_type, null: false
field :items, item.connection_type, null: false, connection: false
# On the old runtime, connection: false was required here:
field :items, item.connection_type, null: false, connection: TESTING_INTERPRETER
field :menu, menu, null: false

define_method :resolve do |input|
Expand Down Expand Up @@ -82,6 +83,12 @@ def self.decode(encoded_text, nonce: false)
self.query(query)
self.mutation(mutation)
self.cursor_encoder(PassThroughEncoder)

if TESTING_INTERPRETER
use GraphQL::Execution::Interpreter
use GraphQL::Analysis::AST
use GraphQL::Pagination::Connections
end
end
}

Expand Down

0 comments on commit bb11c05

Please sign in to comment.