-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
Add a guide to the documentation for Nuxt 3 Support #153
Comments
It's pretty straightforward I think unless I'm missing something? In Nuxt's plugins directory create a file, e.g. import { createClient } from 'villus';
export default defineNuxtPlugin(({ vueApp }) => {
const client = createClient({
url: 'https://graphql.datocms.com/',
});
vueApp.use(client)
}); Then in your pages you can use |
On top of the plugin configuration (as @Siilwyn demonstrated above), I've also integrated villus with Nuxt's @logaretm feel free to copy and adjust the code if you want to add to the docs. Usage Example const query = graphql(`
query Comments {
user {
post {
comments {
id
name
}
}
}
}
`);
const { data: comments } = await useAPI({
query,
transform: (data) => {
return data?.user?.post?.comments ?? [];
},
});
import type { AsyncDataOptions } from "#app";
import type { KeysOf } from "#app/composables/asyncData";
import {
getQueryKey,
type QueryCompositeOptions,
type QueryVariables,
useQuery,
} from "villus";
type UseAPIOptions<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
*/
export default function useAPI<
TData = unknown,
TVars = QueryVariables,
TRes = TData,
>(opts: UseAPIOptions<TData, TVars, TRes>) {
const { execute } = useQuery({
...opts,
fetchOnMount: false,
});
const cacheKey = getQueryKey({
query: toValue(opts.query),
variables: toValue(opts.variables),
});
return useAsyncData(
String(cacheKey),
async () => {
const result = await execute();
return result.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: