tfoxy / graphene-django-optimizer

Optimize database access inside graphene queries
MIT License
428 stars 84 forks source link

Optimizing model_fields across relationship boundaries #60

Open qeternity opened 3 years ago

qeternity commented 3 years ago

It doesn't seem possible from a cursory glance of the code, and I haven't spent enough time to see if it's an easy patch so I figured I'd ask the experts here first.

Is it possible to optimize across model relationship boundaries, for instance let's presume we wanted to make some things available at the top level resolver to avoid lots of nesting (the pseudo example below is not a particularly good one, but the question still stands):

class OrganizationType:
    def resolve_parent_organization(root, info):
        return root.parent_organization
    def resolve_accounts(root, info):
        return root.accounts.all()

class UserType:
@gql_optimizer.resolver_hints(model_field="organization__parent_organization")
    def resolve_parent_organization(root, info):
        return root.organization.parent_organization

and then we'd like to be able to optimize queries according to a pattern such as this:

user {
    parentOrganization {
        accounts
    }
}

The hacky approach we're taking at the moment is to prefetch downstream data when we access the parentOrganization resolver because this is ultimately better than N+1 queries but it still results in lots of overfetching. For instance, let's say that instead of accounts we only want the parentOrganization name.

olidacombe commented 3 years ago

I've been working o functionality to this end, see https://github.com/tfoxy/graphene-django-optimizer/compare/master...olidacombe:deep_model_field_hint