TanStack / query

🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.
https://tanstack.com/query
MIT License
40.1k stars 2.69k forks source link

feat(solid-query): Revise `queryOptions` implementation #7366

Closed ardeora closed 2 weeks ago

ardeora commented 2 weeks ago

While writing documentation for queryOptions in solid-query, I noticed the implementation of queryOptions API was incorrect.

In the current implementation. The queryOptions function would take in a function that would return an object of type QueryOptions. This is unnecessary and can be simplified by just taking in an object of type QueryOptions.

Current implementation:

import { queryOptions, createQuery, QueryClient } from '@tanstack/solid-query';

const client = new QueryClient();

const options = queryOptions(() => ({
  queryKey: ['key'],
  queryFn: async () => {
    return 'data';
  }
}));

const query = createQuery(options);
client.prefetchQuery(options());

There are a few issues with the current implementation:

  1. The goal of the queryOptions function is to provide typesafety for certain QueryClient methods. In this current approach, there is no type inference on methods like getQueryData, setQueryData, fetchQuery, etc.
const options = queryOptions(() => ({
  queryKey: ['number'],
  queryFn: () => Promise.resolve(5),
}))

const a = client.getQueryData(options().queryKey)
//    ^? const a: unknown
  1. Since these options will be used by the QueryClient methods, it would be better to have the options as an object instead of a function that returns an object.

Proposed implementation:

import { queryOptions, createQuery, QueryClient } from '@tanstack/solid-query';

const client = new QueryClient();

const options = queryOptions({
  queryKey: ['key'],
  queryFn: async () => {
    return 'data';
  }
});

const query = createQuery(() => options);
const a = client.getQueryData(options.queryKey);
//    ^? const a: string
vercel[bot] commented 2 weeks ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment | Name | Status | Preview | Comments | Updated (UTC) | | :--- | :----- | :------ | :------- | :------ | | **query** | ⬜️ Ignored ([Inspect](https://vercel.com/tanstack/query/CRPB7uFCa3gZqKPSBaLsgf9TMWFR)) | [Visit Preview](https://query-git-fork-ardeora-fix-solid-query-options-tanstack.vercel.app) | | May 2, 2024 5:44pm |
nx-cloud[bot] commented 2 weeks ago

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 228e7ef49ce2ac7e597678d7037f10521a969d5a. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target - [`nx affected --targets=test:format,test:sherif,test:knip,test:eslint,test:lib,test:types,test:build,build,test:attw --parallel=3`](https://cloud.nx.app/runs/EEriiSDiyr?utm_source=pull-request&utm_medium=comment)

Sent with 💌 from NxCloud.

codesandbox-ci[bot] commented 2 weeks ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 228e7ef49ce2ac7e597678d7037f10521a969d5a:

Sandbox Source
@tanstack/query-example-angular-basic Configuration
@tanstack/query-example-react-basic-typescript Configuration
@tanstack/query-example-solid-basic-typescript Configuration
@tanstack/query-example-svelte-basic Configuration
@tanstack/query-example-vue-basic Configuration