Skip to content

Feedback for “Subscriptions” #2331

Description

@AsaffArieli

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(),
      }
    }))
  ]
};

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions