首页 > 解决方案 > Apollo graphql 在 localhost 上发送请求,而不是指定 ep

问题描述

我配置了一个多客户端 vue apollo,但由于某种原因,它只向 localhost 发送请求。但是,我从未指定 localhost 端点。这是我的配置文件
vue-apollo.js:

import Vue from "vue";
import VueApollo from "vue-apollo";

import {
  createApolloClient,
  restartWebsockets
} from "vue-cli-plugin-apollo/graphql-client";

Vue.use(VueApollo);
const AUTH_TOKEN = "apollo-token";
const httpsEndpoint =
  process.env.VUE_APP_GRAPHQL_HTTPS || "https://myst.endpoint.prod/graphql";

export const filesRoot =
  process.env.VUE_APP_FILES_ROOT ||
  httpsEndpoint.substr(0, httpsEndpoint.indexOf("/graphql"));

Vue.prototype.$filesRoot = filesRoot;


const defaultOptions = {

  httpsEndpoint,

  wssEndpoint:
    process.env.VUE_APP_GRAPHQL_WSS || "wss://myst.endpoint.prod/graphql",

  tokenName: AUTH_TOKEN,

  persisting: false,

  websocketsOnly: false,

  ssr: false
};
const clientAOptions = {
  httpsEndpoint: "https://myst.endpoint.prod/graphql"
};
const clientBOptions = {
  httpsEndpoint: "https://mynd.endpoint.prod/graphql"
};

export function createProvider(options = {}) {

  const createA = createApolloClient({
    ...defaultOptions,
    ...clientAOptions
  });
  const createB = createApolloClient({
    ...defaultOptions,
    ...clientBOptions
  });

  const a = createA.apolloClient;
  const b = createB.apolloClient;


  const apolloProvider = new VueApollo({
    clients: {
      a,
      b
    },
    defaultClient: a,
    defaultOptions: {
      $query: {
      
      }
    },
    errorHandler(error) {

      console.log(
        "%cError",
        "background: red; color: white; padding: 2px 4px; border-radius: 3px; font-weight: bold;",
        error.message
      );
    }
  });

  return apolloProvider;
}


export async function onLogin(apolloClient, token) {
  if (typeof localStorage !== "undefined" && token) {
    localStorage.setItem(AUTH_TOKEN, token);
  }
  if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient);
  try {
    await apolloClient.resetStore();
  } catch (e) {

    console.log("%cError on cache reset (login)", "color: orange;", e.message);
  }
}


export async function onLogout(apolloClient) {
  if (typeof localStorage !== "undefined") {
    localStorage.removeItem(AUTH_TOKEN);
  }
  if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient);
  try {
    await apolloClient.resetStore();
  } catch (e) {
    
    console.log("%cError on cache reset (logout)", "color: orange;", e.message);
  }
}

我根据文档编辑了这个文件。为什么 apollo 将请求发送到错误的端点以及如何修复它?我根据文档编辑了这个文件。为什么 apollo 将请求发送到错误的端点以及如何修复它?我

标签: vue.jsgraphqlapollo

解决方案


推荐阅读