Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

GraphQL

Generates GraphQL Schema Definition Language (SDL) types.

Type Mappings

Rust TypeGraphQL Type
StringString
boolBoolean
u8, u16, u32, i8, i16, i32Int
f32, f64Float
UuidID
DateTime<Utc>DateTime (custom scalar)
Option<T>Nullable (no !)
Vec<T>[T!]
HashMap<K, V>JSON (custom scalar)

Example

Rust:

#![allow(unused)]
fn main() {
#[derive(TypeWriter)]
#[sync_to(graphql)]
pub struct User {
    pub id: String,
    pub email: String,
    pub age: Option<u32>,
}
}

Generated:

scalar DateTime
scalar JSON

type User {
  id: ID!
  email: String!
  age: Int
}

Enums

Simple enums become GraphQL enums:

Rust:

#![allow(unused)]
fn main() {
#[derive(TypeWriter)]
#[sync_to(graphql)]
pub enum Role {
    Admin,
    User,
    Guest,
}
}

GraphQL:

enum Role {
  ADMIN
  USER
  GUEST
}

File Naming

Files use snake_case by default: UserProfileuser_profile.graphql

[graphql]
file_style = "kebab-case"  # user-profile.graphql