在 Apollo Nuxt 中,如何在方法中访问非默认客户端?

In Apollo Nuxt, how do I access the non-default client in a method?

我在 Nuxt 应用程序中使用 Apollo。我正在尝试在一种方法中使用我的非默认客户端 (Shopify)。理想情况下是这样的:

在我的 nuxt.config.js 中,我有这样的 Apollo 设置:

apollo: {
    clientConfigs: {
        default: {
            httpEndpoint:
                "http://example.com",
            tokenName: "apollo-token",
            persisting: false,
            websocketsOnly: false
        },
        shopify: {
            httpEndpoint:
                "http://example.shopify.com",
            tokenName: "apollo-token"
            ... etc...
        }
    }
}

然后在我的组件中,我有这个方法:

methods: {
        createCheckout() {
            this.$apollo.getClient("shopify")
                .mutate({
                    mutation: this.checkoutQuery,
                    variables: {
                        variantId: this.selectedProduct.variantId,
                        quantity: this.selectedProduct.quantity
                    }
                })
        }
}}

这不起作用,因为 getClient() 只有 returns 默认客户端(在本例中为 WordPress)。那么如何在一个方法中访问另一个客户端呢?

想通了!答案是这样的:

methods: {
        createCheckout() {
            this.$apollo.provider.clients.shopify
                .mutate({
                    mutation: this.checkoutQuery,
                    variables: {
                        variantId: this.selectedProduct.variantId,
                        quantity: this.selectedProduct.quantity
                    }
                })
        }
}}