Created
March 11, 2022 22:29
-
-
Save skorfmann/8ceeef367574428963a0cc4500310bed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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