-
-
Save catalinpit/839c22b0a430c5b690a3d2d409115674 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 { ApolloClient, HttpLink, InMemoryCache, split } from "@apollo/client"; | |
import { GraphQLWsLink } from '@apollo/client/link/subscriptions'; | |
import { createClient } from "graphql-ws"; | |
import { getMainDefinition } from "@apollo/client/utilities"; | |
const GRAPHQL_ENDPOINT = "realtime-poll.hasura.app"; | |
const scheme = (proto) => | |
window.location.protocol === "https:" ? `${proto}s` : proto; | |
const wsURI = `${scheme("ws")}://${GRAPHQL_ENDPOINT}/v1/graphql`; | |
const httpURL = `${scheme("https")}://${GRAPHQL_ENDPOINT}/v1/graphql`; | |
const splitter = ({ query }) => { | |
const { kind, operation } = getMainDefinition(query) || {}; | |
const isSubscription = | |
kind === "OperationDefinition" && operation === "subscription"; | |
return isSubscription; | |
}; | |
const cache = new InMemoryCache(); | |
const options = { reconnect: true }; | |
const wsLink = new GraphQLWsLink(createClient({ url: wsURI, connectionParams: { options } })); | |
const httpLink = new HttpLink({ uri: httpURL }); | |
const link = split(splitter, wsLink, httpLink); | |
const client = new ApolloClient({ link, cache }); | |
export default client; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment