spark-graphql-client

Crates.iospark-graphql-client
lib.rsspark-graphql-client
version0.0.2
sourcesrc
created_at2025-05-05 22:16:24.796281+00
updated_at2025-05-05 22:28:46.630085+00
descriptionA generic and native GraphQL client implementation for Spark Rust SDK
homepage
repositoryhttps://github.com/polarityorg/spark-rs
max_upload_size
id1661651
size69,224
Flashnet Core (github:polarityorg:flashnet-core)

documentation

README

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(())
} 
Commit count: 0

cargo fmt