Skip to content

apollo uri support relative path with SSR support. #5

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
},
"devDependencies": {
"@nuxt/schema": "npm:@nuxt/schema-edge@3.0.0-27338323.1e98259",
"unbuild": "0.5.11"
"unbuild": "0.5.11",
"urijs": "^1.19.11"
}
}
15 changes: 12 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { dirname, resolve } from "pathe";
import { fileURLToPath } from "url";
import { defineNuxtModule, addTemplate, addPluginTemplate } from '@nuxt/kit'
import URI from 'urijs'

import type {
InMemoryCache,
Expand All @@ -14,6 +15,7 @@ type ClientConfig = Partial<ApolloClientOptions<any>> & {
}
export interface ApolloModuleOptions {
[name: string]: ClientConfig | any // <= 0.0.9
serverUri?: string
default?: ClientConfig // <= 0.0.9
clientConfigs?: { // > 0.0.9
default: ClientConfig
Expand All @@ -27,7 +29,7 @@ export interface ApolloModuleOptions {
}
}
export default defineNuxtModule<ApolloModuleOptions>({

meta: {
name: '@nuxt3/apollo-module',
configKey: 'apollo',
Expand All @@ -38,13 +40,20 @@ export default defineNuxtModule<ApolloModuleOptions>({

const __dirname__ = dirname(fileURLToPath(import.meta.url));

const server = nuxt.options.server
options.serverUri = options.serverUri ?? new URI({
protocol: server.https ? 'https' : 'http',
hostname: server.host,
port: server.port,
}).toString()

// save options to apollo.options.mjs
addTemplate({
filename: 'apollo.options.mjs',
getContents: () => `export default ${JSON.stringify(options)}`,
})

// add apollo plugin ( see plugin.ts ) to server and client
// add apollo plugin ( see plugin.ts ) to server and client
addPluginTemplate({
src: resolve(__dirname__, "./plugin.mjs"),
mode: 'all'
Expand All @@ -60,4 +69,4 @@ declare module '@nuxt/schema' {
interface NuxtOptions {
apollo?: ApolloModuleOptions
}
}
}
10 changes: 7 additions & 3 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ApolloClients, provideApolloClient } from '@vue/apollo-composable'
import { setContext } from '@apollo/client/link/context';
import { parse, serialize } from "cookie-es";
import { ApolloModuleOptions } from './index'
import URI from 'urijs'
// @ts-expect-error #build resolved by Nuxt3
import apolloOptions from '#build/apollo.options.mjs' // generated by index.ts

Expand Down Expand Up @@ -71,6 +72,9 @@ export default defineNuxtPlugin((nuxt: NuxtApp) => {
const httpLink = createHttpLink(options)
const cache = new InMemoryCache();
if (process.server) {
if (process.server) {
if (new URI(options.uri).is('relative') && apolloModuleOptions.serverUri)
options.uri = new URI(options.uri).absoluteTo(apolloModuleOptions.serverUri).toString()
const apolloClient = new ApolloClient(Object.assign(options, {
ssrMode: true,
link: concat(authLink, httpLink),
Expand All @@ -91,7 +95,7 @@ export default defineNuxtPlugin((nuxt: NuxtApp) => {
}))
apolloClients[clientId] = apolloClient;
}

}

const apolloHelpers = {
Expand All @@ -107,7 +111,7 @@ export default defineNuxtPlugin((nuxt: NuxtApp) => {
}

writeClientCookie(getTokenName(clientId), token, cookieAttributes)

if (!skipResetStore) {
try {
await apolloClients[clientId].resetStore()
Expand Down Expand Up @@ -145,4 +149,4 @@ declare module '#app' {
[key: string]: ApolloClient<any>
}
}
}
}