1

我正在尝试将@vue/apollo-composable与我的 Nuxt-Ts 应用程序一起使用。这是应该如何将其注入“普通”Vue 应用程序的根实例的示例:

import { provide } from '@vue/composition-api'
import { DefaultApolloClient } from '@vue/apollo-composable'

const app = new Vue({
  setup () {
    provide(DefaultApolloClient, apolloClient)
  },

  render: h => h(App),
})

问题:我不知道如何访问Nuxt-TS 中的根实例。

我尝试制作一个插件,但它被直接注入到根实例中(这是不对的,因为@vue/apollo-composable使用composition-api::provide()它会创建它自己的属性_provided

如果我使用 nuxt 插件的inject一个$get 连接。如果我_provided直接通过它写一个对象,ctx.app._provided =它就不会粘住。

import { DefaultApolloClient } from "@vue/apollo-composable";
const myPlugin: Plugin = (context, inject) => {
  const defaultClient = ctx.app.apolloProvider.defaultClient;
  inject(DefaultApolloClient.toString(), defaultClient) // results in $$ and also composition-api::inject is checking inside `_provided[DefaultApolloClient]`
}

export default myPlugin

我不能provide()像原来的例子那样调用,因为它只允许在VueComponent::setup函数内部。

我还尝试创建一个组件并在我需要它的页面上使用它(虽然有点违背了在根实例中安装的目的)

const InstallGraphQl = createComponent({
  name: "InstallGraphQl",
  setup(_props, ctx: any) {
    debugger;
    const apolloClient = ctx.app.apolloProvider.defaultClient;
    ctx.provide(DefaultApolloClient, apolloClient);
  },
});
export default createComponent({
  name: "DefaultLayout",
  components: {
    InstallGraphQl
  },
  setup(_props, _ctx: SetupContext) {
    const { result } = useQuery(SharedLayoutQuery);
    return { result };
  },
});

但是随后setup导出的组件在InstallGraphQl::setup...之前被调用

编辑:有关更多信息,@vue/apollo-composable请参见此处的讨论:https ://github.com/vuejs/vue-apollo/issues/687

4

3 回答 3

6

只需将 a 设置setup()为根选项:

/* plugins/provide-apollo-client.js */

import {provide} from '@vue/composition-api'
import {DefaultApolloClient} from '@vue/apollo-composable'

export default function ({app}) {
  app.setup = () => {
    provide(DefaultApolloClient, ...)
  }

  // Or, use local mixin
  app.mixins = (app.mixins || []).concat({
    setup () {...},
  })
}
/* nuxt.config.js */

export default {
  plugins: ['~/plugins/provide-apollo-client'],
}

虽然对 nuxt-ts 不太熟悉,但我认为代码应该可以正常工作。

于 2019-12-10T15:38:12.083 回答
2

我不使用 nuxt-ts,但我确实在 nuxt 应用程序中有此设置。在我的 default.vue 模板中,我提供了这样的内容。

<script>
  import { provide } from '@vue/composition-api';
  import { ApolloClients } from '@vue/apollo-composable'

  export default {
    setup(props, context) {
      provide(ApolloClients, {
        default: context.root.$apollo,
      })
    }
  }
</script>

包版本是

"@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'
  ],
于 2019-12-05T16:04:56.847 回答
1

现在有一个官方的onGlobalSetup帮助器nuxt-composition-api

根据文档,您只需在插件中使用它,如下所示:

import { onGlobalSetup } from 'nuxt-composition-api'

export default () => {
  onGlobalSetup(() => {
    provide('globalKey', true)
  })
}
于 2020-06-15T18:51:15.233 回答