Expand description
§spark-graphql-client
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.
§Features
- Basic GraphQL query and mutation execution.
- Support for variables in GraphQL requests.
- Optional Zstd payload compression.
- Automatic response decompression (Zstd).
- Configurable base URL and user agent.
§Limitations
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.
§Example
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(())
} Structs§
- Graphql
Client - A client for executing GraphQL operations (queries and mutations).
Enums§
- GraphQL
Payload Compression - Specifies the type of payload compression to use for outgoing GraphQL requests.