【问题标题】:In Apollo Nuxt, how do I access the non-default client in a method?在 Apollo Nuxt 中,如何在方法中访问非默认客户端?
【发布时间】:2019-12-16 14:34:17
【问题描述】:

我在 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)。那么如何在方法中访问其他客户端呢?

【问题讨论】:

    标签: javascript vue.js shopify nuxt.js apollo


    【解决方案1】:

    想通了!答案是这样的:

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

    【讨论】:

      猜你喜欢
      • 2019-01-10
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-30
      • 2021-12-01
      • 2021-05-07
      • 2021-10-14
      相关资源
      最近更新 更多