CodeGenerator

Struct CodeGenerator 

Source
pub struct CodeGenerator { /* private fields */ }
Expand description

High-level interface for generating Rust ORM code from GraphQL schemas.

The CodeGenerator provides a unified API for generating code regardless of the underlying ORM (Diesel or Sea-ORM). It handles the complete code generation pipeline: schema introspection, parsing, and code emission.

§ORM Support

  • Diesel: Generates table schemas, entity structs, and database migrations
  • Sea-ORM: Generates entity models, active records, and migration files

§Example

use graphql_codegen_rust::{CodeGenerator, Config};
use graphql_codegen_rust::cli::{OrmType, DatabaseType};

let config = Config {
    url: "https://api.example.com/graphql".to_string(),
    orm: OrmType::Diesel,
    db: DatabaseType::Postgres,
    output_dir: "./generated".into(),
    ..Default::default()
};

let generator = CodeGenerator::new(&config.orm);
generator.generate_from_config(&config).await?;

Implementations§

Source§

impl CodeGenerator

Source

pub fn new(orm: &OrmType) -> Self

Creates a new code generator for the specified ORM type.

§Parameters
  • orm: The ORM to generate code for (Diesel or Sea-ORM)
§Returns

A configured CodeGenerator ready to produce ORM code.

§Example
use graphql_codegen_rust::{CodeGenerator, cli::OrmType};

let generator = CodeGenerator::new(&OrmType::Diesel);
Source

pub async fn generate_from_config(&self, config: &Config) -> Result<()>

Generates complete ORM code from a GraphQL configuration.

This method orchestrates the full code generation pipeline:

  1. Introspects the GraphQL schema from the configured endpoint
  2. Parses the schema into an internal representation
  3. Generates ORM-specific code (entities, migrations, schemas)
  4. Writes generated files to the configured output directory
§Parameters
  • config: Complete configuration including GraphQL endpoint, ORM type, database settings, and output preferences
§Returns
  • Ok(()) on successful code generation
  • Err(anyhow::Error) with detailed context on failure
§Errors

This method can fail due to:

  • Network issues when accessing the GraphQL endpoint
  • Authentication failures (invalid headers)
  • Schema parsing errors (invalid GraphQL schema)
  • File system errors (permission issues, disk space)
  • Code generation constraints (unsupported GraphQL types)
§Example
use graphql_codegen_rust::{CodeGenerator, Config};
use graphql_codegen_rust::cli::{OrmType, DatabaseType};

let config = Config {
    url: "https://api.example.com/graphql".to_string(),
    orm: OrmType::Diesel,
    db: DatabaseType::Postgres,
    output_dir: "./generated".into(),
    ..Default::default()
};

let generator = CodeGenerator::new(&config.orm);
generator.generate_from_config(&config).await?;

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,