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

Feedback for “Subscriptions” #2331

Open
AsaffArieli opened this issue Jan 13, 2025 · 1 comment
Open

Feedback for “Subscriptions” #2331

AsaffArieli opened this issue Jan 13, 2025 · 1 comment

Comments

@AsaffArieli
Copy link

AsaffArieli commented Jan 13, 2025

Authentication on the subscription topic in the documentation is unclear. I failed to authenticate using the following link over my named client.

const token = 'my_token';

const basic = setContext(() => ({
  headers: {
    Accept: 'charset=utf-8'
  }
}));

const authLink = setContext((operation, context) =>  ({
  headers: {
    Authorization: `Bearer ${token}`,
  },
}));

const splitLink = split(
  ({ query }) => {
    const definition = getMainDefinition(query);
    return (
      definition.kind === Kind.OPERATION_DEFINITION &&
      definition.operation === OperationTypeNode.SUBSCRIPTION
    );
  },
  new WebSocketLink({
    uri: `ws://localhost:5079/graphql`,
    options: {
      reconnect: true,
      connectionParams: () => ({
        authToken: `Bearer ${token}`, // also tried using Authorization:  `Bearer ${token}`,
      }),
    },
  }),
  authLink.concat(inject(HttpLink).create({
    uri: `http://localhost:5079/graphql`,
  })),
);

export const appConfig: ApplicationConfig = {
  providers: [
    provideRouter(routes),
    provideHttpClient(withFetch()),
    provideNamedApollo(() => ({
      myNamedClient: {
        link: ApolloLink.from([
          basic,
          splitLink
        ]),
        cache: new InMemoryCache(),
      }
    }))
  ]
};
@KeithGillette
Copy link
Contributor

You don't include imports in your code excerpt, but it looks like you are using subscriptions-graphql-ws which is no longer maintained and has been replaced in Apollo Client with graphql-ws.

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