gql-rs
is a Rust library designed to streamline working with GraphQL. It provides modules for extracting GraphQL schemas using introspector queries and for making GraphQL requests in Rust applications.
- Schema Extraction: Extract GraphQL schemas effortlessly using introspector queries.
- GraphQL Requests: Perform GraphQL requests with ease in your Rust projects.
- Typed-Safe Client Code Generator for operations
- Operations & Fragement Generation
Use the introspector module to extract GraphQL schemas:
use gql_introspector::GQLIntrospector;
fn main() {
let introspector = GQLIntrospector::new();
introspector
.add("Authorization", "Bearer <GITHUB_TOKEN>")
.add("User-Agent", "Awesome-Octocat-App")
.get_schema("https://api.github.com/graphql")
.expect("Failed to get schema")
.build()
.expect("Failed to build schema")
.write("./output.graphql")
.expect("Failed to write schema to file");
println!("Schema introspection and write completed.");
}
Use the request module to perform GraphQL queries and mutations:
use gqlclient::{GQLClient, QueryBuilder};
fn main() {
let client: GQLClient = GQLClient::new("https://sauron.gandalf.network/public/gql");
let mut query_builder: QueryBuilder = QueryBuilder::new(INTROSPECTION_QUERY);
// dummy variables
query_builder.set_variable("test", "0x021513c8ed1a8b7566ebad8aa16ddcb476e83eaf493667db6967a9cd76fd70b388");
// dummy headers
query_builder.set_headers("X-Signature", "0x021513c8ed1a8b7566ebad8aa16ddcb476e83eaf493667db6967a9cd76fd70b388");
match client.run_query::<serde_json::Value>(&query_builder) {
Ok(response) => println!("GraphQL response: {:?}", response),
Err(e) => eprintln!("GraphQL query failed: {}", e),
}
}
See the LICENSE file for details.