Vue.js 如何使用@vue/composition api和nuxt.js获取vue apollo实例

Vue.js 如何使用@vue/composition api和nuxt.js获取vue apollo实例,vue.js,apollo-client,vue-apollo,vuejs3,vue-composition-api,Vue.js,Apollo Client,Vue Apollo,Vuejs3,Vue Composition Api,我如何配置vue apollo,并与@vue/apollo composable结合使用@vue/composition api或Vue3.0 因为尽管我通过使用@nuxtjs/apollo获得了默认的apollo客户端: import { DefaultApolloClient } from "@vue/apollo-composable"; const myPlugin: Plugin = (context, inject) => { const defaultClient = c

我如何配置
vue apollo
,并与
@vue/apollo composable
结合使用
@vue/composition api
Vue3.0

因为尽管我通过使用
@nuxtjs/apollo
获得了默认的
apollo客户端

import { DefaultApolloClient } from "@vue/apollo-composable";
const myPlugin: Plugin = (context, inject) => {
  const defaultClient = ctx.app.apolloProvider.defaultClient;
   // do stuff with defaultClient, e.g. provide()
}

export default myPlugin
它仍然是空的,而不是用
numxt.config.ts
中的我的设置填充


如何使用
@vue/apollo composable
创建工作的
vue apollo
客户端,或者如何直接创建
context.root.$apollo

以下是我目前在nuxt中设置vue apollo的方法。这很可能是一个移动的目标,因为这两个包都相对较新,并且正在积极开发中

软件包版本为

"@vue/apollo-composable": "4.0.0-alpha.1"
"@vue/composition-api": "version": "0.3.4"
阿波罗装置

//apolloClient.js
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import link from './link';

export default function apolloClient(_, inject) {
  const cache = new InMemoryCache();

  const client = new ApolloClient({
    // Provide required constructor fields
    cache,
    link,
    // Provide some optional constructor fields
    name: 'apollo-client',
    queryDeduplication: false,
    defaultOptions: {
      watchQuery: {
        fetchPolicy: 'cache-and-network',
      },
    },
  });

  inject('apollo', client);
}

// link.js
import { split } from 'apollo-link';
import { HttpLink } from 'apollo-link-http';
import { WebSocketLink } from 'apollo-link-ws';
import { getMainDefinition } from 'apollo-utilities';
import fetch from 'unfetch';
const httpLink = new HttpLink({
  uri: 'http://localhost:8080/v1/graphql',
  credentials: 'same-origin',
  fetch,
});

const wsParams = {
  uri: `ws://localhost:8080/v1/graphql`,
  reconnect: true,
};

if (process.server) {
  wsParams.webSocketImpl = require('ws');
}

const wsLink = new WebSocketLink(wsParams);

// using the ability to split links, you can send data to each link
// depending on what kind of operation is being sent
const link = split(
  // split based on operation type
  ({ query }) => {
    const definition = getMainDefinition(query);
    return (
      definition.kind === 'OperationDefinition' &&
      definition.operation === 'subscription'
    );
  },
  wsLink,
  httpLink,
);

export default link;
然后在上面,您将apollo作为插件包含在您的nuxtconfig中

  plugins: [
    '~/plugins/vue-composition-api',
    '~/plugins/apolloClient'
  ],