Issue
I'm working on some async code for subscriptions / web sockets and have the following code after some effort. This in itself is functional.
@type(name="Subscription")
class ContactsSubscription:
@subscription
async def company(self, info: Info, pk: str) -> AsyncGenerator[CompanyType, None]:
async for i in model_subscribe_publisher(info=info, pk=pk, model=Company):
yield i
The part where I'm struggling is discovering how I can rewrite certain aspects to shorten this code even more and remove the repetitiveness out of the code for each model declaration through my 40-odd models in the app.
My goals would be to condense this into something like:
@type(name="Subscription")
class ContactsSubscrption:
company: AsyncGenerator[CompanyType, None] = model_subscribe_publisher(Company)
I've seen this done in the same strawberry library, for queries:
@type(name="Query")
class ContactsQuery:
company: CompanyType = node()
I fail to understand the magic that makes this work. How could I accomplish this?
Solution
There is no "magic" to make this work - the syntax you are using is that of a class decorator.
As you don't post the code for your type
decorator, there is no way pointing what can be done - but it is a callable that gets the class, after ready, as a parameter: all one has to do is, inside the decorator call, to iterate over te class members and the class __annotations__
, find out which members it want to place as new or replace, instantiate the new objects and assign then back to their names.
(That said, as I commented above, type
is a poor choice for a class decorator name because it already has 3 different meanings in Python)
Or, if you don't want to change the class decorator, in your "desired" code, you just have to write a method that returns the model_subscribe_publisher
as you want it:
ex.:
def subscriber_factory(model):
async def subscriber_model_publisher(self, info, pk):
async for item in model_subscribe_publisher(info=info, pk=pk, model=model):
yield item
return subscriber_model_publisher
@type(name="Subscription")
class ContactsSubscrption:
company: AsyncGenerator[CompanyType, None] = subscriber_factory(Company)
Answered By - jsbueno
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.