apolloConfig.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { ApolloClient } from 'apollo-client'
  2. import { createHttpLink } from 'apollo-link-http'
  3. import { InMemoryCache, IntrospectionFragmentMatcher } from 'apollo-cache-inmemory'
  4. import { ApolloLink } from 'apollo-link'
  5. import { onError } from 'apollo-link-error'
  6. import { Message } from 'element-ui';
  7. import { getCookie, setCookie } from '@/utils/util.js';
  8. import store from '@/store/index.js';
  9. import vue from 'vue'
  10. const httpLink = createHttpLink({
  11. uri: '/graphql',
  12. credentials: 'include',
  13. headers: {
  14. Authorization: `Bearer ${getCookie('accessToken_devops')}` || null
  15. }
  16. })
  17. const middlewareLink = new ApolloLink((operation, forward) => {
  18. // operation.setContext({
  19. // headers: {
  20. // 'access-token': sessionStorage.getItem('access-token') || null
  21. // }
  22. // })
  23. return forward(operation).map(response => {
  24. // 结果数据
  25. return response
  26. })
  27. })
  28. // 拦截错误
  29. const errorLink = onError(({ networkError, response }) => {
  30. let errorMsg = '';
  31. console.log(networkError);
  32. if (networkError && networkError.statusCode === 401) {
  33. Message({
  34. message: '登录已过期,请重新登录',
  35. duration: 1000,
  36. type: 'error',
  37. onClose: function () {
  38. // 弹窗关闭时,跳转到登录
  39. setCookie('accessToken_devops', '', -1, '/');
  40. setCookie('refreshToken', '', -1, '/');
  41. setCookie('workDate', '', -1);
  42. window.top.location.href = store.getters.ctx;
  43. }
  44. });
  45. } else {
  46. if (!!response && response.errors !== undefined && response.errors.length) {
  47. errorMsg = !response.errors[0].message ? '服务器错误' : response.errors[0].message
  48. }
  49. if (networkError) {
  50. errorMsg = networkError.message
  51. if (networkError.result !== undefined) {
  52. errorMsg = networkError.result.success === false ? networkError.result.message : networkError.result.error
  53. }
  54. }
  55. if (errorMsg) {
  56. Message.error(errorMsg);
  57. }
  58. }
  59. })
  60. const authLink = middlewareLink.concat(httpLink)
  61. const defaultOptions = {
  62. watchQuery: {
  63. fetchPolicy: 'network-only',
  64. errorPolicy: 'ignore'
  65. },
  66. query: {
  67. fetchPolicy: 'network-only',
  68. errorPolicy: 'all'
  69. }
  70. }
  71. const fragmentMatcher = new IntrospectionFragmentMatcher({
  72. introspectionQueryResultData: {
  73. __schema: {
  74. types: [
  75. {
  76. kind: 'INTERFACE',
  77. name: 'Document',
  78. possibleTypes: [
  79. { name: 'MyInterface1' },
  80. { name: 'SomeInterface2' }
  81. ]
  82. }
  83. ]
  84. }
  85. }
  86. })
  87. const apollo = new ApolloClient({
  88. link: errorLink.concat(authLink),
  89. cache: new InMemoryCache({ fragmentMatcher }),
  90. connectToDevTools: true,
  91. defaultOptions: defaultOptions
  92. })
  93. export default apollo