From 7d63c0c9940612da3e4d5e79674e9e6ff7e0caa4 Mon Sep 17 00:00:00 2001 From: Brodie Davis Date: Tue, 10 Jun 2025 11:29:41 -0400 Subject: [PATCH 1/2] add support for custom connectionParams in websocket initializer --- src/runtime/plugin.ts | 17 +++++++++++++++-- src/types.d.ts | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts index 9263dbc1..522daab9 100644 --- a/src/runtime/plugin.ts +++ b/src/runtime/plugin.ts @@ -6,6 +6,7 @@ import { ApolloClients, provideApolloClients } from '@vue/apollo-composable' import { ApolloClient, ApolloLink, createHttpLink, InMemoryCache, split } from '@apollo/client/core' import { GraphQLWsLink } from '@apollo/client/link/subscriptions' import { setContext } from '@apollo/client/link/context' +import type { ClientOptions } from 'graphql-ws' import type { ClientConfig, ErrorResponse } from '../types' import createRestartableClient from './ws' import { useApollo } from './composables' @@ -76,11 +77,23 @@ export default defineNuxtPlugin((nuxtApp) => { ...clientConfig.wsLinkOptions, url: clientConfig.wsEndpoint, connectionParams: async () => { + let connectionParams: ClientOptions['connectionParams'] = clientConfig.wsLinkOptions?.connectionParams || {} + + // if connection params is a function, call it + if (typeof connectionParams === 'function') { + connectionParams = await connectionParams() + } + const auth = await getAuth() + if (!auth) { return connectionParams } - if (!auth) { return } + const headers = connectionParams?.headers || {} - return { headers: { [clientConfig.authHeader!]: auth } } + // merge any existing connection params with the auth header + return { + headers: { [clientConfig.authHeader!]: auth, ...headers }, + ...connectionParams + } } }) diff --git a/src/types.d.ts b/src/types.d.ts index f9d41f72..7b106922 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -29,7 +29,7 @@ export type ClientConfig = { * Provide additional configuration for the `GraphQLWsLink`. * See https://github.com/enisdenjo/graphql-ws/blob/master/docs/interfaces/client.ClientOptions.md **/ - wsLinkOptions?: Omit; + wsLinkOptions?: Omit; /** * Specify a websocket endpoint to be used for subscriptions. From 1454ed1f0974f416450324aebbd270bcf138adeb Mon Sep 17 00:00:00 2001 From: Brodie Davis Date: Tue, 10 Jun 2025 13:02:54 -0400 Subject: [PATCH 2/2] fix merge order of objects --- src/runtime/plugin.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts index 522daab9..aae0f6f6 100644 --- a/src/runtime/plugin.ts +++ b/src/runtime/plugin.ts @@ -87,13 +87,14 @@ export default defineNuxtPlugin((nuxtApp) => { const auth = await getAuth() if (!auth) { return connectionParams } - const headers = connectionParams?.headers || {} - - // merge any existing connection params with the auth header - return { - headers: { [clientConfig.authHeader!]: auth, ...headers }, - ...connectionParams + // merge connection headers with the auth header + const headers = { + [clientConfig.authHeader!]: auth, + ...(connectionParams?.headers || {}) } + + // merge existing connection params with headers + return { ...connectionParams, headers } } })