Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function create (initialState) {
// Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient
return new ApolloClient({
connectToDevTools: process.browser,
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
link: ApolloLink.from([
new ApolloLink((operation, forward) => {
operation.setContext({
headers: {
Authorization: process.browser && localStorage.token
}
});
return forward(operation)
}),
new HttpLink({
uri: config.url,
credentials: 'same-origin',
fetch (uri, options) {
const { operationName } = JSON.parse(options.body)
return fetch(`${uri}?query=${operationName}`, options)
}
})
]),
InMemoryCache,
HttpLink
} from "apollo-boost";
// Components
import Home from "./pages/Home.jsx";
import Dashboard from "./pages/Dashboard.jsx";
// Styles
import "./styles/index.scss";
require("dotenv").config();
// Keys
const httpLink = new HttpLink({ uri: process.env.REACT_APP_SERVER_URL });
// Middleware to set the headers
const authLink = new ApolloLink((operation, forward) => {
if (localStorage.getItem("access_token") !== undefined) {
const token = localStorage.getItem("access_token");
operation.setContext({
headers: {
authorization: token ? `Bearer ${token}` : ""
}
});
return forward(operation);
}
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
fetchOptions: {
credentials: "include"
export function initClient(): AppClient {
let host = window.location.host;
if (process.env.NODE_ENV !== 'production') {
host = `${process.env.REACT_APP_SERVICE_HOST}:${process.env.REACT_APP_SERVICE_PORT}`;
}
const healthEndpoint = `${isSecureProtocol() ? 'https' : 'http'}://${host}/status`;
const batchLink = new BatchHttpLink({ uri: `${isSecureProtocol() ? 'https' : 'http'}://${host}/graphql` });
const authLink = new ApolloLink((operation, next) => {
const token = localStorage.getItem('token');
operation.setContext((context: Context) => ({
...context,
headers: {
...context.headers,
Authorization: token,
},
}));
return next ? next(operation) : null;
});
const httpLink = ApolloLink.from([authLink, batchLink]);
const subscriptionClient = new SubscriptionClient(`${isSecureProtocol() ? 'wss' : 'ws'}://${host}/graphql`, {
reconnect: true,
import { createUploadLink } from 'apollo-upload-client'
const cache = new InMemoryCache()
persistCache({
cache,
storage: localStorage
})
if (localStorage['apollo-cache-persist']) {
let cacheData = JSON.parse(localStorage['apollo-cache-persist'])
cache.restore(cacheData)
}
const httpLink = createUploadLink({ uri: 'http://localhost:4000/graphql' })
const authLink = new ApolloLink((operation, forward) => {
operation.setContext(context => ({
headers: {
...context.headers,
authorization: localStorage.getItem('token')
}
}))
return forward(operation)
})
const httpAuthLink = authLink.concat(httpLink)
const wsLink = new WebSocketLink({
uri: `ws://localhost:4000/graphql`,
options: { reconnect: true }
})