Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize retrieving a single object #46

Open
stickperson opened this issue Apr 23, 2020 · 4 comments
Open

Optimize retrieving a single object #46

stickperson opened this issue Apr 23, 2020 · 4 comments

Comments

@stickperson
Copy link

I'm following the graphene-django tutorial and got up to retrieving a single object. The README here does not mention how optimization would work here. gql_optimizer.query expects a queryset. As a workaround, I'm using:

      def resolve_category(self, info, **kwargs):
          id = kwargs.get('id')
          name = kwargs.get('name')

          if id is not None:
              return gql_optimizer.query(Category.objects.filter(pk=id), info).get()

          if name is not None:
              return gql_optimizer.query(Category.objects.filter(name=name), info).get()

          return None

Is this the recommended approach?

@mcabrams
Copy link

Interested to know the proposed solution here as well, this seems like it should work ok. Not sure if it could be supported, but obviously preferred developer experience would likely be:

gql_optimizer.query(Category.objects.get(name=name), info)

which currently raises 'Category' object has no attribute 'select_related'

@stickperson
Copy link
Author

That's because .get() returns an instance instead of a queryset. You can do gql_optimizer.query(Category.objects.select_related(...).get(name=name), info)

@mcabrams
Copy link

mcabrams commented Aug 5, 2020

@stickperson Yep, makes sense, I am using your workaround for now 👍

@tfoxy
Copy link
Owner

tfoxy commented Sep 8, 2020

Yes, the first post is the recommended approach. It's been a long time since I used Django, but from what I remember, when using .get you would lose the prefetch_related optimizations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants