-
Notifications
You must be signed in to change notification settings - Fork 227
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
Conversation
There was a problem hiding this 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.
How would you use the length field to be returned along with the query results? |
@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? |
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. |
@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:
|
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. |
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. |
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).
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.