Skip to content
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

Static general method for handing root objects in subscription events #30

Open
techknowfile opened this issue Aug 5, 2020 · 0 comments

Comments

@techknowfile
Copy link

I have a graphene subscription object type defined with the following resolver (resolve_area_by_dc). I need to create multiple subscription resolvers for different model types (this one is for AreaModel). The only thing that needs to be different between the resolvers are the model types -- as such, I'm trying to make my code as DRY as possible by having a single resolver function that parameterizes the model type.

Unfortunately, when I try to do so, it doesn't work, and so I'm probably not understanding how the callbacks actually work..

This is what I'm trying:

    area_by_dc = graphene.Field(AreaSubscriptionPayload, dc=graphene.String())

    @staticmethod
    def resolver(model=None, root=None, info=None, dc=None):
        return root.filter(
            lambda event:
            isinstance(event.instance, model) and
            dc == event.distribution_center
        ).map(lambda event: {"dc": event.distribution_center, "mutation": event.operation, "data": event.instance})

    def resolve_area_by_dc(*args, **kwargs):
        return DistributionCenterSubscriptions.resolver(AreaModel, *args, *kwargs)

If I replace resolve_area_by_dc with this, it works:

    def resolve_area_by_dc(root, info, dc=None):
 return root.filter(
            lambda event:
            isinstance(event.instance, AreaModel) and
            dc == event.distribution_center
        ).map(lambda event: {"dc": event.distribution_center, "mutation": event.operation, "data": event.instance})
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

No branches or pull requests

1 participant