1

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

  • Show your `initialize` method of `Connections::MySubclassConnection`. – user1934428 May 28 '21 at 06:51
  • I don't have an initializer defined for `Connections::MySubclassConeection`, since I assumed it's inheriting from `Schema::Object`. Referencing these docs: https://graphql-ruby.org/api-doc/1.12.8/GraphQL/Types/Relay/BaseConnection.html – Christopher Reece May 28 '21 at 07:02

2 Answers2

0

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.

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • Thank you for the suggestion, but I think I figured the issue out. I was trying to initialize a "Type" subclass which should be the return type for the resolver. I didn't need to change inheritance for the Connections::MySubclassConnection which was originally inheriting from GraphQL::Pagination::Connection. It works just fine now when using the Types::BaseConnection subclass as the field return type. – Christopher Reece May 28 '21 at 07:45
0

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
  • I still don't get how you managed to fix it... Can you add more details please? – Al17 Aug 14 '23 at 15:08
  • PS nevermind, figured it out: I had to nest my custom connection class from `GraphQL::Pagination::Connection` rather than from `BaseConnection` (which in its turn is nested from `GraphQL::Types::Relay::BaseConnection`). So current setup is: `Connections::MySubclassConnection < GraphQL::Pagination::Connection`. I also had to make sure there's a `def nodes` method in custom connection. – Al17 Aug 14 '23 at 16:43