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

feat(svelte-query): Svelte Query Adapter for TanStack Query #4768

Merged
merged 91 commits into from Jan 8, 2023
Merged

feat(svelte-query): Svelte Query Adapter for TanStack Query #4768

merged 91 commits into from Jan 8, 2023

Conversation

lachlancollins
Copy link
Member

@lachlancollins lachlancollins commented Jan 5, 2023

Overview

This PR is a continuation of #4344. It adds an official Svelte adapter which is based on the brilliant work of @drejohnson and @amen-souissi.

I'll be actively editing this description as progress is made. I'd also greatly appreciate any and all contributions over at https://github.com/lachlancollins/query.

API

Based on the discussion at the old PR, the functions which export a store now have the create prefix to be less React hooks-like. Additionally, the <QueryClientProvider> and <Hydrate> components have been added to be more consistent with the other adapters.

  • createQuery
  • createQueries
  • createInfiniteQuery
  • createMutation
  • useQueryClient
  • useIsFetching
  • useIsMutating
  • useHydrate
  • <QueryClientProvider>
  • <Hydrate>

Package

svelte-package outputs all required files to a (customisable) /build folder, which contains everything that needs to be published to npm. I'm fairly sure this differs from how the other packages are published, so let me know how I can ensure this integrates with the CI scripts properly.

To-do

If you would like to help with one of these tasks, please comment here or create an issue on the fork :)

  • Docs overview
  • Build using svelte-package (allows using .svelte files)
  • Implement QueryClientProvider component
  • Implement Hydrate component
  • Rename store functions with create prefix
  • Update examples to SvelteKit 1.0
  • Add playground and star-wars examples
  • Use vitest instead of jest (setup easier for vite environment)
  • Add createQuery and createMutation tests
  • Integrate into publish scripts

@TkDodo
Copy link
Collaborator

TkDodo commented Jan 7, 2023

we're getting there. The preview sandbox shows the following error:

node_modules/esbuild/lib/main.js:1336:21: ERROR: [plugin: vite:dep-scan] Failed to resolve entry for package "@tanstack/svelte-query". The package may have incorrect main/module/exports specified in its package.json.
node_modules/esbuild/lib/main.js:1336:21: ERROR: [plugin: vite:dep-scan] Failed to resolve entry for package "@tanstack/svelte-query". The package may have incorrect main/module/exports specified in its package.json.

I think the package.json is not correct, have a look at how other packages have entry points specified:

"types": "build/lib/index.d.ts",
"main": "build/lib/index.js",
"module": "build/lib/index.esm.js",
"exports": {
".": {
"types": "./build/lib/index.d.ts",
"import": "./build/lib/index.mjs",
"default": "./build/lib/index.js"
},
"./package.json": "./package.json"
},
"sideEffects": false,

I'm not sure if svelte-kit produces the output in the same branches, but it would be good if we could customize it like that.

for example, for our size testing, we check all file matching packages/*/build/umd/*.production.js

@TkDodo
Copy link
Collaborator

TkDodo commented Jan 7, 2023

and prettier needs to run on some files :)

@lachlancollins
Copy link
Member Author

we're getting there. The preview sandbox shows the following error:

node_modules/esbuild/lib/main.js:1336:21: ERROR: [plugin: vite:dep-scan] Failed to resolve entry for package "@tanstack/svelte-query". The package may have incorrect main/module/exports specified in its package.json.
node_modules/esbuild/lib/main.js:1336:21: ERROR: [plugin: vite:dep-scan] Failed to resolve entry for package "@tanstack/svelte-query". The package may have incorrect main/module/exports specified in its package.json.

I think the package.json is not correct, have a look at how other packages have entry points specified:

...

I'm not sure if svelte-kit produces the output in the same branches, but it would be good if we could customize it like that.

for example, for our size testing, we check all file matching packages/*/build/umd/*.production.js

Hi @TkDodo, I've made a bit of progress here.

By default, svelte-package generates its own package.json inside the build dir which autogenerates all the main/module/exports fields. However, it seems like this wouldn't integrate too easily into the CI. So, I've copied the important fields from the build package.json into the main package.json, and set up rimraf to delete the build package.json.

The next "issue" is that svelte-package only builds ESM. From what I can tell, there isn't really any need for CJS or UMD in the Svelte ecosystem (anymore?) - at least nobody has been complaining about the output of svelte-package. There are definitely some Svelte packages I've used in the past which refused to work in SSR that were built through Rollup.

@TkDodo
Copy link
Collaborator

TkDodo commented Jan 7, 2023

okay, if ESM is all we need, then so be it 😅

@lachlancollins
Copy link
Member Author

lachlancollins commented Jan 8, 2023

@TkDodo I think this should pass github CI now

@codecov-commenter
Copy link

codecov-commenter commented Jan 8, 2023

Codecov Report

Base: 96.36% // Head: 91.05% // Decreases project coverage by -5.31% ⚠️

Coverage data is based on head (ef81d26) compared to base (eab6e2c).
Patch has no changes to coverable lines.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4768      +/-   ##
==========================================
- Coverage   96.36%   91.05%   -5.32%     
==========================================
  Files          45      102      +57     
  Lines        2281     3866    +1585     
  Branches      640      999     +359     
==========================================
+ Hits         2198     3520    +1322     
- Misses         80      325     +245     
- Partials        3       21      +18     
Impacted Files Coverage Δ
src/react/reactBatchedUpdates.ts
src/react/useInfiniteQuery.ts
src/core/query.ts
src/react/setLogger.ts
src/react/utils.ts
src/react/setBatchUpdatesFn.ts
src/core/queryClient.ts
src/core/infiniteQueryBehavior.ts
src/core/queryObserver.ts
src/react/QueryErrorResetBoundary.tsx
... and 137 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@TkDodo TkDodo merged commit b324a9b into TanStack:main Jan 8, 2023
TkDodo added a commit that referenced this pull request Jan 25, 2023
* feat(svelte-query): Svelte Query Adapter for TanStack Query (#4768)

* feat: svelte-query adapter

* add examples

* add @testing-library/svelte and svelte-jester

* add transform for testing svelte components

* re-export @tanstack/core core

* add a few initial test

* delete tests in examples

* use ^ in dependencies

* delete and gitignore .vscode

* rename basic-typescript to basic

* add basic example to ci.json

* remove transform from global preset

* update version

* don't gitignore lib directory in svelte examples

* fix build files location in package.json

* chore: prettier formatting

* add missing state

* chore: fix eslint errors

* add context

* unsubscribe

* add missing export

* use svelte context

* update version

* update examples to use context

* release: v4.14.5

* remove onMount

* add onMount to setQueryClient

* remove unneeded exports

* Add basic svelte docs overview

* Add SvelteKit 1.0 example

Uses the data from the simple example

* Edit nodeResolve settings

* More rollup and babel tweaks

Seems to be working

* Try svelte plugin options

* Separate out svelte config

* Switch to svelte-package

* Reset rollup config

* Output to ./build

* Use vitest in svelte-query

* Fix test imports

* Avoid transpiling TS during typecheck

* Fix vitest command

* More vitest migration work

* Minor fixes to tests

* Rename file to types.d.ts

* Replace setQueryContext with QueryClientProvider

* Replace tabs with spaces for repo consistency

* Update examples to sveltekit 1.0

* Implement Hydrate component

* Add playground example

Rewrite of the react example

* Start adding star-wars example

* Add films and film pages

* Finish star-wars example

* Rename store functions with create prefix

* Add correct favicons

* Reorder some files in svelte-query

* Undo rollup.config.ts changes

* Add new createQuery and createMutation tests

* More descriptive test name

* Misc fixes from feedback

- Max TS 4.7.4
- Move root package.json dependencies
- Use object syntax
- Use test:jest script (for now)
- Remove sveltekit autogenerated files

* Fix pnpm-lock

* Specify svelte-package source, update TS to 4.7.4

* Remove unnecessary packages in examples

* Sync pnpm-lock

* Reset pnpm-lock to upstream

* Run pnpm install

* Remove svelte-query from root tsconfg

Needs to be run from its own svelte-check package... sorry

* Run prettier, remove unused import

* Run prettier on svelte files

Requires prettier-plugin-svelte to run

* Prettier for changed files

* Fix cipublish from svelte-query

* rimraf to remove build/lib/package.json

* Run svelte-kit sync before vitest

* Add files field to package.json

* Add types field to package.json

* Bump svelte-query version to 4.20.0

In case this fixes the CI issue

* Add path alias to vitest

* Update vitest to 0.26

Changes module resolution?

* More CI improvements

* Remove --parallel from pnpm scripts

* Remove unused dependency

* Add eslint settings

Thanks @rivo420

* Add coverage report

Co-authored-by: DeAndre Johnson <dre@phreshr.com>
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>

RELEASE_ALL

* chore: Remove incompatible vitest flag from test:ci (#4777)

* Remove coverage flag

Add back again in a simpler script setup

* Allow coverage to run on test:ci

* release: v4.21.0

* feat: export default hydration methods for easier extension in `dehydrateOptions` (#4751)

* export default hydration metods to allow for easier extending

* pretty

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>

* release: v4.22.0

* docs(svelte-query): Add installation and examples (#4781)

* chore: Restructure package scripts (#4779)

* Use rimraf for clean script

Supported on windows

* Restructure jest tests, types test, and building types

* Relative eslint and jest paths are not needed

Now works on windows too, and tested to make sure it doesn't break linux

* Run codemod tests from main jest.config.ts

--collectCoverage false breaks when passed in to the union of these

* Remove unused test:lib scripts

* Add prettier-plugin-svelte

* Call test:types in github workflow

* Add missing build:types to query-async-storage-persister

* Simplify build script

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>

* docs(svelte-query): Add SSR and SvelteKit guide (#4786)

* Add SSR docs

* Remove semicolons to match project style

* docs: fix sveltekit SSR link (#4792)

* fix(svelte-query): Remove dependency on SvelteKit (#4788)

* Use rimraf for clean script

Supported on windows

* Restructure jest tests, types test, and building types

* Relative eslint and jest paths are not needed

Now works on windows too, and tested to make sure it doesn't break linux

* Run codemod tests from main jest.config.ts

--collectCoverage false breaks when passed in to the union of these

* Remove unused test:lib scripts

* Add prettier-plugin-svelte

* Call test:types in github workflow

* Add missing build:types to query-async-storage-persister

* Build without sveltekit

* Update vite-plugin-svelte to 2.0

* Add dev/watch script to svelte-query

* Replace sveltePreprocess with vitePreprocess

* Fix example component and indentation

* Remove dev/watch

Breaks setup because it adds package.json that isn't removed by rimraf

* Move /src/lib to /src, ignore __tests__ in build

* Fix tsconfig included files

* Replace tabs with spaces

* Whoops messed up merge

* Add more tsconfig.json settings

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>

* release: v4.22.1

* refactor(svelte-query): Clean-up type definitions (#4800)

* Simplify types

* Run prettier

* Add reactivity docs, rename example

* This function does not need to be reactive

* release: v4.22.2

* docs(svelte-query): Expand SSR docs (#4809)

* docs(svelte-query): Expand SSR docs

* Expand wording

* Run prettier

* docs: Update devtools.md with note that mutations are not tracked (#4810)

This can save others some time browsing around trying to get it to work

* docs: clarify interaction of query filter predicates with other criteria (#4532)

* docs: clarify interaction of query filter predicates with other criteria

* Update docs/guides/filters.md

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>

* Update docs/guides/filters.md

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>

* docs(svelte-query): Rework SvelteKit setup (#4811)

* Add some default options

* Rewrite hydration example

* Run prettier

* Rename example to SSR

* Add recommended SvelteKit setup

* Rework infinite scroll in sveltekit

* Add SvelteKit note to overview

* Switch basic example to sveltekit

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>

* chore: Add sandbox.config.json (#4812)

* docs: fix typo (quey -> query) (#4813)

* docs(svelte-query): Add recommended defaults to prefetchQuery setup (#4815)

* Add recommended defaults to prefetch example

* Move SSR docs up in sidebar

* docs: update tkdodos blog (#4820)

* docs: clarify the documentation on running examples (#4818)

* docs: clarify the documentation on running examples

* docs: use the proper watch command in docs for running examples

* chore: fix duplicated example package name (#4823)

* test: stabilize various tests (#4825)

* test: stabilize various tests

* chore: bump istanbul version

* docs(useMutation): clarify `mutationFn` option default (#4837)

* docs(useMutation): clarify `mutationFn` option default

* Make optionality consistent with `useQuery` documentation

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>

* fix(svelte-query): Fix createMutation for functions that take no arguments (#4847)

* Update createMutation test function name

* Default TVariables = void

* Fix createMutation test

* Fix prettier

* release: v4.22.3

* fix(core): do not call mutate callbacks if mutation started after unmount (#4848)

* test: add mutation callback after unmount test

* test: make test more resilient

* fix(core): do not call mutate callbacks if mutation started after unmount

* test: adapt tests to what we have in v5

- one test has been removed (because setState was removed entirely)
- the second test has been re-written to not use internals anymore, and it works in v4 as well

* fix(core): do not call mutate callbacks if mutation started after unmount

by making sure the callbacks are only invoked if we have an active listener

* chore: prettier again

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>

* release: v4.22.4

* docs(useMutation): correct docs for mutate function callbacks (#4601)

* docs(useMutation): correct docs for mutate function callbacks

* docs: fix merge conflicts

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>

* docs: add QueryClient import (#4856)

* docs: add readme.md to react adapter

* Fix createQuery

* Finish svelte-query changes

* Run prettier

* Fix initial merge

* Fix examples

* Fix prettier again

Co-authored-by: Tanner Linsley <tannerlinsley@users.noreply.github.com>
Co-authored-by: Manthan Mallikarjun <nahtnam@gmail.com>
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
Co-authored-by: ottomated <31470743+ottomated@users.noreply.github.com>
Co-authored-by: Joseph Markus <joseph.markus@outlook.com>
Co-authored-by: Ben Longo <11063448+benlongo@users.noreply.github.com>
Co-authored-by: Masaki Koyanagi <mascii@gmail.com>
Co-authored-by: Michal Tecza <zorza2@gmail.com>
Co-authored-by: Louis Young <me@louisyoung.co.uk>
Co-authored-by: Jan <39303600+janlat@users.noreply.github.com>
Co-authored-by: Qz <38932402+QzCurious@users.noreply.github.com>
Co-authored-by: Joël Kuijper <31251240+Joehoel@users.noreply.github.com>
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.

None yet

6 participants