Subscribe only available for AWS AppSync endpoint

Viewed 228

I get issues when using @apollo/client: 3.5.10, aws-appsync:4.1.5. There is my config

import { ApolloClient, ApolloLink, createHttpLink, InMemoryCache } from '@apollo/client';
import { createAuthLink } from 'aws-appsync-auth-link';
import { createSubscriptionHandshakeLink } from 'aws-appsync-subscription-link';
// Config
import { AWS_APPSYNC } from '../config';

const { graphqlEndpoint, region, apiKey } = AWS_APPSYNC;
const auth = {
  type: AWS_APPSYNC.authenticationType,
  apiKey: apiKey,
};

const httpLink = createHttpLink({ uri: graphqlEndpoint });
const link = ApolloLink.from([
  // @ts-ignore
  createAuthLink({ graphqlEndpoint, region, auth }),
  // @ts-ignore
  createSubscriptionHandshakeLink({ graphqlEndpoint, region, auth }, httpLink),
]);

export const client = new ApolloClient({
  link,
  cache: new InMemoryCache(),
});

and I am using

const {
    data: subscription_message_data,
    loading: subscription_message_loading,
    error: subscription_message_error
  } = useSubscription(
    SUBSCRIPTION_NEW_MESSAGE, {
      variables: { conversationId: conversationId }
    });

But I got an error form useSubscription is: "Subscribe only available for AWS AppSync endpoint"

Does anyone have experience with this issue?

1 Answers

Had the exact same issue, and was baffled by this error message. At the end the issue turned out to be the incorrect property name for the url used in the createAuthLink and createSubscriptionHandshakeLink input objects.

This was the fix that worked for me:

const link = ApolloLink.from([
  createAuthLink({ url: graphQLEndpoint, region, auth }),
  createSubscriptionHandshakeLink({ url: graphQLEndpoint, region, auth }, httpLink),
]);
Related