Hi guys, trying to set up wss with apollo and nextjs. using this set up:
import {
ApolloClient,
createHttpLink,
InMemoryCache,
split,
} from "@apollo/client";
import { GraphQLWsLink } from "@apollo/client/link/subscriptions";
import { createClient } from "graphql-ws";
import { setContext } from "@apollo/client/link/context";
import { getMainDefinition } from "@apollo/client/utilities";
const httpLink = createHttpLink({
uri: process.env.NEXT_PUBLIC_BACKEND_URL!,
// ...(isLive && { credentials: "include" }),
});
const wssLink = new GraphQLWsLink(
createClient({
url: "wss://uk.ws.8base.com/",
connectionParams: async () => {
const token = await checkAuth0Token();
return {
token,
workspaceId: process.env.NEXT_PUBLIC_8BASE_ENV,
environmentName: "staging",
};
},
webSocketImpl: require("websocket").w3cwebsocket,
})
);
export const checkAuth0Token = async () => {
if (typeof window !== "undefined") {
const res = await fetch("/api/session");
if (res.ok) {
const ses = await res.json();
return ses?.idToken || null;
}
}
};
const authLink = setContext(async (_, { headers }) => {
// get the authentication token from local storage if it exists
const token = await checkAuth0Token();
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
// ...(isLive && { credentials: "include" }),
},
};
});
const cache = new InMemoryCache();
const splitLink = split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === "OperationDefinition" &&
definition.operation === "subscription"
);
},
wssLink,
authLink.concat(httpLink)
);
const client = new ApolloClient({
link: splitLink,
cache,
credentials: "include",
});
I just get an ‘WebSocket connection to ‘wss://uk.ws.8base.com/’ failed: undefined’ with no error message. Any idea what this may be about?