Closed nuraeil closed 3 months ago
CC: @danielroe
I've been stuck on trying to figure out why it does work for useQuery() but not for useLazyAsyncQuery() / useAsyncQuery() all morning, glad I came across your post!
I didnt figure out it was due to the bundler and made a quick wrapper to keep TS-support for now;
composables/useTypedLazyAsyncQuery.ts;
import type { DocumentNode } from "graphql";
export function useTypedLazyAsyncQuery<T>(
query: DocumentNode
): Ref<T | undefined> {
const { data } = await useLazyAsyncQuery<T>(query);
return computed(() => data.value as T);
}
usage:
import type { GetShopsPreviewResult } from "~/types/generated/index"
import { GET_SHOPS_PREVIEW } from '~/graphql/queries/shops/getShopsPreview';
import { useTypedLazyAsyncQuery } from '@/composables/useTypedLazyAsyncQuery';
const data = await useTypedLazyAsyncQuery<GetShopsPreviewResult>(GET_SHOPS_PREVIEW);
Environment
Darwin
v20.7.0
3.10.1
3.10.0
2.8.1
pnpm@8.7.6
-
ssr
,devServer
,vite
,devtools
,experimental
,future
,alias
,modules
,cookieControl
,primevue
,nuxtTypedRouter
,authJs
,security
,delayHydration
,build
,postcss
,runtimeConfig
,nitro
,app
,sourcemap
,typescript
,bugsnag
,apollo
,colorMode
,tailwindcss
,i18n
@formkit/nuxt@1.5.7
,nuxt-icon@0.6.8
,@nuxtjs/tailwindcss@6.11.3
,@nuxtjs/color-mode@3.3.2
,@nuxtjs/i18n@8.1.0
,@nuxtjs/apollo@5.0.0-alpha.11
,@hebilicious/authjs-nuxt@0.3.5
,nuxt-delay-hydration@1.3.3
,nuxt-typed-router@3.5.1
,nuxt-bugsnag@7.2.3
,nuxt-security@1.1.1
,@pinia/nuxt@0.5.1
,@pinia-plugin-persistedstate/nuxt@1.2.0
,@vueuse/nuxt@10.7.2
,@dargmuesli/nuxt-cookie-control@7.2.5
,@nuxt/devtools@1.0.8
,nuxt-primevue@0.2.2
,nuxt-build-cache@0.1.1
-
Describe the bug
With the switch in Nuxt version 3.10 to defaulting to the
bundler
resolution, this plugins type inference fully breaks. When usingstrict
, the build process stops and throws the following error:This is what is displayed in VS Code when hovering over the variable for the destructured data.
Expected behaviour
When turning
bundler
resolution off, everything builds and type inference works.nuxt.config.ts
Reproduction
https://stackblitz.com/edit/github-ohfhqq
Additional context
No response
Logs
No response