Nuxt3 module for Apollo client
npm install @nuxt3/apollo-modulebash
npm i -D @nuxt3/apollo-module
`
Configuration
`js
// nuxt.config.js
import '@nuxt3/apollo-module' // import to remove config warning, not necessary
export default {
buildModules: [
'@nuxt3/apollo-module'
],
apollo: {
clientConfigs: {
default: {
// see https://www.apollographql.com/docs/react/api/core/ApolloClient/#ApolloClient.constructor
},
client1: {
// another client
},
client2: {
// authentication type
authenticationType: 'Bearer', // default 'Bearer'
}
},
// Cookie parameters used to store authentication token
cookieAttributes: {
/**
* Define when the cookie will be removed. Value can be a Number
* which will be interpreted as days from time of creation or a
* Date instance. If omitted, the cookie becomes a session cookie.
*/
expires: 7,
/**
* Define the path where the cookie is available. Defaults to '/'
*/
path: '/',
/**
* Define the domain where the cookie is available. Defaults to
* the domain of the page where the cookie was created.
*/
domain: 'example.com',
/**
* A Boolean indicating if the cookie transmission requires a
* secure protocol (https). Defaults to false.
*/
secure: false,
},
}
}
`
Usage
Query code(You can use@nuxt3/graphql-codegen-module to generate code)
`
import gql from 'graphql-tag'
import * as VueApolloComposable from '@vue/apollo-composable'
export const HelloDocument = gql
export function useHelloQuery() {
return VueApolloComposable.useQuery(HelloDocument, {}, {
// prefetch: false, // prefetch: false will fetch on client
})
}
`
Fetch in setup
`
`
Authentication
You have following methods for authentication available:
`js
// set your graphql-token
$apolloHelpers.onLogin(token / if not default you can pass in clientId as second argument, you can set custom cookies attributes object as the third argument, and you can skip reset store as the fourth argument /)
// unset your graphql-token
$apolloHelpers.onLogout(/ if not default you can pass in clientId as first argument, and you can skip reset store as the second argument /)
// get your current token (we persist token in a cookie)
$apolloHelpers.getToken(/ you can provide clientId /)
`
User login
`js
import { useLoginMutation } from '@/generated/operations' // generated by @nuxt3/graphql-codegen-module
const { mutate: login, onDone } = useLoginMutation({
})
const { $apolloHelpers } = useNuxtApp()
onDone((res) => {
const token = res.data.login.token // based on your resolver
$apolloHelpers.onLogin(token)
})
`
User logout
`js
// ~/components/my-component.js
const { $apolloHelpers } = useNuxtApp()
const logout = () => {
$apolloHelpers.onLogout()
}
`
Dev
`
pnpm i
`
`
pnpm run build
``