pub struct GraphqlClient { /* private fields */ }Expand description
GraphQL API client for Shopify Admin API.
Provides methods (query, query_with_debug) for executing GraphQL queries
with variable support, custom headers, and retry handling.
§Thread Safety
GraphqlClient is Send + Sync, making it safe to share across async tasks.
§GraphQL is the Recommended API
Unlike the REST Admin API, the GraphQL Admin API is Shopify’s recommended approach. This client does not log deprecation warnings.
§Example
use shopify_sdk::{GraphqlClient, Session, ShopDomain};
use serde_json::json;
let session = Session::new(
"session-id".to_string(),
ShopDomain::new("my-store").unwrap(),
"access-token".to_string(),
"read_products".parse().unwrap(),
false,
None,
);
let client = GraphqlClient::new(&session, None);
// Simple query
let response = client.query("query { shop { name } }", None, None, None).await?;
// Query with variables
let response = client.query(
"query GetProduct($id: ID!) { product(id: $id) { title } }",
Some(json!({ "id": "gid://shopify/Product/123" })),
None,
None
).await?;Implementations§
Source§impl GraphqlClient
impl GraphqlClient
Sourcepub fn new(session: &Session, config: Option<&ShopifyConfig>) -> Self
pub fn new(session: &Session, config: Option<&ShopifyConfig>) -> Self
Creates a new GraphQL client for the given session.
This constructor uses the API version from the configuration, or falls back to the latest stable version if not specified.
Unlike RestClient, this constructor
is infallible (returns Self, not Result) and does not log a
deprecation warning since GraphQL is the recommended API.
§Arguments
session- The session providing shop domain and access tokenconfig- Optional configuration for API version and other settings
§Example
use shopify_sdk::{GraphqlClient, Session, ShopDomain};
let session = Session::new(
"session-id".to_string(),
ShopDomain::new("my-store").unwrap(),
"access-token".to_string(),
"read_products".parse().unwrap(),
false,
None,
);
let client = GraphqlClient::new(&session, None);Sourcepub fn with_version(
session: &Session,
config: Option<&ShopifyConfig>,
version: ApiVersion,
) -> Self
pub fn with_version( session: &Session, config: Option<&ShopifyConfig>, version: ApiVersion, ) -> Self
Creates a new GraphQL client with a specific API version override.
This constructor allows overriding the API version from configuration.
§Arguments
session- The session providing shop domain and access tokenconfig- Optional configuration for other settingsversion- The API version to use for requests
§Example
use shopify_sdk::{GraphqlClient, Session, ShopDomain, ApiVersion};
let session = Session::new(
"session-id".to_string(),
ShopDomain::new("my-store").unwrap(),
"access-token".to_string(),
"read_products".parse().unwrap(),
false,
None,
);
// Use a specific API version
let client = GraphqlClient::with_version(&session, None, ApiVersion::V2024_10);Sourcepub const fn api_version(&self) -> &ApiVersion
pub const fn api_version(&self) -> &ApiVersion
Returns the API version being used by this client.
Sourcepub async fn query(
&self,
query: &str,
variables: Option<Value>,
headers: Option<HashMap<String, String>>,
tries: Option<u32>,
) -> Result<HttpResponse, GraphqlError>
pub async fn query( &self, query: &str, variables: Option<Value>, headers: Option<HashMap<String, String>>, tries: Option<u32>, ) -> Result<HttpResponse, GraphqlError>
Executes a GraphQL query against the Admin API.
This method sends a POST request to the graphql.json endpoint with
the query and optional variables.
§Arguments
query- The GraphQL query stringvariables- Optional variables for the queryheaders- Optional extra headers to include in the requesttries- Optional number of retry attempts (default: 1, no retries)
§Returns
Returns the raw HttpResponse containing:
code: HTTP status code (usually 200 for GraphQL)headers: Response headersbody: JSON response withdata,errors, andextensionsfields
§Errors
Returns GraphqlError::Http for HTTP-level errors (network errors,
non-2xx responses, retry exhaustion).
Note that GraphQL-level errors (user errors, validation errors) are
returned with HTTP 200 status and contained in response.body["errors"].
§Example
use shopify_sdk::GraphqlClient;
use serde_json::json;
// Simple query
let response = client.query(
"query { shop { name } }",
None,
None,
None
).await?;
println!("Shop: {}", response.body["data"]["shop"]["name"]);
// Query with variables and retries
let response = client.query(
"query GetProduct($id: ID!) { product(id: $id) { title } }",
Some(json!({ "id": "gid://shopify/Product/123" })),
None,
Some(3) // Retry up to 3 times on 429/500
).await?;
// Check for GraphQL errors
if let Some(errors) = response.body.get("errors") {
println!("GraphQL errors: {}", errors);
}Sourcepub async fn query_with_debug(
&self,
query: &str,
variables: Option<Value>,
headers: Option<HashMap<String, String>>,
tries: Option<u32>,
) -> Result<HttpResponse, GraphqlError>
pub async fn query_with_debug( &self, query: &str, variables: Option<Value>, headers: Option<HashMap<String, String>>, tries: Option<u32>, ) -> Result<HttpResponse, GraphqlError>
Executes a GraphQL query with debug mode enabled.
This method is identical to query but appends
?debug=true to the request URL, which causes Shopify to include
additional query cost and execution information in the response’s
extensions field.
§Arguments
query- The GraphQL query stringvariables- Optional variables for the queryheaders- Optional extra headers to include in the requesttries- Optional number of retry attempts (default: 1, no retries)
§Returns
Returns the raw HttpResponse with additional debug information
in response.body["extensions"].
§Errors
Returns GraphqlError::Http for HTTP-level errors.
§Example
use shopify_sdk::GraphqlClient;
let response = client.query_with_debug(
"query { shop { name } }",
None,
None,
None
).await?;
// Debug info available in extensions
if let Some(extensions) = response.body.get("extensions") {
println!("Query cost: {}", extensions["cost"]);
}