Skip to content
Source
Sign inSign up
TanStack/query

Feature/query options #5153

MergedTkDodomerged 11 commits intoalpha fromrefs/pull/5153/headmerged by TkDodo View on GitHub

TkDodocommented

No description provided.

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

      1 Ignored Deployment
      NameStatusPreviewCommentsUpdated (UTC)
      query⬜️ Ignored (Inspect)Apr 30, 2023 4:31pm
    2. 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 07d6aa921c6c75458b21def05cee3dcd4af8331d:

      SandboxSource
      @tanstack/query-example-react-basic-typescriptConfiguration
      @tanstack/query-example-solid-basic-typescriptConfiguration
      @tanstack/query-example-svelte-basicConfiguration
      @tanstack/query-example-vue-basicConfiguration
    3. TkDodo reviewed

      packages/react-query/src/useQuery.ts lines 11 to 31
      @@ -9,0 +10,21 @@
      10+export function queryOptions<
      11+ TQueryFnData = unknown,
      12+ TError = unknown,
      13+ TData = TQueryFnData,
      14+ TQueryKey extends QueryKey = QueryKey,
      15+>(
      16+ options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
      17+): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
      18+
      19+export function queryOptions<
      20+ TQueryFnData = unknown,
      21+ TError = unknown,
      22+ TData = TQueryFnData,
      23+ TQueryKey extends QueryKey = QueryKey,
      24+>(
      25+ options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
      26+): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
      27+
      28+export function queryOptions(options: unknown) {
      29+ return options
      30+}

      should we move this to the core, @DamianOsipiuk ?

      The options are currently based on UseQueryOptions, which only exists in react-query. I think each lib has their own impl over QueryObserverOptions 🤔

      okay then let's let every framework export their own queryOptions ?

      Yes, i think that would be necessary.

    4. Codecov Report

      Patch coverage has no change and project coverage change: -0.23 :warning:

      Comparison is base (020645e) 90.83% compared to head (9c7123f) 90.60%.

      :exclamation: Current head 9c7123f differs from pull request most recent head 07d6aa9. Consider uploading reports for the commit 07d6aa9 to get more accurate results

      :mega: This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

      Additional details and impacted files
      @@            Coverage Diff             @@
      ##            alpha    #5153      +/-   ##
      ==========================================
      - Coverage   90.83%   90.60%   -0.23%     
      ==========================================
        Files          29       29              
        Lines         807      809       +2     
        Branches      274      274              
      ==========================================
        Hits          733      733              
      - Misses         70       72       +2     
        Partials        4        4              
      Impacted FilesCoverage Δ
      packages/react-query/src/useQuery.ts50.00% <0.00%> (-50.00%):arrow_down:

      :umbrella: View full report in Codecov by Sentry. :loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

    5. packages/react-query/src/useQuery.ts line 12
      @@ -7,3 +7,5 @@
      77 } from './types'
      88 import { useBaseQuery } from './useBaseQuery'
      99
      10+export function queryOptions<
      11+ TQueryFnData = unknown,

      Since this is query specific, we would need similar helper for infinite, mutation and queries?

      TkDodocommented

      probably not necessary for mutations, but for infinite: yes

      We don't even have the initialData overload for infiniteQuery 😅

      I would add a separate helper in a follow-up. This is not breaking so we can add it at any time I think

    6. ☁️ Nx Cloud Report

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

      📂 See all runs for this branch


      🟥 Failed Commands
      nx run-many --target=test:lib --parallel=5
      ✅ Successfully ran 4 targets

      Sent with 💌 from NxCloud.

    7. ☁️ Nx Cloud Report

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

      📂 See all runs for this branch


      ✅ Successfully ran 1 target

    8. Sent with 💌 from NxCloud.

  • from testing this, it seems like queryOptions needs to take the return type of select into account?

    eg

    
    export function fetchConfigOverridesQueryOptions(hostId: string, { suspense } = { suspense: false }) {
      return queryOptions({
        queryKey: hostQueryKeys.configOverrides(hostId),
        queryFn: () => Hosts.getConfigOverrides(hostId),
        suspense,
        select: mapOverridesToForm,
      });
    }

    where

    export function mapOverridesToForm({ data: { overrides } }: Successful<Hosts.HostConfigOverrideResult>): FormData {

    means that I get this error with a vendorized version of this PR:

    Type 'UndefinedInitialDataOptions<{ data: HostConfigOverrideResult; metadata?: Metadata | undefined; }, unknown, FormData, (string | readonly [...string[], "detail", string])[]>' is not assignable to type 'UseQueryOptionsForUseQueries<{ data: HostConfigOverrideResult; metadata?: Metadata | undefined; }, Error, { data: HostConfigOverrideResult; metadata?: Metadata | undefined; }, (string | readonly [......[]])[]>'.
      Type 'UndefinedInitialDataOptions<{ data: HostConfigOverrideResult; metadata?: Metadata | undefined; }, unknown, FormData, (string | readonly [...string[], "detail", string])[]>' is not assignable to type 'Omit<UseQueryOptions<{ data: HostConfigOverrideResult; metadata?: Metadata | undefined; }, Error, { data: HostConfigOverrideResult; metadata?: Metadata | undefined; }, (string | readonly [......[]])[]>, "placeholderData">'.
        Types of property 'refetchInterval' are incompatible.
          Type 'number | false | ((data: FormData | undefined, query: Query<{ data: HostConfigOverrideResult; metadata?: Metadata | undefined; }, unknown, { ...; }, (string | readonly [......[]])[]>) => number | false) | undefined' is not assignable to type 'number | false | ((data: { data: HostConfigOverrideResult; metadata?: Metadata | undefined; } | undefined, query: Query<{ data: HostConfigOverrideResult; metadata?: Metadata | undefined; }, Error, { ...; }, (string | readonly [......[]])[]>) => number | false) | undefined'.
            Type '(data: FormData | undefined, query: Query<{ data: HostConfigOverrideResult; metadata?: Metadata | undefined; }, unknown, { data: HostConfigOverrideResult; metadata?: Metadata | undefined; }, (string | readonly [......[]])[]>) => number | false' is not assignable to type 'number | false | ((data: { data: HostConfigOverrideResult; metadata?: Metadata | undefined; } | undefined, query: Query<{ data: HostConfigOverrideResult; metadata?: Metadata | undefined; }, Error, { ...; }, (string | readonly [......[]])[]>) => number | false) | undefined'.
              Type '(data: FormData | undefined, query: Query<{ data: HostConfigOverrideResult; metadata?: Metadata | undefined; }, unknown, { data: HostConfigOverrideResult; metadata?: Metadata | undefined; }, (string | readonly [......[]])[]>) => number | false' is not assignable to type '(data: { data: HostConfigOverrideResult; metadata?: Metadata | undefined; } | undefined, query: Query<{ data: HostConfigOverrideResult; metadata?: Metadata | undefined; }, Error, { ...; }, (string | readonly [......[]])[]>) => number | false'.
                Types of parameters 'data' and 'data' are incompatible.
                  Type '{ data: HostConfigOverrideResult; metadata?: Metadata | undefined; } | undefined' is not assignable to type 'FormData | undefined'.
                    Type '{ data: HostConfigOverrideResult; metadata?: Metadata | undefined; }' is missing the following properties from type 'FormData': lighthouse, listen, tun, loggi

    Though this is only when passed to useQueries!

  • if I pass the configs inline,

     const [{ data: savedData, isPending }, { data: featureFlags }] = useQueries({
        queries: [
          {
            queryKey: hostQueryKeys.configOverrides(hostId),
            queryFn: () => Hosts.getConfigOverrides(hostId),
            suspense: true,
            select: mapOverridesToForm,
          },
          {
            queryKey: orgQueryKeys.featureFlags(),
            suspense: true,
            queryFn: ({ signal }) => Org.getFeatureFlags({ signal }),
            // 10 minutes
            staleTime: 600000,
          },
        ],
      });

    they typecheck, except signal is untyped

  • TkDodo merged this0e3f15b

  • TkDodo closed this

  • is the resolution for "useQueries" with this PR still tbd?

    my issue got closed - idk whether there's plans to fix the useQueries types?

  • it's labelled as a known limitation

  • well, the second type error is covered by that, but the first problem in https://github.com/TanStack/query/pull/5153#issuecomment-1522517570 is that the output of the queryOptions function doesn't typecheck to useQueries. I'll wait to file an issue til I can try the prereleased version instead of my copy of this PR though.

  • Sign in to join this conversation.