0
votes

When I copy the example below from 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
    }
  }
}

and run it via the "Shopify GraphiQL App", the mutation is successfully created.

I am not sure how to do it with Ruby and the shopify_api gem though (note that I am new to Ruby as well as GraphQL, so it's probably something very basic I am missing, but I have not been able to find the answer anywhere).

I attempted the following:

    @@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

I get the following error though:

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

I tried skipping the mutation part, but then I just get the error:

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

This led me to have a look at the GraphQL class of the shopify_api gem, hoping to find a "mutation" method to use instead of the "query" method, but there is none.

I cannot figure it out from the graphql-client gem that shopify_api is using either - there's no mutation examples in the readme.

What am I missing?

Thanks,

-Louise

1

1 Answers

0
votes

Got the answer on the Shopify forum - I just have to remove the outer {} in 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