Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

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

Solved

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

Lull
Excursionist
37 1 11

Hi,

 

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

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

Accepted Solution (1)

SBD_
Shopify Staff
1831 273 423

This is an accepted solution.

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 {
...

 

Scott | Developer Advocate @ Shopify 

View solution in original post

Replies 2 (2)

SBD_
Shopify Staff
1831 273 423

This is an accepted solution.

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 {
...

 

Scott | Developer Advocate @ Shopify 

Lull
Excursionist
37 1 11

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

 

Thank you!

-Louise