For some reason the "new" method is protected on a subclass of Graphql-ruby's BaseConnection

442 views Asked by At

The error I'm getting is:

NoMethodError Exception: protected method `new' called for Connections::MySubclassConnection:Class

I've implemented a BaseConnection < GraphQL::Types::Relay::BaseConnection in base_connection.rb and then created a subclass class Connections::MySubclassConnection < Types::BaseConnection in my_subclass_connection.rb.

This error is being raised in my root query in the connection field:

field :my_subclass_connection, ...
def my_subclass_connection
 Connections::MySubclassConnection.new( <--- error is being raised here
      Connections::MySubclassItems.new(
        ...
      ),
    )
end

2

There are 2 answers

1
user1934428 On

Add to your Connections::MySubclassConeection something like

def initialize
  super
end

But before you do this, check the docs of Types::BaseConnection whether they expect you to do something particular in your constructor before invoking the one of the base class.

2
Christopher Reece On

Types::BaseConnection should be used as the return type for the resolver. Not as a replacement for GraphQL::Pagination::Connection. You can make a custom BaseConnection to add extra fields to your Connection.

field :my_subclass_connection, ...
def my_subclass_connection
 Connections::MySubclassConnection.new( <--- This class was a "Types" subclass which should be used to define the return type for the resolver. 
      Connections::MySubclassItems.new(
        ...
      ),
    )
end