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.

Paginating over subfield

See original GitHub issue

Summary

Hi, I’m coming from Relay where it’s super easy to paginate over subfields. However, I’m not managing to achieve this with apollo, can somebody please give me a heads up?

Problem

When the component loads, the query works fine, then when fetchMore is called, all data from the paginating field vanish. In the example below, bar would have results on initial load, then after fetchMore it becomes []. What I’m doing wrong?

Code

query

export const MY_QUERY = gql`
  query foo($pk: uuid!, $offset: Int = 0) {
    foo_by_pk(pk: $pk) {
      bar(limit: 20, offset: $offset) @connection(key: "foo_bar") {
        ...bar_data
      }
    }
  }
  ${Bar_Fragment.data}
`

field policy

const client = new ApolloClient({
  uri: URL,
  cache: new InMemoryCache({
    typePolicies: {
      Query: {
        fields: {
          bar: offsetLimitPagination(),
        },
      },
    },
  }),
})

fetch more

const onEndReached = () => {
  fetchMore({
    variables: {
      offset: data.foo.bar.length,
    },
  })
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
benjamncommented, Oct 9, 2020

Great to hear! Since that was the problem, I believe offsetLimitPagination can be improved so that it defaults args.offset to zero. I’ll fix that shortly.

1reaction
PedroBerncommented, Oct 9, 2020

@benjamn thanks it did help! But a new issue came up with your solution:

I have 2 screens:

Screen 1

Simple feed pagination that was working before the solution:

  query FooFeed($offset: Int = 0) {
    foo(limit: 20, offset: $offset,){
      ...FooItem_data
      bar(limit: 1) {
        ...BarItem_data
      }
    }
  }

Screen 2

Details screens that paginate over bar for a given foo. Now this one is working as expected thanks to you. Before your solution, I was needing to query bar with a where constraint.

  query FooDetail($pk: uuid!, $offset: Int = 0) {
    foo_by_pk(pk: $pk) {
      bar(limit: 20, offset: $offset) {
        ...BarItem_data
      }
    }
  }

Type Policies

export const cache: InMemoryCache = new InMemoryCache({
  typePolicies: {
    foo: {
      fields: {
        bar: offsetLimitPagination(), // used in Screen 2 -> Details
      },
    },
    Query: {
      fields: {
        foo: offsetLimitPagination(), // used in Screen 1 -> Feed
      },
    },
  },
})

Question

@benjamn before your solution, the Screen 1 was working and Screen 2 broken. Now it’s the opposite. The bar field of Screen 1 is always empty. What am I doing wrong here? Thanks for the help

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pagination with sub field in mongodb - Stack Overflow
Getting sub-fields are can be done with 'dot notation'. Pagination is simply using query.limit() and query.skip() .
Read more >
300: Physical Description (Network Development and MARC ...
When the statement of pagination and of illustrative matter are combined, they are both recorded in a single subfield $a.
Read more >
Tutorial: Pagination - Apollo GraphQL Blog
In Parts 6 and 7 we went over how to add subscriptions to the server and hot-load changes to the server in your...
Read more >
11. 300 ‡a: Extent for multi-parts with Examples - Yale Library
On CIP, with a blank 300 field, and only one volume in hand, a multipart monograph ... these are not recorded as plates...
Read more >
Pagination - Hot Chocolate - ChilliCream GraphQL Platform
If we need more control over the pagination process we can do so, by returning a ... if it has been specified as...
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