Loading Rust to GraphQL Schema Converter...

How to Turn Rust Structs into a GraphQL Schema

If a Rust service already returns JSON through serde and you want a GraphQL layer or a typed client on top of it, writing the schema by hand means copying every field and guessing how serde spells it. Paste the structs here and the types come out with the same names and shapes as that JSON.

Step 1

Paste your Rust types

Paste the struct and enum definitions your API returns, with their #[derive] and #[serde] attributes, or upload a .rs file. Whole files are fine: use lines, impl blocks and functions are skipped, and types inside mod blocks are read too. Click Sample to see a small example first.

use serde::Serialize;

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Order {
    pub order_id: u64,
    pub total: f64,
    pub quantity: i32,
    pub note: Option<String>,
    pub items: Vec<Item>,
    pub status: Status,
}

#[derive(Serialize)]
pub struct Item {
    pub sku: String,
    pub in_stock: bool,
}

#[derive(Serialize)]
pub enum Status {
    Pending,
    Shipped,
}
Step 2

Read the generated schema

A moment after you stop typing, the GraphQL schema appears on the right. Comments at the top explain every choice that needed a decision, such as which fields use the Long or JSON scalar and which keys were renamed. If the Rust code has a syntax mistake, you get the line and column instead.

The example above comes out like this. order_id is a u64, so it uses Long; rename_all turns it into orderId, while Item has no rename_all and keeps in_stock:

# Notes:
# - The Query type is a starting point with one field per top-level type. Replace it with your real queries.
# - Field names are the keys serde writes, so snake_case stays snake_case. async-graphql and Juniper turn Rust field names into camelCase by default; add #[serde(rename_all = "camelCase")] if you want both to match.
# - Long is a custom scalar declared below, used for Order.orderId.

"A whole number that can go past 2147483647, where GraphQL Int stops. Used for Rust u32, i64, u64, isize and usize fields. Implement it on your server as a 64-bit integer scalar; clients often read it as a string or BigInt to keep every digit."
scalar Long

type Query {
  order: Order
}

type Order {
  orderId: Long!
  total: Float!
  quantity: Int!
  note: String
  items: [Item!]!
  status: Status!
}

type Item {
  sku: String!
  in_stock: Boolean!
}

enum Status {
  Pending
  Shipped
}
Step 3

Copy or download it

Copy puts the schema on your clipboard and Download saves it as schema.graphql. Replace the starter Query type with your real queries and implement the custom scalars on your server.

What is Rust to GraphQL Conversion?

A GraphQL schema lists the types an API can return and which fields may be null. Rust structs carry the same information: field names, types, and Option for anything that can be missing. The conversion is mostly a translation of names and types, plus a few decisions where GraphQL is stricter, such as its 32-bit Int.

The field names follow serde's attributes, because that is what decides the JSON keys. For JSON you already have, try JSON to GraphQL; to check a schema after editing it, use the GraphQL Schema Validator, and GraphQL to TypeScript turns the result into client types.

Frequently Asked Questions

What Rust code does the converter read?

Type definitions: structs with named fields, tuple structs, unit structs, enums and type aliases, including generics, where clauses, pub(crate) and nested mod blocks. Everything else in a file (use lines, impl blocks, functions, constants, macros) is skipped. The code is read, never compiled or run. Values such as a vec![...] of struct literals describe data rather than types, so the page asks for the definitions instead; for JSON data, use JSON to GraphQL.

How are Rust types mapped to GraphQL?

String, &str and char become String, bool becomes Boolean, f32 and f64 become Float, and i8, i16, i32, u8 and u16 become Int. Option<T> makes a field nullable, every other field is non-null (!). Vec, VecDeque, HashSet, BTreeSet and arrays become lists. Box, Rc, Arc and Cow are looked through. Structs become object types and enums with plain variants become GraphQL enums. Uuid, chrono dates, Decimal and PathBuf become String, because serde writes them as text.

Why do i64 and u64 fields use a Long scalar instead of Int?

GraphQL Int is a signed 32-bit number, so it stops at 2147483647. An i64 ID like 8901240544102066246 would be rejected by a spec-following server, and so would a u32 above that limit. So u32, i64, u64, isize and usize use a custom scalar called Long, and i128 and u128 use BigInt. Both are declared in the output with a short description. Many clients read such numbers as strings so no digits are lost on the JavaScript side.

Which serde attributes are followed?

rename and rename_all (all of serde's case styles, such as camelCase, kebab-case and SCREAMING_SNAKE_CASE), skip and skip_serializing (the field is left out), skip_serializing_if (the field becomes nullable, since it can be missing), flatten (the inner struct's fields are pulled in) and transparent. Enums marked tag or untagged, enums with data, maps and tuples have no direct GraphQL form, so they use the JSON scalar and a comment explains why. A key that is not a valid GraphQL name, like first-name, gets a cleaned-up field name with the original key in its description.

Can I use the schema with async-graphql, Juniper or Apollo Server?

With Apollo Server, graphql-js or another schema-first server, paste the schema in and write resolvers that return the JSON your Rust service already produces. The field names match serde's keys, so the data fits. async-graphql and Juniper build the schema from Rust code themselves and turn snake_case fields into camelCase by default, so there the output is better used as a reference, or for client code generation and documentation. Run it through the GraphQL Schema Validator if you edit it by hand.

What are the starter Query type and the JSON scalar?

A GraphQL schema needs a Query type, so the output has one field for each type that no other type uses. Rename it or replace it with your real queries. The JSON scalar stands in for values GraphQL cannot type, such as HashMap fields, tuples, enums that carry data and generic parameters. You can give those fields a real type once you know their shape.

Is my code uploaded anywhere?

No. The conversion runs in your browser, the code is not sent to our server, and nothing is kept: a reload starts with an empty editor. Uploaded .rs files are read in the browser too, up to 10 MB.