SQL Server guide

Execute GraphQL query


Executes a GraphQL query against the Shopify Admin API using the generic_request table. This is useful for accessing data or performing operations not available via the standard REST API endpoints.

Standard SQL query example

This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.

SELECT * FROM generic_request 
WITH (
    URL='/graphql.json'
  --OR Use full URL
  --URL='https://MY-STORE-HERE.myshopify.com/admin/api/2023-10/graphql.json'
  
  , RequestMethod='POST'
  , Filter='$.data.products.nodes' --change table name here e.g. products
  , Headers='Content-Type: application/json'
  --change table name and columns below here e.g. products... and id, title etc
  -- change pagesize if needed (i.e. max 250)
  , Body='{
 "query" : "<<{
 
 products(first: 250 [$tag$])
 {
    nodes {
      id
      title
      createdAt
    }
    
    pageInfo {
      hasNextPage
      endCursor
    }
    
  }
  
},FUN_JSONENC>>"
}'
	, NextUrlAttributeOrExpr='$.data.products.pageInfo.endCursor' --change table name 
	, NextUrlEndIndicator='false'
	, StopIndicatorAttributeOrExpr='$.data.products.pageInfo.hasNextPage' --change table name 
	, UseConnection='True'
	, EnablePageTokenForBody='True'
	, HasDifferentNextPageInfo='True'
	, NextPageBodyPart='after: \"[$pagetoken$]\"'
	--Use metadata to speed up execution. To get Metadata Run query without Meta clause. 
	-- Then click View Metadata button found in Botttom Result Grid Toolbar. Get Compact format and paste below
	--, Meta='id:String(255); title:String(255); createdAt:DateTime; '
)

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_SHOPIFY_IN_GATEWAY], 'SELECT * FROM generic_request 
WITH (
    URL=''/graphql.json''
  --OR Use full URL
  --URL=''https://MY-STORE-HERE.myshopify.com/admin/api/2023-10/graphql.json''
  
  , RequestMethod=''POST''
  , Filter=''$.data.products.nodes'' --change table name here e.g. products
  , Headers=''Content-Type: application/json''
  --change table name and columns below here e.g. products... and id, title etc
  -- change pagesize if needed (i.e. max 250)
  , Body=''{
 "query" : "<<{
 
 products(first: 250 [$tag$])
 {
    nodes {
      id
      title
      createdAt
    }
    
    pageInfo {
      hasNextPage
      endCursor
    }
    
  }
  
},FUN_JSONENC>>"
}''
	, NextUrlAttributeOrExpr=''$.data.products.pageInfo.endCursor'' --change table name 
	, NextUrlEndIndicator=''false''
	, StopIndicatorAttributeOrExpr=''$.data.products.pageInfo.hasNextPage'' --change table name 
	, UseConnection=''True''
	, EnablePageTokenForBody=''True''
	, HasDifferentNextPageInfo=''True''
	, NextPageBodyPart=''after: \"[$pagetoken$]\"''
	--Use metadata to speed up execution. To get Metadata Run query without Meta clause. 
	-- Then click View Metadata button found in Botttom Result Grid Toolbar. Get Compact format and paste below
	--, Meta=''id:String(255); title:String(255); createdAt:DateTime; ''
)')

Using EXEC in SQL Server (handling larger SQL text)

The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements. This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).

Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_SHOPIFY_IN_GATEWAY] syntax.

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * FROM generic_request 
WITH (
    URL=''/graphql.json''
  --OR Use full URL
  --URL=''https://MY-STORE-HERE.myshopify.com/admin/api/2023-10/graphql.json''
  
  , RequestMethod=''POST''
  , Filter=''$.data.products.nodes'' --change table name here e.g. products
  , Headers=''Content-Type: application/json''
  --change table name and columns below here e.g. products... and id, title etc
  -- change pagesize if needed (i.e. max 250)
  , Body=''{
 "query" : "<<{
 
 products(first: 250 [$tag$])
 {
    nodes {
      id
      title
      createdAt
    }
    
    pageInfo {
      hasNextPage
      endCursor
    }
    
  }
  
},FUN_JSONENC>>"
}''
	, NextUrlAttributeOrExpr=''$.data.products.pageInfo.endCursor'' --change table name 
	, NextUrlEndIndicator=''false''
	, StopIndicatorAttributeOrExpr=''$.data.products.pageInfo.hasNextPage'' --change table name 
	, UseConnection=''True''
	, EnablePageTokenForBody=''True''
	, HasDifferentNextPageInfo=''True''
	, NextPageBodyPart=''after: \"[$pagetoken$]\"''
	--Use metadata to speed up execution. To get Metadata Run query without Meta clause. 
	-- Then click View Metadata button found in Botttom Result Grid Toolbar. Get Compact format and paste below
	--, Meta=''id:String(255); title:String(255); createdAt:DateTime; ''
)'
EXEC (@MyQuery) AT [LS_TO_SHOPIFY_IN_GATEWAY]