question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prefetch_related in root resolver not preserved in children resolvers

See original GitHub issue

It seems that prefetch_related is not preserved in children resolvers. (select_related is no problem).

Example

This example is a simplified version of the code I am working with.

Models

class Family(models.Model):
    name = models.CharField(max_length=255)


class Person(models.Model):
    name = models.CharField(max_length=256)
    family = models.ForeignKey('Family', related_name='members')

Nodes

class PersonNode(DjangoObjectType):
    class Meta:
        model = Person
        interfaces = (graphene.relay.Node,)


class FamilyNode(DjangoObjectType):
    members = graphene.ConnectionField(PersonNode)

    class Meta:
        model = Family
        interfaces = (graphene.relay.Node,)

“Endpoint”

class Query(graphene.ObjectType):
    families = graphene.ConnectionField(Family)

    def resolve_families(self, args, context, info):
        qs = Family.objects.prefetch_related('members')
        return qs

Query

{
  families {
    edges {
      node {
        members {
          edges {
            node {
              name
            }
          }
        }
      }
    }
  }
}

Problem

The family members were prefetched in the root resolver but that is definitely not used in children resolvers as a db query is made for person data each time a family is resolved.

Question

Does anyone have an idea how to get prefetch_related used in children resolvers?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jkimbocommented, Jul 1, 2019

@japrogramer @hgylfason @jonbesga I think I’ve found the issue that is meaning that prefetch_related is not preserved. It’s a bug and this PR should fix it: https://github.com/graphql-python/graphene-django/pull/693

0reactions
jkimbocommented, Jul 1, 2019

And thanks @japrogramer for helping figure out the problem. Made fixing it very simple 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to prefetch parents of a child on mptt tree with django ...
If I write query like below instead using get_ancestors() method, database queries stays low. Product.objects.filter(active=True).
Read more >
How to use Angular resolvers to prefetch beers to the party
1- Create an interface for mapping the API response. · 2- Create the beer service, to get the data and provide a subscription...
Read more >
Many small queries are efficient in SQLite | Hacker News
I've been applying all those tips and tricks, but it didn't improve performance that much, but I have lost data. Sqlite is full...
Read more >
Tastypie Documentation
Not everyone's needs are the same, so Tastypie goes out of its way to ... to add a resource as a child of...
Read more >
Django Documentation - Read the Docs
6.18 django.core.urlresolvers utilityfunctions . ... If a child class does not declare its own Meta class, it will inherit the parent's Meta ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found