Headless CMS for Rust
Hygraph is the ideal Headless CMS for Rust websites and applications. Read further to learn how our API-first CMS allows you to add components to your Rust apps in minutes and enable your website's content to be managed from a powerful CMS.
Step #1 - Construct your query and fetch the data from Hygraph
In Rust, querying a GraphQL API involves crafting a GraphQL query as a string, encoding it as a JSON payload, and then using an HTTP client like reqwest
to send a POST request to the API endpoint. The response, typically in JSON format, is then parsed and deserialized into Rust data structures using serde
, allowing for type-safe manipulation of the retrieved data within the Rust program. This succinct interaction pattern enables efficient and robust data fetching from a GraphQL server.
use reqwest;use serde::{Deserialize, Serialize};use serde_json::json;#[derive(Serialize, Deserialize, Debug)]struct Product {name: String,description: String,slug: String,availability: bool,image_url: String,}#[tokio::main]async fn main() -> Result<(), reqwest::Error> {let client = reqwest::Client::new();let query = json!({"query": "query GetProducts {products {namedescriptionslugavailabilityimageUrl}}"});let res = client.post("https://api-<region>.hygraph.com/v2/<some hash>/master").json(&query).send().await?;let json: serde_json::Value = res.json().await?;let products: Vec<Product> = serde_json::from_value(json["data"]["products"].clone()).unwrap();println!("{:#?}", products);Ok(())}
Running GraphQL mutations to store data to headless CMS
For mutations in Rust, the process is quite similar to queries. You compose a mutation string that specifies the operation you wish to perform on the server, such as adding or updating data. This string is then sent as JSON in the body of a POST request, using an HTTP client like reqwest
. Upon receiving a response, you parse the result with serde_json
to handle the JSON data. This enables your Rust application to perform write operations on the GraphQL server, effectively changing or adding data as needed.
use reqwest;use serde::{Deserialize, Serialize};use serde_json::json;#[derive(Serialize, Deserialize, Debug)]struct CreateProductResponse {createProduct: Product,}#[derive(Serialize, Deserialize, Debug)]struct Product {name: String,description: String,slug: String,availability: bool,image_url: String,}#[tokio::main]async fn main() -> Result<(), reqwest::Error> {let client = reqwest::Client::new();let mutation = json!({"query": "mutation CreateProduct($name: String!, $description: String!, $slug: String!, $availability: Boolean!, $imageUrl: String!) {createProduct(input: {name: $name, description: $description, slug: $slug, availability: $availability, imageUrl: $imageUrl}) {namedescriptionslugavailabilityimageUrl}}","variables": {"name": "New Product","description": "A new product description","slug": "new-product","availability": true,"imageUrl": "http://example.com/image.png"}});let res = client.post("https://management.hygraph.com/graphql").json(&mutation).send().await?;let json: serde_json::Value = res.json().await?;let create_product_response: CreateProductResponse = serde_json::from_value(json["data"].clone()).unwrap();println!("{:#?}", create_product_response);Ok(())}
Start building with Rust
We made it really easy to set up your project in Hygraph and use our GraphQL API within your Rust project.
Quickstart
Check out our docs to see how you can quickly set up your Hygraph project and enable the content API for your Rust website or app.
Learn GraphQL
Hygraph is GraphQL-native Headless CMS offers precise data retrieval, minimizing over-fetching and optimizing efficiency.
Examples
Look at some of the example projects to see Hygraph in action.
Why Hygraph
Choosing Hygraph for your Rust project
A GraphQL-native headless CMS paired with a Rust application empowers developers with the ability to make precise data queries that resonate with Rust's emphasis on efficiency and safety, ensuring that data handling is both optimized and reliable.
Content editors benefit from an agile management platform, where they can independently curate content with the assurance of consistent, application-wide representation, facilitated by GraphQL's self-documenting nature. This symbiosis between GraphQL and Rust streamlines content delivery across multiple platforms, enhancing both the development process and content strategy execution.
Developer Experience
We try to be the most un-opinionated CMS on the market with a wide collection of open source example projects to get you started.
Headless CMS
As a headless CMS (i.e. API based content management), you can be as modular and flexible as you need. We even support multiplatform content management.
Management API
Hygraph boasts a flexible and powerful management API to manage your content and schema, as well as a blazing fast content API.