Skip to content

Instantly share code, notes, and snippets.

@skorfmann
Created March 11, 2022 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skorfmann/8ceeef367574428963a0cc4500310bed to your computer and use it in GitHub Desktop.
Save skorfmann/8ceeef367574428963a0cc4500310bed to your computer and use it in GitHub Desktop.
import { Credentials } from "@aws-amplify/core";
import { AuthOptions, createAuthLink } from "aws-appsync-auth-link";
import { createSubscriptionHandshakeLink } from "aws-appsync-subscription-link";
import {
ApolloClient, ApolloLink, HttpLink, InMemoryCache
} from "@apollo/client/core"
import gql from 'graphql-tag';
global.WebSocket = require('ws');
// Amplify config setup...
export async function subscribe() {
const url = config.graphqlEndpoint;
const region = config.region;
const auth: AuthOptions = {
type: "AWS_IAM",
credentials: async () => {
const credentials = await Credentials.get();
return Credentials.shear(credentials);
},
};
const httpLink = new HttpLink({ uri: url, })
const links = ApolloLink.from([
createAuthLink({ url, region, auth }),
createSubscriptionHandshakeLink({ url, region, auth }, httpLink),
]);
const apolloClient = new ApolloClient({
link: links,
cache: new InMemoryCache(),
})
apolloClient.subscribe({ query: gql`subscription onLogs {
logs {
message
timestamp
}
}`}).subscribe({
next: data => {
console.log(JSON.stringify(data, null, 2));
},
error: error => {
console.log(JSON.stringify(error, null, 2));
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment