SQL Server
SQL Server
Shopify GraphQL Simple Query (Get Shop Object)
See more Shopify Examples
Demonstrates a simple Shopify GraphQL query to get specific fields of the Shop object.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- This example will use private authentication (which is HTTP Basic authentication)
-- See the other Chilkat Shopify examples for OAuth2 authentication.
-- To use HTTP Basic Authentication with any HTTP request, we simply set the Login, Password, and BasicAuth properties.
-- Important: All HTTP requests using Basic authentication must be over SSL/TLS.
EXEC sp_OASetProperty @http, 'Login', 'SHOPIFY_PRIVATE_API_KEY'
EXEC sp_OASetProperty @http, 'Password', 'SHOPIFY_PRIVATE_API_SECRET_KEY'
EXEC sp_OASetProperty @http, 'BasicAuth', 1
-- We're going to do a POST https://{shop}.myshopify.com/admin/api/2021-04/graphql.json
-- Make sure to replace "chilkat" with your store name.
-- The body of the request will be:
-- {
-- shop {
-- id
-- name
-- description
-- email
-- }
-- }
-- The above query is not JSON. It looks like JSON, but it's actually not.
-- We'll just make it one line:
DECLARE @query nvarchar(4000)
SELECT @query = '{ shop { id name description email } }'
-- My store name is "chilkat". Use your store name here instead.
DECLARE @url nvarchar(4000)
SELECT @url = 'https://chilkat.myshopify.com/admin/api/2021-04/graphql.json'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpStr', @success OUT, 'POST', @url, @query, 'utf-8', 'application/graphql', @resp
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
RETURN
END
-- Examine the response code.
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'Received error response code: ' + @iTmp0
PRINT 'Response body:'
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
RETURN
END
DECLARE @sbResponseBody int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT
EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody
DECLARE @jResp int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT
EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
EXEC sp_OASetProperty @jResp, 'EmitCompact', 0
PRINT 'Response Body:'
EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
PRINT @sTmp0
DECLARE @respStatusCode int
EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT
PRINT 'Response Status Code = ' + @respStatusCode
IF @respStatusCode >= 400
BEGIN
PRINT 'Response Header:'
EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
RETURN
END
-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)
-- {
-- "data": {
-- "shop": {
-- "id": "gid:\/\/shopify\/Shop\/24198053",
-- "name": "chilkat",
-- "description": null,
-- "email": "admin@chilkatsoft.com"
-- }
-- },
-- "extensions": {
-- "cost": {
-- "requestedQueryCost": 1,
-- "actualQueryCost": 1,
-- "throttleStatus": {
-- "maximumAvailable": 1000.0,
-- "currentlyAvailable": 999,
-- "restoreRate": 50.0
-- }
-- }
-- }
-- }
-- Sample code for parsing the JSON response...
-- Use the following online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
DECLARE @shopId nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @shopId OUT, 'data.shop.id'
DECLARE @shopName nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @shopName OUT, 'data.shop.name'
DECLARE @shopDescription nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @shopDescription OUT, 'data.shop.description'
DECLARE @shopEmail nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @shopEmail OUT, 'data.shop.email'
DECLARE @costRequestedQueryCost int
EXEC sp_OAMethod @jResp, 'IntOf', @costRequestedQueryCost OUT, 'extensions.cost.requestedQueryCost'
DECLARE @costActualQueryCost int
EXEC sp_OAMethod @jResp, 'IntOf', @costActualQueryCost OUT, 'extensions.cost.actualQueryCost'
DECLARE @costThrottleStatusMaximumAvailable nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @costThrottleStatusMaximumAvailable OUT, 'extensions.cost.throttleStatus.maximumAvailable'
DECLARE @costThrottleStatusCurrentlyAvailable int
EXEC sp_OAMethod @jResp, 'IntOf', @costThrottleStatusCurrentlyAvailable OUT, 'extensions.cost.throttleStatus.currentlyAvailable'
DECLARE @costThrottleStatusRestoreRate nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @costThrottleStatusRestoreRate OUT, 'extensions.cost.throttleStatus.restoreRate'
PRINT 'Shop name: ' + @shopName
-- ...
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
END
GO