Prefetch_related in root resolver not preserved in children resolvers
See original GitHub issueIt 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:
- Created 6 years ago
- Reactions:2
- Comments:11 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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
And thanks @japrogramer for helping figure out the problem. Made fixing it very simple 😄