From 20f7a294f7fc928d764254f23d55c48ee34d11ed Mon Sep 17 00:00:00 2001 From: Kyle Tse Date: Mon, 11 Jul 2022 04:03:34 +0800 Subject: [PATCH] apollo uri support relative path with SSR support. --- package.json | 3 ++- src/index.ts | 15 ++++++++++++--- src/plugin.ts | 10 +++++++--- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 6c06806..c6b4bcd 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/index.ts b/src/index.ts index 98c3afe..9a703fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, @@ -14,6 +15,7 @@ type ClientConfig = Partial> & { } export interface ApolloModuleOptions { [name: string]: ClientConfig | any // <= 0.0.9 + serverUri?: string default?: ClientConfig // <= 0.0.9 clientConfigs?: { // > 0.0.9 default: ClientConfig @@ -27,7 +29,7 @@ export interface ApolloModuleOptions { } } export default defineNuxtModule({ - + meta: { name: '@nuxt3/apollo-module', configKey: 'apollo', @@ -38,13 +40,20 @@ export default defineNuxtModule({ 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' @@ -60,4 +69,4 @@ declare module '@nuxt/schema' { interface NuxtOptions { apollo?: ApolloModuleOptions } -} \ No newline at end of file +} diff --git a/src/plugin.ts b/src/plugin.ts index 20e3fc0..6b43d69 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -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 @@ -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), @@ -91,7 +95,7 @@ export default defineNuxtPlugin((nuxt: NuxtApp) => { })) apolloClients[clientId] = apolloClient; } - + } const apolloHelpers = { @@ -107,7 +111,7 @@ export default defineNuxtPlugin((nuxt: NuxtApp) => { } writeClientCookie(getTokenName(clientId), token, cookieAttributes) - + if (!skipResetStore) { try { await apolloClients[clientId].resetStore() @@ -145,4 +149,4 @@ declare module '#app' { [key: string]: ApolloClient } } -} \ No newline at end of file +}