vue-apollo的多客户端的用法

2019-12-23 23:38:59

参考地址 vue-apollo的多客户端的用法

vue-apollo的多客户端的用法以及apollo.js的配置


关于如何安装和如何使用,这篇文章就先暂时不介绍了,如果不清楚就看我另一篇关于vue-apollo的用法


在做项目中,有时候后端的接口是按模块功能去划分的,那么请求的地址就会不同,关于vue-apollo的多客户端配置如下:


import { ApolloClient } from 'apollo-client';

import { HttpLink } from 'apollo-link-http';

import { InMemoryCache } from 'apollo-cache-inmemory';

import VueApollo from 'vue-apollo';


const httpLink = new HttpLink({

  // You should use an absolute URL here

  uri: './v1/assess/api',

  credentials: 'same-origin',

});


const otherHttpLink = new HttpLink({

  // You should use an absolute URL here

  uri: './v1/broker', 

  // 这个地址就是我们做另外部分的功能所用到的接口地址

  credentials: 'same-origin',

});


// Create the apollo client

const apolloClient = new ApolloClient({

  link: httpLink,

  cache: new InMemoryCache(),

  connectToDevTools: true,

});


const otherApolloClient = new ApolloClient({

  link: otherHttpLink,

  cache: new InMemoryCache(),

  connectToDevTools: true,

});


export default new VueApollo({

  clients: {

    default: apolloClient,

    // 这个是我设置的默认接口的地址

    other: otherApolloClient,

    // 这是另一部分功能的接口地址,另外这个key名,在页面中写具体请求时会用到,因为我们要指定接口的地址

  },

  defaultClient: apolloClient,

});


那么配置好以后,关于用法,官方文档介绍了两种用法,那么我呢就以其中的一种做一个说明,因为我的项目中就是这么使用的:

例如:


loadPlan() {

    this.$apollo.query({

      query: getBrokerPlanDetail,

      variables: {

        id: this.planId,

      },

      client: 'other',

      //其中client是自己内置的属性,他的值注意要和自己在apollo.js中配置的key要保持一致

      fetchPolicy: 'no-cache',

    }).then(({ data }) => {

      this.brokerPLan = data.brokerPlan;

    });

},


  • 2020-02-02 15:40:36

    Apache Commons IO之IOUtils优雅操作流

    在开发过程中,你肯定遇到过从流中解析数据,或者把数据写入流中,或者输入流转换为输出流,而且最后还要进行流的关闭,原始jdk自带的方法写起来太复杂,还要注意各种异常,如果你为此感到烦恼,那IOUtils可以让我们优雅的操作流。

  • 2020-02-02 19:24:38

    百度视频SDK,突然不能播放

    开发过程中,不知道什么时候开始视频不能播发了,怎么办都不行,其他项目没问题,线上都也没有问题,这可急躁完蛋我了,整了仨小时,还是那熊样。 哎。

  • 2020-02-04 18:43:10

    AssetManager.finalize() Timed Out 解决办法以及分析

    在我们的项目崩溃中,有一个比较常见的bug,就是 java.util.concurrent.TimeoutException android.content.res.AssetManager.finalize() timed out after 10 seconds 意思简单明了,就是说在AssetManager析构的时候发生了超时异常。

  • 2020-02-06 13:32:10

    android.os.NetworkOnMainThreadException

    在Android 4.0以上,网络连接不能放在主线程上,不然就会报错android.os.NetworkOnMainThreadException。但是4.0下版本可以不会报错。

  • 2020-02-07 23:46:44

    You must call removeView() on the child's parent first解决办法

    出现这样的情况最多是发生在recyclerView中,holder复用的过程中,多次添加view,第一次添加的时候view有了父类了,可能就是复用引起的。 我是发生在给recyclerView添加广告view的时候发生的。