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?