Shopify Admin API GraphQL tutorials or sample code?

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

0 Likes
William_Homer
Tourist
7 1 4

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>'
0 Likes
William_Homer
Tourist
7 1 4

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 :D hoping I can get this working as it will make life so much easier 

0 Likes
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

 

0 Likes
William_Homer
Tourist
7 1 4

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

0 Likes
Alan_Reid
Tourist
12 0 1

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

0 Likes
William_Homer
Tourist
7 1 4

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

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
0 Likes