Skip to content

Subclassing Connection in 2.0 #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nmrtv opened this issue Aug 6, 2017 · 4 comments
Closed

Subclassing Connection in 2.0 #65

nmrtv opened this issue Aug 6, 2017 · 4 comments

Comments

@nmrtv
Copy link

nmrtv commented Aug 6, 2017

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?

@richmondwang
Copy link

richmondwang commented Sep 12, 2017

I am also having trouble with this as I am adding custom specification for my connections.
This "feature" would be nice and (very) helpful to have.


For now, my workaround is to overwrite graphene.relay.Connection with my subclassed MyConnection.

import graphene.relay

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


graphene.relay.Connection = MyConnection

@geertjanvdk
Copy link

@neriusmika Your fix is great and this is a fix that was done for the Django-version as well. Best would be to rename connection_type to connection_class.

@nikordaris
Copy link

I just submitted #120 which should fix this issue

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related topics referencing this issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants