here is my refrence https://github.com/awslabs/aws-mobile-appsync-sdk-js#using-authorization-and-subscription-links-with-apollo-client-v3-no-offline-support I am using apollo client version3.x
Here is code.
const auth = {
type: 'AMAZON_COGNITO_USER_POOLS',
jwtToken: async () => getToken()
}
const region = 'xx-xxxx-1';
const httpLink = createHttpLink({
uri: URL,
});
const authLink = createAuthLink({
url: URL,
region: region,
auth: auth
})
const wsLink = createSubscriptionHandshakeLink({ URL, region, auth }, httpLink)
const splitLink = ApolloLink.from([
authLink,
wsLink,
]);
const apollo_client = new ApolloClient({
link: splitLink,
cache: new InMemoryCache(),
});