Skip to content

Subclassing Connection in 2.0 #65

Closed
@nmrtv

Description

@nmrtv

Hi, in 2.0dev version I can't see the way to subclass relay.Connection. Because SQLAlchemyObjectType doesn't have a Connection attribute, the only way I see is to pass connection via the Meta class. Otherwise the connection is automatically created with default Connection class. But I can't specify the subclassed connection in Meta, because it needs a node attribute and I get circular reference. E. g.

class UserConnection(graphene.relay.Connection):
    class Meta:
        node = User

class User(SQLAlchemyObjectType):
    class Meta:
        model = UserModel
        interfaces = (relay.Node, )
        connection = UserConnection

So I don't understand what's the point of connection parameter in SQLAlchemyObjectType Meta class? To make it work, I have changed SQLAlchemyObjectType.__init_subclass_with_meta__ and introduced connection_type parameter. Now I can make an abstract subclassed Connection and pass it to Meta:

class SQLAlchemyObjectType(ObjectType):
    @classmethod
    def __init_subclass_with_meta__(cls, model=None, registry=None, skip_registry=False,
                                    only_fields=(), exclude_fields=(), connection=None, connection_type=None,
                                    use_connection=None, interfaces=(), id=None, **options):

        ...

        if use_connection and not connection:
            # We create the connection automatically
            connection = (connection_type or Connection).create_type('{}Connection'.format(cls.__name__), node=cls) 

        ...
class UserConnection(graphene.relay.Connection):
    class Meta:
        abstract = True


class User(SQLAlchemyObjectType):
    class Meta:
        model = UserModel
        interfaces = (relay.Node, )
        connection_type = UserConnection

Maybe I don't understand something and there is an easier way to make it work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions