const _initNetworkInterface = graphql_url => { const networkInterface = createNetworkInterface({ uri: graphql_url, opts: { credentials: 'same-origin' } }) // Authen networkInterface.use([{ applyMiddleware(req, next) { if (!req.options.headers) { req.options.headers = {} // Create the header object if needed. } // get the authentication token from local storage if it exists (async () => { const token = await persist.willGetSessionToken() req.options.headers.authorization = token ? `Bearer ${token}` : null next() })() } }]) return networkInterface }
function create(data) { const networkInterface = createNetworkInterface({ uri: data.endPoint, }); const middleWares = [ { applyMiddleware(req, next) { const token = data.oAuthToken; if (token) { req.options.headers = {}; req.options.headers.authorization = `Bearer ${token}`; } next(); }, }, ]; networkInterface.use(middleWares); const severNetworkInterface = createNetworkInterface({ uri: data.endPoint }); severNetworkInterface.use(middleWares); return new ApolloClient({ ssrMode: true, networkInterface: severNetworkInterface, }); }