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.

Optimize pagination of relay connection fields

See original GitHub issue

Copied from https://github.com/tfoxy/graphene-django-optimizer/issues/1#issuecomment-419340157.

@tfoxy Do you use optimizers with any pagination? We’re also using Apollo as the client and we want to provide cursor-based pagination. But we also have queries with nested connections e.g.:

{
  products(first: 10) {
    edges {
      node {
        name
        variants(first: 5) {
          edges {
            node {
              name
            }
          }
        }
      }
    }
  }
}

Using just the ORM I’m able to fetch the data only with two DB queries:

Product.objects.prefetch_related('variants')

I’m trying to achieve the same in the API but unfortunately any pagination seems to break the prefetches as they do slicing on querysets internally which results in refetching objects that were already cached by prefetch_related.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
maarcingebalacommented, Nov 21, 2018

@nwaxiomatic Here is an example usage of optimization hints in my project. There is a Product model and each product can have multiple images (1-N relationship). From these images, we choose one as a product thumbnail to display in the UI.

Now, if we had a query that fetches first 5 products and a thumbnail for each of them:

{
  products(first: 5) {
    edges {
      node {
        name
        thumbnailUrl
      }
    }
  }
}
  • Without optimization hints For each product, Django would do a query to fetch all its images and return one as a thumbnail.

  • With optimization hints @gql_optimizer.resolver_hints(prefetch_related='images') tells Graphene to prefetch images relation when the resolve_thumbnail_url resolver is used. As a result, each time the thumbnail field is used, I’m able to take advantage of prefetched images and avoid duplicated database queries.

1reaction
maarcingebalacommented, Sep 22, 2018

@tfoxy Yes it does, but I still can benefit from prefetching data in one query instead of doing duplicated queries for each row of data.

Anyway, I managed to use prefetching by using optimization hints and the slightly modified version of DjangoConnectionField, as suggested in this comment. I’ll keep experimenting with it. For now, I’d consider my question resolved, so you can close the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Relay Connections | Caliban
The field can be paginated forwards by using first (number of items) and after (the current cursor), or backwards by using last (number...
Read more >
Pagination - Relay
By default, Relay will automatically append new items to the connection upon completing a pagination request, and will make them available to your...
Read more >
Enforce pagination in graphene relay.ConnectionField
ConnectionField implementation that ships with graphene does not by default paginate queries. (The connection can optionally be paginated using ...
Read more >
Windowed pagination with Relay - aamir.j
Relay -style pagination is a popular option for paginating graphql queries. ... Connection types must have fields named edges and pageInfo .
Read more >
Step by step guide to pagination in GraphQL - Daily.dev
We see that in cursor-based pagination, the pagination is done based on the fields of the record in the database. It is more...
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