首页 > 解决方案 > 如何使用 Ruby shopify_api gem 创建 GraphQL appSubscriptionCreate 突变?

问题描述

当我从https://help.shopify.com/en/api/guides/billing-api/implement-your-business-model#implement-the-appsu ...复制以下示例时:

  appSubscriptionCreate(
    name: "Super Duper Recurring Plan"
    returnUrl: "http://super-duper.shopifyapps.com"
    lineItems: [{
      plan: {
        appRecurringPricingDetails: {
            price: { amount: 10.00, currencyCode: USD }
        }
      }
    }]
  ) {
    userErrors {
      field
      message
    }
    confirmationUrl
    appSubscription {
      id
    }
  }
}

并通过“Shopify GraphiQL App”运行它,突变成功创建。

我不确定如何使用 Ruby 和 shopify_api gem 来完成它(请注意,我是 Ruby 和 GraphQL 的新手,所以这可能是我缺少的一些非常基本的东西,但我无法在任何地方找到答案) .

我尝试了以下操作:

    @@client = ShopifyAPI::GraphQL.new

    PAYMENT_MUTATION = @@client.parse <<-'GRAPHQL'
    {
        mutation {
            appSubscriptionCreate(
                name: "Super Duper Recurring Plan"
                returnUrl: "http://super-duper.shopifyapps.com"
                lineItems: [{
                    plan: {
                        appRecurringPricingDetails: {
                            price: {
                                amount: 10.00,
                                currencyCode: USD
                            }
                        }
                    }
                }]
            ) {
                userErrors {
                    field
                    message
                }
                confirmationUrl
                appSubscription {
                    id
                }
            }
        }
    }
    GRAPHQL

    def initialize
        @result = @@client.query(PAYMENT_MUTATION)
    end

    def confirmationUrl
        @result.data.appSubscriptionCreate.confirmationUrl
    end
end

我收到以下错误:

GraphQL::Client::ValidationError (Field 'mutation' doesn't exist on type 'QueryRoot'):

我尝试跳过突变部分,但后来我得到了错误:

GraphQL::Client::ValidationError (Field 'appSubscriptionCreate' doesn't exist on type 'QueryRoot'):

这导致我查看了 shopify_api gem 的 GraphQL 类,希望找到一个“变异”方法来代替“查询”方法,但没有。

我无法从 shopify_api 正在使用的 graphql-client gem 中弄清楚 - 自述文件中没有突变示例。

我错过了什么?

谢谢,

-路易丝

标签: ruby-on-railsrubygraphqlshopifyshopify-app

解决方案


在 Shopify 论坛上得到了答案 - 我只需删除 PAYMENT_MUTATION 中的外部 {}。

 PAYMENT_MUTATION = @@client.parse <<-'GRAPHQL'
    mutation {
        appSubscriptionCreate(
            name: "Super Duper Recurring Plan"
            returnUrl: "http://super-duper.shopifyapps.com"
            lineItems: [{
                plan: {
                    appRecurringPricingDetails: {
                        price: {
                            amount: 10.00,
                            currencyCode: USD
                        }
                    }
                }
            }]
        ) {
            userErrors {
                field
                message
            }
            confirmationUrl
            appSubscription {
                id
            }
        }
    }
GRAPHQL

推荐阅读