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

Add async iterator for query results #1198

Merged
merged 12 commits into from
Dec 21, 2024
Merged

Conversation

NSeydoux
Copy link
Contributor

@NSeydoux NSeydoux commented Dec 18, 2024

This adds a helper function to make it easier to go through all of the paginated results. It allows syntax like the following:

  const pages = paginateQuery({ type: "SolidAccessGrant", status: "Active" },
    {
      fetch: session.fetch,
      queryEndpoint: new URL("https://vc.inrupt.com/query"),
    });
  let i = 0;
  for await (const page of pages) {
    console.log(`Page ${i} has ${page.items.length} items.`)
    i += 1;
  }

Unit tests aren't completely thorough, because technically they should check for all the error conditions, which are already checked in the underlying query function. I don't think it's worth the overhead.

@NSeydoux NSeydoux force-pushed the feat/SDK-3376/pagination-iterator branch from 3309850 to 46cc2e5 Compare December 18, 2024 13:31
Copy link
Contributor

@edwardsph edwardsph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice approach. Ideally we'd have tests that covered 0, 1, and 2 pages to account for different conditions in the generator

@NSeydoux
Copy link
Contributor Author

Yes, I'll add more unit tests for sure. And I'm not 100% sure the name of the function is descriptive of what it does, paginating results is what the server does technically, this goes through the pages, but I haven't found a more suitable name yet.

Base automatically changed from feat/SDK-3376/access-grant-queries to main December 19, 2024 16:28
@NSeydoux NSeydoux force-pushed the feat/SDK-3376/pagination-iterator branch from 0478b52 to 3f26cee Compare December 20, 2024 09:46
page = await query(page.next, options);
}
// Return the last page.
yield page;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You previously had return here and I looked up why. It not only yields the last page but marks the iterator as done so I thought you were right. Why do you think it should be yield?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I initially expected too, and that's why I had a return, but in tests that didn't work as I expected. If I understood correctly, the iterator has a return function to mark it done, but here the function is a generator, which returns said iterator, hence the difference. None of the generator examples in the MDN docs use return, and here is an example in the Node CLI:

> function* test() {
... yield 1;
... yield 2;
... yield 3;
... return 4;
... }
undefined
> const i = test()
undefined
> Array.from(i)
[ 1, 2, 3 ]

@NSeydoux NSeydoux merged commit f074a59 into main Dec 21, 2024
26 checks passed
@NSeydoux NSeydoux deleted the feat/SDK-3376/pagination-iterator branch December 21, 2024 21:10
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

Successfully merging this pull request may close these issues.

3 participants