In progress subscriptions

This commit is contained in:
Patrick Fic
2019-12-11 16:52:57 -08:00
parent 51040fd455
commit 5c7523e6bd
7 changed files with 118 additions and 24 deletions

View File

@@ -6,8 +6,11 @@ import App from "./App";
import Spin from "../components/loading-spinner/loading-spinner.component";
import ApolloClient from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { split } from "apollo-link";
import { HttpLink } from "apollo-link-http";
import { WebSocketLink } from "apollo-link-ws";
import { getMainDefinition } from "apollo-utilities";
import { InMemoryCache } from "apollo-cache-inmemory";
import { setContext } from "apollo-link-context";
import { resolvers, typeDefs } from "../graphql/resolvers";
import apolloLogger from "apollo-link-logger";
@@ -26,6 +29,27 @@ class AppContainer extends Component {
uri: process.env.REACT_APP_GRAPHQL_ENDPOINT
});
const wsLink = new WebSocketLink({
uri: process.env.REACT_APP_GRAPHQL_ENDPOINT_WS,
options: {
reconnect: true
}
});
const link = split(
// split based on operation type
({ query }) => {
const definition = getMainDefinition(query);
console.log("000000", definition.operation);
return (
definition.kind === "OperationDefinition" &&
definition.operation === "subscription"
);
},
wsLink,
httpLink
);
const authLink = setContext((_, { headers }) => {
// get the authentication token from local storage if it exists
const token = localStorage.getItem("token");
@@ -46,7 +70,7 @@ class AppContainer extends Component {
if (process.env.NODE_ENV === "development") {
middlewares.push(apolloLogger);
}
middlewares.push(authLink.concat(httpLink));
middlewares.push(authLink.concat(link));
const cache = new InMemoryCache({ addTypename: false });
@@ -61,7 +85,7 @@ class AppContainer extends Component {
errorPolicy: "ignore"
},
query: {
fetchPolicy: "network-only",
fetchPolicy: "cache-and-network",
errorPolicy: "all"
}
}
@@ -98,6 +122,7 @@ class AppContainer extends Component {
if (!loaded) {
return <Spin />;
}
return (
<ApolloProvider client={client}>
<Query query={GET_CURRENT_USER}>