tfoxy / graphene-django-optimizer

Optimize database access inside graphene queries
MIT License
427 stars 86 forks source link

Add support for model_field also a callable #51

Closed nicam closed 4 years ago

nicam commented 4 years ago

Hi There!

Would it be possible to have model_field also use _normalize_hint_value so we could use a lambda function?

We have the problem that we need to define the hints at runtime since they are different based on language. Using the annotation does only work with lambdas as they get executed during the request.

Thanks for your help!

tfoxy commented 4 years ago

Hi!

Let me know if you still need this to be possible. If so, can you provide some example on how would this be used?

tfoxy commented 4 years ago

From what I understand, you want something like the following:

    @gql_optimizer.resolver_hints(
        model_field=lambda info: 'product',
    )
    def resolve_product(root, info):
        return root.product

But instead of returning 'product' in the lambda, you want to use info. Is that right?

nicam commented 4 years ago

actually just beeing able to use a lambda function, instead of a string, for model_field would already be enough :)

As far as I have seen you'd only need to wrap it with _normalize_hint_value internally

nicam commented 4 years ago

Concretly I'd like to do the following:

@gql_optimizer.resolver_hints(
    model_field = lambda info:  f'image_{translation.get_language()}',
)

The field name needs to be evaluated in the lambda at runtime since it differs by language so I can prefetch the correct model field

tfoxy commented 4 years ago

Understood! Yeah, from what I can see it should be a simple change. Let me check what I can do. Please ping me if I don't answer you by Sunday.

tfoxy commented 4 years ago

Fixed in v0.7.0! (commit 3da91421c6de7c2f96749d706a62f84f8787d742)

nicam commented 4 years ago

Thanks a lot!