Skip to content
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

The WS connection failed, I'm not sure where the configuration issue is #338

Open
wuzikai18 opened this issue Aug 1, 2024 · 1 comment

Comments

@wuzikai18
Copy link

wuzikai18 commented Aug 1, 2024

image

ApolloWrapper.tsx
"use client"
import { HttpLink, split } from "@apollo/client"
import { GraphQLWsLink } from "@apollo/client/link/subscriptions"
import { getMainDefinition } from "@apollo/client/utilities"
import { ApolloClient, ApolloNextAppProvider, InMemoryCache } from "@apollo/experimental-nextjs-app-support"
import { createClient } from "graphql-ws"

// Helper function to create a client
function makeClient() {
  const httpLink = new HttpLink({
    uri: "https://zion-app.functorz.com/zero/y7xXo5wAY9o/api/graphql-v2",
    fetchOptions: { cache: "no-store" },
    headers: {
      Authorization:
        "xxx",
    },
  })

  const wsClient = createClient({
    url: "wss://zion-app.functorz.com/zero/y7xXo5wAY9o/api/graphql-subscription",
    connectionParams: {
      Authorization:
        "xxx",
    },
    on: {
      connected: () => console.log("WebSocket connected"),
      closed: (event) => console.log("WebSocket closed", event),
      error: (error) => console.log("WebSocket error", error),
    },
  })

  const wsLink = new GraphQLWsLink(wsClient)

  const splitLink = split(
    ({ query }) => {
      const definition = getMainDefinition(query)
      return definition.kind === "OperationDefinition" && definition.operation === "subscription"
    },
    //@ts-ignore
    wsLink,
    httpLink
  )

  return new ApolloClient({
    ssrMode: true,
    cache: new InMemoryCache(),
    link: splitLink,
  })
}

// Component to wrap your app in
export function ApolloWrapper({ children }: React.PropsWithChildren) {
  return <ApolloNextAppProvider makeClient={makeClient}>{children}</ApolloNextAppProvider>
}

'use client';
import { gql, useSubscription } from '@apollo/client';

const MESSAGE_ADDED_SUBSCRIPTION = gql`
  subscription get_messages {
    message {
      id
      created_at
      updated_at
      content
      deleted
    }
  }
`;
const { data, loading, error } = useSubscription(MESSAGE_ADDED_SUBSCRIPTION, {
    skip: typeof window == "undefined",
  });
@phryneas
Copy link
Member

phryneas commented Aug 5, 2024

You seem to be getting a WebSocket error, but at first look that doesn't seem to be related to Apollo Client. Can you try to remove AC from the picture and report back if it works opening a direct WebSocket connection without Apollo Client?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants