zenstack icon indicating copy to clipboard operation
zenstack copied to clipboard

feat: create prefetch functions for Tanstack Query plugin

Open Arkanii opened this issue 1 year ago • 4 comments

Hello!

I need help with this one. 😓 I'm trying to run this on Svelte but prefetch queries need to be executed on the server so I can't use the getHooksContext function and I can't find any solution for it yet.

I also need to test on Vue and React. If anyone can test it that would be great as I don't have much time to test on all three frameworks. 🙏🏻

Thanks !

Svelte error: lifecycle_outside_component
2024-09-18T16:10:51.139896568Z `getContext(...)` can only be used during component initialisation
2024-09-18T16:10:51.139903231Z     at Module.lifecycle_outside_component (/srv/app/node_modules/svelte/src/internal/shared/errors.js:28:17)
2024-09-18T16:10:51.139909643Z     at get_or_init_context_map (/srv/app/node_modules/svelte/src/internal/server/context.js:51:5)
2024-09-18T16:10:51.139915794Z     at Module.getContext (/srv/app/node_modules/svelte/src/internal/server/context.js:15:22)
2024-09-18T16:10:51.139921936Z     at Module.getHooksContext (/srv/app/node_modules/@zenstackhq/src/runtime-v5/svelte.ts:48:35)
2024-09-18T16:10:51.139963213Z     at Module.usePrefetchFindManyTodo (/srv/app/src/lib/hooks/todo.ts:65:33)
2024-09-18T16:10:51.139967551Z     at load (/srv/app/src/routes/(website)/(home)/+page.ts:7:8)
2024-09-18T16:10:51.139971478Z     at async Module.load_data (/srv/app/node_modules/@sveltejs/kit/src/runtime/server/page/load_data.js:192:17)
2024-09-18T16:10:51.139975546Z     at async eval (/srv/app/node_modules/@sveltejs/kit/src/runtime/server/page/index.js:168:13)

Arkanii avatar Sep 18 '24 16:09 Arkanii

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces significant enhancements to the tanstack-query plugin, particularly focusing on prefetching capabilities. Key changes include the addition of a generatePrefetch option in various functions, updates to function signatures, and the introduction of new functions for prefetching and fetching queries across different frameworks (React, Vue, Svelte). The test suite has also been updated to validate these new functionalities, ensuring that the plugin can effectively handle prefetching operations.

Changes

File Path Change Summary
packages/plugins/tanstack-query/src/generator.ts - Added generatePrefetch option to various functions.
- Updated generate, generateQueryHook, and makeQueryArgsType function signatures.
- Introduced makePrefetchQueryOptions function.
packages/plugins/tanstack-query/src/runtime-v5/vue.ts - Added new types and functions for query handling.
- Defined MaybeRefDeep type.
- Introduced prefetchModelQuery, fetchModelQuery, prefetchInfiniteModelQuery, and fetchInfiniteModelQuery functions.
packages/plugins/tanstack-query/src/runtime/index.ts - Added DEFAULT_QUERY_ENDPOINT and ExtraPrefetchOptions exports.
packages/plugins/tanstack-query/src/runtime/common.ts - Introduced ExtraPrefetchOptions type for prefetching options.
packages/plugins/tanstack-query/tests/plugin.test.ts - Updated test suite to validate prefetch functionality across frameworks.
- Added makePrefetchSource function for generating prefetch source objects.

Possibly related PRs

  • #1480: This PR modifies the generateQueryHook function in generator.ts, which is directly related to the changes made in the main PR regarding the generateQueryHook function and its parameters, including the handling of options for different versions.
  • #1775: Although this PR focuses on optimistic updates, it modifies the common.ts file, which is related to the overall query handling in the tanstack-query plugin, potentially impacting how queries are processed, including those involving prefetching.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Sep 18 '24 16:09 coderabbitai[bot]

Hey @Arkanii , thanks for making this PR. I'll follow up here a bit later.

ymc9 avatar Sep 20 '24 23:09 ymc9

Hello!

I need help with this one. 😓 I'm trying to run this on Svelte but prefetch queries need to be executed on the server so I can't use the getHooksContext function and I can't find any solution for it yet.

I also need to test on Vue and React. If anyone can test it that would be great as I don't have much time to test on all three frameworks. 🙏🏻

Thanks !

Svelte error: lifecycle_outside_component
2024-09-18T16:10:51.139896568Z `getContext(...)` can only be used during component initialisation
2024-09-18T16:10:51.139903231Z     at Module.lifecycle_outside_component (/srv/app/node_modules/svelte/src/internal/shared/errors.js:28:17)
2024-09-18T16:10:51.139909643Z     at get_or_init_context_map (/srv/app/node_modules/svelte/src/internal/server/context.js:51:5)
2024-09-18T16:10:51.139915794Z     at Module.getContext (/srv/app/node_modules/svelte/src/internal/server/context.js:15:22)
2024-09-18T16:10:51.139921936Z     at Module.getHooksContext (/srv/app/node_modules/@zenstackhq/src/runtime-v5/svelte.ts:48:35)
2024-09-18T16:10:51.139963213Z     at Module.usePrefetchFindManyTodo (/srv/app/src/lib/hooks/todo.ts:65:33)
2024-09-18T16:10:51.139967551Z     at load (/srv/app/src/routes/(website)/(home)/+page.ts:7:8)
2024-09-18T16:10:51.139971478Z     at async Module.load_data (/srv/app/node_modules/@sveltejs/kit/src/runtime/server/page/load_data.js:192:17)
2024-09-18T16:10:51.139975546Z     at async eval (/srv/app/node_modules/@sveltejs/kit/src/runtime/server/page/index.js:168:13)

Hi @Arkanii , my apologies for the late response. Just to clarify your goal here. Do you want to do server-side prefetch with SvelteKit? If so, maybe we should simply generate a set of presetFindManyModel etc. async functions (that wraps queryClient.prefetchQuery inside), instead of generating usePrefetchQuery?

ymc9 avatar Sep 30 '24 22:09 ymc9

Hello! I need help with this one. 😓 I'm trying to run this on Svelte but prefetch queries need to be executed on the server so I can't use the getHooksContext function and I can't find any solution for it yet. I also need to test on Vue and React. If anyone can test it that would be great as I don't have much time to test on all three frameworks. 🙏🏻 Thanks !

Svelte error: lifecycle_outside_component
2024-09-18T16:10:51.139896568Z `getContext(...)` can only be used during component initialisation
2024-09-18T16:10:51.139903231Z     at Module.lifecycle_outside_component (/srv/app/node_modules/svelte/src/internal/shared/errors.js:28:17)
2024-09-18T16:10:51.139909643Z     at get_or_init_context_map (/srv/app/node_modules/svelte/src/internal/server/context.js:51:5)
2024-09-18T16:10:51.139915794Z     at Module.getContext (/srv/app/node_modules/svelte/src/internal/server/context.js:15:22)
2024-09-18T16:10:51.139921936Z     at Module.getHooksContext (/srv/app/node_modules/@zenstackhq/src/runtime-v5/svelte.ts:48:35)
2024-09-18T16:10:51.139963213Z     at Module.usePrefetchFindManyTodo (/srv/app/src/lib/hooks/todo.ts:65:33)
2024-09-18T16:10:51.139967551Z     at load (/srv/app/src/routes/(website)/(home)/+page.ts:7:8)
2024-09-18T16:10:51.139971478Z     at async Module.load_data (/srv/app/node_modules/@sveltejs/kit/src/runtime/server/page/load_data.js:192:17)
2024-09-18T16:10:51.139975546Z     at async eval (/srv/app/node_modules/@sveltejs/kit/src/runtime/server/page/index.js:168:13)

Hi @Arkanii , my apologies for the late response. Just to clarify your goal here. Do you want to do server-side prefetch with SvelteKit? If so, maybe we should simply generate a set of presetFindManyModel etc. async functions (that wraps queryClient.prefetchQuery inside), instead of generating usePrefetchQuery?

Hello ! No problem. 😄 Yes, my goal here is to do server-side prefetch with SvelteKit to reduce loading times, and to do it for other frameworks too if it can helps someone.

I'll try to check your idea a lit later. If you or someone else want to edit my PR, no problem !

Arkanii avatar Oct 02 '24 12:10 Arkanii

Mmmh I think it's too complicated for me to end this... Do you have any tip for me ? thanks

Arkanii avatar Oct 11 '24 13:10 Arkanii

Mmmh I think it's too complicated for me to end this... Do you have any tip for me ? thanks

No worries @Arkanii , I'll follow up on this PR!

ymc9 avatar Oct 11 '24 18:10 ymc9

Hi @Arkanii , I've made some progress to change the PR to generate prefetch[Action][Model] and fetch[Action][Model] instead, which is probably more versatile than usePrefetch.

However, I believe more though and work is needed about how data is actually fetched from the server side. It's complicated and not performant if the hooks still send an HTTP request. It should probably just directly call enhanced prisma client.

May I keep the PR open for now and continue thinking about this? I really appreciate that you started this effort. It's been a long-waited feature!

ymc9 avatar Oct 16 '24 16:10 ymc9

Hi @Arkanii , I've made some progress to change the PR to generate prefetch[Action][Model] and fetch[Action][Model] instead, which is probably more versatile than usePrefetch.

However, I believe more though and work is needed about how data is actually fetched from the server side. It's complicated and not performant if the hooks still send an HTTP request. It should probably just directly call enhanced prisma client.

May I keep the PR open for now and continue thinking about this? I really appreciate that you started this effort. It's been a long-waited feature!

Sure no problem ! I'll just put it in draft. Thanks to you to accept to work my PR. 🙏🏻

Arkanii avatar Oct 17 '24 13:10 Arkanii

ops sorry @ymc9 I closed the PR...

Arkanii avatar Nov 05 '24 20:11 Arkanii