All files / src/apollo client.js

100% Statements 13/13
50% Branches 2/4
100% Functions 2/2
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 402x 2x 2x 2x   2x   2x 2x   2x               2x   2x 2x         2x               2x          
const fetch = require("isomorphic-fetch");
const { ApolloClient, InMemoryCache, ApolloLink, HttpLink } = require("@apollo/client");
const { setContext } = require("@apollo/client/link/context");
const { MAGENTO_GRAPHQL, STRAPI_GRAPHQL } = require('../../config');
 
const magentoLink = new HttpLink({ uri: MAGENTO_GRAPHQL, fetch });
 
const magentoAuthLink = setContext((_, { headers }) => {
  const token = typeof window !== 'undefined' ? localStorage.getItem('ks-token') : null;
 
  return {
    headers: {
      ...headers,
      authorization: token ? `Bearer ${token}` : ''
    }
  }
});
 
const strapiLink = new HttpLink({ uri: STRAPI_GRAPHQL, fetch });
 
const graphqlEndpoints = ApolloLink.split(
  operation => operation.getContext().client === 'strapi',
  strapiLink,
  magentoAuthLink.concat(magentoLink)
);
 
const cache = new InMemoryCache({
  typePolicies: {
    Customer: {
      keyFields: ["email"]
    }
  }
});
 
const client = new ApolloClient({
  link: ApolloLink.from([graphqlEndpoints]),
  cache
});
 
export default client;