Crates.io | spark-graphql-client |
lib.rs | spark-graphql-client |
version | 0.0.2 |
source | src |
created_at | 2025-05-05 22:16:24.796281+00 |
updated_at | 2025-05-05 22:28:46.630085+00 |
description | A generic and native GraphQL client implementation for Spark Rust SDK |
homepage | |
repository | https://github.com/polarityorg/spark-rs |
max_upload_size | |
id | 1661651 |
size | 69,224 |
A lightweight and flexible GraphQL client for Rust.
This crate provides a simple client for executing GraphQL queries and mutations against a GraphQL endpoint. It handles request building, optional payload compression (using Zstd), and response decompression and parsing.
Note: This client expects GraphQL queries and mutations to be provided as raw string literals. It does not work with pre-generated query types or builders.
This client is intentionally lightweight and does not implement the full GraphQL specification.
It focuses primarily on sending requests and parsing the data
part of the response.
Features like subscriptions, complex error handling beyond HTTP status, or automatic
fragment expansion are not included.
use std::collections::HashMap;
use serde::Deserialize;
use spark_graphql_client::{GraphQLPayloadCompression, GraphqlClient};
#[derive(Deserialize, Debug)]
struct MyData {
// Define fields matching your GraphQL response structure
field: String,
}
#[tokio::main]
async fn main() -> eyre::Result<()> {
let client = GraphqlClient::new(
"https://your-graphql-endpoint.com/graphql",
"my-app/1.0",
Some(GraphQLPayloadCompression::Zstd), // Use Zstd compression
);
let query = r#"
query GetData($id: ID!) {
getData(id: $id) {
field
}
}
"#;
let mut variables = HashMap::new();
variables.insert("id".to_string(), serde_json::json!("some-id"));
// Replace with actual identity key and session token if needed
let dummy_identity_public_key = b"identity_key";
let session_token = Some("your_session_token");
let result = client
.execute_graphql_request::<MyData>(
query,
&variables,
dummy_identity_public_key,
session_token,
)
.await?;
println!("Received data: {:?}", result);
Ok(())
}