Skip to content

Adding iterable and length to SQLAlchemyConnectionField #36

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

Merged
merged 2 commits into from
Apr 6, 2017

Conversation

yfilali
Copy link
Contributor

@yfilali yfilali commented Feb 24, 2017

It is currently difficult to resolve a total_count field with the SQLAlchemy graphene library.

The example from swapi has a nice little implementation that does not work here since unlike Django, the SQLAlchemyConnectionField does not set neither length nor iterable on the connection returned.

This PR brings length and iterable to SQLAlchemyConnectionField to match DjangoConnectionField.

@coveralls
Copy link

coveralls commented Feb 24, 2017

Coverage Status

Coverage increased (+0.1%) to 91.093% when pulling d17c284 on yfilali:master into 2de70b4 on graphql-python:master.

Copy link

@richmondwang richmondwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was really needed.

@syrusakbary syrusakbary merged commit 030e5e1 into graphql-python:master Apr 6, 2017
@sebastiandev
Copy link

How would you use the length field to be returned along with the query results?
If I just add length on the query, It fails saying the Connection class doesn't have a length field.

@wichert
Copy link
Contributor

wichert commented Jul 12, 2018

@yfilali Is there any example of how to use this? You are storing this on the connection field instance now, but that does not make it visible anywhere. Extending PageInfo with a new field and populating that is still needed somewhere I suspect?

@yfilali
Copy link
Contributor Author

yfilali commented Jul 13, 2018

I haven't tested this recently, but the original example should still stand: swapi

The official docs show how to add an extra field to a connection subclass: http://docs.graphene-python.org/en/latest/relay/connection/, in that example, you would just add a resolve_extra method there that returns self.length.

@wichert
Copy link
Contributor

wichert commented Jul 13, 2018

@yfilali I guess the part I'm missing is how to use that Connection subclass in combination with SQLAlchemyObjectType. Using the swapi example I tried this:

class LengthConnection(graphene.Connection):
    class Meta:
        abstract = True

    total_count = graphene.Int()

    def resolve_total_count(self, info):
        return self.length


class Article(SQLAlchemyObjectType):
    class Meta:
        model = model.Article
        interfaces = (relay.Node, )
        connection_class = LengthConnection

but that immediately aborts with a TypeError:

  File "src/curvetips/api/admin/graphql/article.py", line 38, in <module>
    class Article(SQLAlchemyObjectType):
  File "lib/python3.6/site-packages/graphene/utils/subclass_with_meta.py", line 48, in __init_subclass__
    super_class.__init_subclass_with_meta__(**options)
  File "lib/python3.6/site-packages/graphene_sqlalchemy/types.py", line 131, in __init_subclass_with_meta__
    super(SQLAlchemyObjectType, cls).__init_subclass_with_meta__(_meta=_meta, interfaces=interfaces, **options)
  File "lib/python3.6/site-packages/graphene/types/objecttype.py", line 61, in __init_subclass_with_meta__
    super(ObjectType, cls).__init_subclass_with_meta__(_meta=_meta, **options)
TypeError: __init_subclass_with_meta__() got an unexpected keyword argument 'connection_class'

@wichert
Copy link
Contributor

wichert commented Jul 13, 2018

The graphene documentation for a custom connection makes you think this should work:

class Article(SQLAlchemyObjectType):
    class Meta:
        model = model.Article
        interfaces = (relay.Node, )


class LengthConnection(graphene.Connection):
    class Meta:
        node = Article

    total_count = graphene.Int()

    def resolve_total_count(self, info):
        return self.length


class Query(graphene.ObjectType):
    articles = SQLAlchemyConnectionField(ArticleConnection)

but that fails because ArticleConnection is not a subtype of ArticleConnection.

@wichert
Copy link
Contributor

wichert commented Jul 13, 2018

I think I am on to something. DjangoObjectType. init_subclass_with_meta has an extra connection_class parameter, and uses that as connection type (if provided) instead of dynamically creating a new connection class. There is no equivalent for that in SQLAlchemyObjectType.

kigen pushed a commit to kigen/graphene-sqlalchemy that referenced this pull request Dec 9, 2018
This builds on graphql-python#36, and adds support
for specifying a custom connection class to SQLAlchemyObjectType. The
implementation is mostly a copy & paste from [the identical feature in
DjangoObjectType](https://github.com/graphql-python/graphene-django/blob/master/graphene_django/types.py#L72).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants