You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As a first time user to the repo, I had a quick look at the issues and there's a lot about Nuxt 3. As a fellow Nuxt 3 user, it would be helpful to have a nuxt/SSR guide.
The text was updated successfully, but these errors were encountered:
On top of the plugin configuration (as @Siilwyn demonstrated above), I've also integrated villus with Nuxt's useAsyncData. This enables Villus to work with Nuxt's prerendering and prefetching of linked pages since the data will be embedded into Nuxt's page payload. On top of that, it also enables the use of the transform function.
@logaretm feel free to copy and adjust the code if you want to add to the docs.
Usage Example
constquery=graphql(` query Comments { user { post { comments { id name } } } }`);const{data: comments}=awaituseAPI({
query,transform: (data)=>{returndata?.user?.post?.comments??[];},});
useAPI Code
importtype{AsyncDataOptions}from"#app";importtype{KeysOf}from"#app/composables/asyncData";import{getQueryKey,typeQueryCompositeOptions,typeQueryVariables,useQuery,}from"villus";typeUseAPIOptions<TData,TVars,TRes>=Omit<QueryCompositeOptions<TData,TVars>,"fetchOnMount">&Pick<AsyncDataOptions<TData|null,TRes,KeysOf<TRes>,null>,"transform">;/** * A composable function to fetch data from a GraphQL API. * It integrates the Villus GraphQL client with Nuxt’s `useAsyncData`. * @param opts */exportdefaultfunctionuseAPI<TData=unknown,TVars=QueryVariables,TRes=TData,>(opts: UseAPIOptions<TData,TVars,TRes>){const{ execute }=useQuery({
...opts,fetchOnMount: false,});constcacheKey=getQueryKey({query: toValue(opts.query),variables: toValue(opts.variables),});returnuseAsyncData(String(cacheKey),async()=>{constresult=awaitexecute();returnresult.data;},{transform: opts.transform,},);}
As a first time user to the repo, I had a quick look at the issues and there's a lot about Nuxt 3. As a fellow Nuxt 3 user, it would be helpful to have a nuxt/SSR guide.
The text was updated successfully, but these errors were encountered: