簡體   English   中英

在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()僅返回默認客戶端(在這種情況下為WordPress)。 那么,如何通過一種方法訪問其他客戶端呢?

弄清楚了! 答案是這樣的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM