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}>

View File

@@ -18,11 +18,10 @@ class App extends React.Component {
componentDidMount() {
const { setCurrentUser } = this.props;
console.log("DIDMOUNT");
this.unsubscribeFromAuth = auth.onAuthStateChanged(async user => {
console.log("Current User:", user);
if (user) {
const token = await user.getIdToken();
const idTokenResult = await user.getIdTokenResult();
const hasuraClaim =
@@ -48,7 +47,7 @@ class App extends React.Component {
});
});
}
console.log('###Debug (app.js)| Token', token)
// console.log('###Debug (app.js)| Token', token)
//add the bearer token to the headers.
localStorage.setItem("token", token);
} else {
@@ -67,7 +66,10 @@ console.log('###Debug (app.js)| Token', token)
}
render() {
console.log("###Debug (App.js) | Props Current User: ", this.props.currentUser.email);
console.log(
"###Debug (App.js) | Props Current User: ",
this.props.currentUser.email
);
return (
<div>
<Switch>