How do I create a GraphQL appSubscriptionCreate mutation with the Ruby shopify_api gem?

Hi,

When I copy the example below from https://help.shopify.com/en/api/guides/billing-api/implement-your-business-model#implement-the-appsubscription-resource:

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

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:

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

Hey @Lull ,

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

Sounds like you’re close. I suspect wrapping the mutation in { } is causing the issue. Try:

PAYMENT_MUTATION = @@client.parse <<-'GRAPHQL'
        mutation {
          ...

It works - fantastic!
So close, and yet so far ;o)

Thank you!

-Louise