Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { ApolloLink } from 'apollo-link';
  2. import { HttpLink } from 'apollo-link-http';
  3. import { API_URL } from '~/utils';
  4.  
  5. export default (ctx) => {
  6.   const httpLink = new HttpLink({ uri: `${API_URL}/graphql` });
  7.  
  8.   // auth token
  9.  
  10.   // middleware
  11.   const middlewareLink = new ApolloLink((operation, forward) => {
  12.     const token = ctx.isServer ? ctx.req.session.token : window.__NUXT__.state.auth.token;
  13.     if (token) {
  14.       operation.setContext({
  15.         headers: { Authorization: `JWT ${token}` },
  16.       });
  17.     }
  18.     return forward(operation);
  19.   });
  20.  
  21.   const link = middlewareLink.concat(httpLink);
  22.   return {
  23.     link,
  24.   };
  25. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement