|
| 1 | +from collections import namedtuple |
| 2 | + |
| 3 | +from django.db.models import ForeignKey |
| 4 | +from django.db.models.fields.reverse_related import ForeignObjectRel |
| 5 | +from graphene.utils.str_converters import to_snake_case |
| 6 | + |
| 7 | +from .registry import get_global_registry |
| 8 | +from .utils import get_related_model |
| 9 | + |
| 10 | +REGISTRY = get_global_registry() |
| 11 | +SELECT = 'select' |
| 12 | +PREFETCH = 'prefetch' |
| 13 | +RelatedSelection = namedtuple('RelatedSelection', ['name', 'fetch_type']) |
| 14 | + |
| 15 | + |
| 16 | +def model_fields_as_dict(model): |
| 17 | + return dict((f.name, f) for f in model._meta.get_fields()) |
| 18 | + |
| 19 | + |
| 20 | +def get_related_fetches_for_model(model, graphql_ast): |
| 21 | + model_fields = model_fields_as_dict(model) |
| 22 | + selections = graphql_ast.selection_set.selections |
| 23 | + |
| 24 | + graphene_obj_type = REGISTRY.get_type_for_model(model) |
| 25 | + optimizations = {} |
| 26 | + if graphene_obj_type and graphene_obj_type._meta.optimizations: |
| 27 | + optimizations = graphene_obj_type._meta.optimizations |
| 28 | + |
| 29 | + relateds = [] |
| 30 | + |
| 31 | + for selection in selections: |
| 32 | + selection_name = to_snake_case(selection.name.value) |
| 33 | + selection_field = model_fields.get(selection_name, None) |
| 34 | + |
| 35 | + try: |
| 36 | + related_model = get_related_model(selection_field) |
| 37 | + except: |
| 38 | + # This is not a ForeignKey or Relation, check manual optimizations |
| 39 | + manual_optimizations = optimizations.get(selection_name) |
| 40 | + if manual_optimizations: |
| 41 | + for manual_select in manual_optimizations.get(SELECT, []): |
| 42 | + relateds.append(RelatedSelection(manual_select, SELECT)) |
| 43 | + for manual_prefetch in manual_optimizations.get(PREFETCH, []): |
| 44 | + relateds.append(RelatedSelection(manual_prefetch, PREFETCH)) |
| 45 | + |
| 46 | + continue |
| 47 | + |
| 48 | + query_name = selection_field.name |
| 49 | + if isinstance(selection_field, ForeignObjectRel): |
| 50 | + query_name = selection_field.field.related_query_name() |
| 51 | + |
| 52 | + nested_relateds = get_related_fetches_for_model(related_model, selection) |
| 53 | + |
| 54 | + related_type = PREFETCH # default to prefetch, it's safer |
| 55 | + if isinstance(selection_field, ForeignKey): |
| 56 | + related_type = SELECT # we can only select for ForeignKeys |
| 57 | + |
| 58 | + if nested_relateds: |
| 59 | + for related in nested_relateds: |
| 60 | + full_name = '{0}__{1}'.format(query_name, related.name) |
| 61 | + |
| 62 | + nested_related_type = PREFETCH |
| 63 | + if related_type == SELECT and related.fetch_type == SELECT: |
| 64 | + nested_related_type = related_type |
| 65 | + |
| 66 | + relateds.append(RelatedSelection(full_name, nested_related_type)) |
| 67 | + else: |
| 68 | + relateds.append(RelatedSelection(query_name, related_type)) |
| 69 | + |
| 70 | + return relateds |
| 71 | + |
| 72 | + |
| 73 | +def optimize_queryset(model, queryset, graphql_ast): |
| 74 | + relateds = get_related_fetches_for_model(model, graphql_ast) |
| 75 | + |
| 76 | + for related in relateds: |
| 77 | + if related.fetch_type == SELECT: |
| 78 | + queryset = queryset.select_related(related.name) |
| 79 | + else: |
| 80 | + queryset = queryset.prefetch_related(related.name) |
| 81 | + |
| 82 | + return queryset |
0 commit comments