Solved

Shopify Admin API GraphQL tutorials or sample code?

Alan_Reid
Tourist
12 0 1

Any one know of any decent tutorials or sample code to help me learn Shopify's GraphQL Admin API? I am using Rails, and have checked out the Shopify API docs, but don't seem to be getting anywhere.

 

I have the REST API working perfectly well, but can't seem to get the GraphQL side working - I have heard that the GraphQL side is quicker than the REST API.

 

Any help would be much appreciated.

Accepted Solution (1)

William_Homer
Tourist
7 1 6

This is an accepted solution.

Alan, I'm also working through testing the GraphQL API.  Are you using the ShopifyAPI gem? 

 

If you are one thing for initial setup would be to walk through this documentation, https://github.com/Shopify/shopify_api/blob/master/docs/graphql.md#make-queries.

 

From there I simply create a query and pass it to client.query.  

 

client = ShopifyAPI::GraphQL.client

QUERY = client.parse <<-GRAPHQL
	mutation {
		orderEditBegin(id: "gid://shopify/Order/2140232974441") {
			calculatedOrder {
				id
			}
			calculatedLineItems {
				id
			}
		}
	}
GRAPHQL

result = client.query(QUERY)

Here is a list of the queries that you can create based on Shopify's documentation, https://shopify.dev/docs/admin-api/graphql/reference/mutation/ordereditbegin.

 

I hope that helps at least get you started.

View solution in original post

Replies 16 (16)

William_Homer
Tourist
7 1 6

This is an accepted solution.

Alan, I'm also working through testing the GraphQL API.  Are you using the ShopifyAPI gem? 

 

If you are one thing for initial setup would be to walk through this documentation, https://github.com/Shopify/shopify_api/blob/master/docs/graphql.md#make-queries.

 

From there I simply create a query and pass it to client.query.  

 

client = ShopifyAPI::GraphQL.client

QUERY = client.parse <<-GRAPHQL
	mutation {
		orderEditBegin(id: "gid://shopify/Order/2140232974441") {
			calculatedOrder {
				id
			}
			calculatedLineItems {
				id
			}
		}
	}
GRAPHQL

result = client.query(QUERY)

Here is a list of the queries that you can create based on Shopify's documentation, https://shopify.dev/docs/admin-api/graphql/reference/mutation/ordereditbegin.

 

I hope that helps at least get you started.

Alan_Reid
Tourist
12 0 1

Hi William,

Thanks for your response. I have tried that, but I couldn't get it to work. I am assuming that you need to set up the following first, then you can access the code you pasted?

shop_url = "https://#{API_KEY}:#{PASSWORD}@#{SHOP_NAME}.myshopify.com"
ShopifyAPI::Base.site = shop_url
ShopifyAPI::Base.api_version = '<version_name>'
William_Homer
Tourist
7 1 6

yes, you would need to set the site and api version.  Are you getting an error on running `ShopifyAPI::GraphQL.client`?  If that's the case you need to save the schema so it loads when created, the gem has a nice rake task, the below is for private apps. 

rake shopify_api:graphql:dump SHOP_URL="https://API_KEY:PASSWORD@SHOP_NAME.myshopify.com" API_VERSION=2020-01

 

Alan_Reid
Tourist
12 0 1

I will try that, thanks for the advice 😄 hoping I can get this working as it will make life so much easier 

Alan_Reid
Tourist
12 0 1

I get this error...

 

dynamic constant assignment SHOP_NAME_QUERY = client.parse <<-'GRAPHQL'

any ideas?

 

This is my code (obviously with the correct api key etc.)

    shop_url = "https://#{API_KEY}:#{PASSWORD}@#{SHOP_NAME}.myshopify.com"
    ShopifyAPI::Base.site = shop_url
    ShopifyAPI::Base.api_version = '2020-01'

    client = ShopifyAPI::GraphQL.client

    SHOP_NAME_QUERY = client.parse <<-'GRAPHQL'
    {
    shop {
      name
    }
    }
    GRAPHQL

    result = client.query(SHOP_NAME_QUERY)
    puts result.data.shop.name

 

William_Homer
Tourist
7 1 6

Can you send the code that you are running prior to the error? 

Alan_Reid
Tourist
12 0 1

Just updated the code I am running, thank you for looking.

William_Homer
Tourist
7 1 6

Hmm, I'm running your exact code on my end and it's working.  The message that you are providing as well doesn't give me anything to go off of.  I'm not sure how you are running the code, if possible (and if your not already doing so), to run a rails c and run the code one line at a time to see where it may be failing? 

Alan_Reid
Tourist
12 0 1

@William_Homer wrote:

Hmm, I'm running your exact code on my end and it's working.  The message that you are providing as well doesn't give me anything to go off of.  I'm not sure how you are running the code, if possible (and if your not already doing so), to run a rails c and run the code one line at a time to see where it may be failing? 


mmmm, strange that's working within `rails c` 

I am just running it in my controller, I will try restart the app and see if that makes any difference. Just gotta wait for my workers to finishing processing data to Shopify haha

William_Homer
Tourist
7 1 6

yeah, if you are able to connect locally to a test or staging environment, It would be interesting to see what errors you get line by line.  Besides that I'm not sure If I can offer any additional suggests. 

Alan_Reid
Tourist
12 0 1

Do you have a sample of a method you're using at all?

Basically it seems that the issue is the graphQL query is what's causing the error in my code. If I move it outside the method I can run it, but then I am getting the error about setting the API version. 

You must set ShopifyAPI::Base.api_version before making a request.

Im thinking I need to set the credentials elsewhere, so its global and can be accessed.

Man the REST API was so easy to use haha, this is all new to me. 

Alan_Reid
Tourist
12 0 1

All sorted, thanks for all your help 😄 

Alan_Reid
Tourist
12 0 1

The system seemed to remove my update so I will repost.

shop_url = "https://#{API_KEY}:#{PASSWORD}@#{SHOP_NAME}.myshopify.com"
ShopifyAPI::Base.site = shop_url
ShopifyAPI::Base.api_version = '<version_name>'

client = ShopifyAPI::GraphQL.client

QUERY = client.parse <<-'GRAPHQL'
{
  shop {
    name
  }
}
GRAPHQL

result = client.query(QUERY)
puts result.data.shop.name
arjunrajkumar
Shopify Partner
9 0 1

Hi William, 

 

Had a doubt regarding this.. For public apps, this is the rake task:

 

$ rake shopify_api:graphql:dump SHOP_DOMAIN="SHOP_NAME.myshopify.com" ACCESS_TOKEN="SHOP_TOKEN" API_VERSION=2020-01

 

I was wondering how I would call it and create this automatically for each merchant store that signs up. Maybe each time a store is created, I should run this task? By using something like this inside the model?

Rake::Task["shopify_api:graphql:dump"].invoke("#{shopify_domain}", "#{shopify_token}", "2020-01")

Able to get this working from the console, but in the Rails app - its failing.. Any suggestions on how I could solve this?

 

Thank you! 

 

Arjun

future_friend
Tourist
5 0 0

Hi Arjun,

 

I was wondering the same thing. Did you figure it out?

 

Thanks,

Jonathan

robin_sandhu
Tourist
8 0 0

where should we have to run this rake command i am new to graphQL and dont know much about it.