Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
add collection of types
Browse files Browse the repository at this point in the history
  • Loading branch information
kjuulh committed Jan 29, 2023
1 parent 2eb5d98 commit 7433453
Showing 1 changed file with 67 additions and 14 deletions.
81 changes: 67 additions & 14 deletions src/code_generation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use graphql_introspection_query::introspection_response::{
FullType, IntrospectionResponse, Schema, SchemaContainer,
self, FullType, IntrospectionResponse, Schema, SchemaTypes,
};

pub struct CodeGeneration;
Expand All @@ -20,25 +20,78 @@ impl CodeGeneration {
let mut output = String::new();
output.push_str("# code generated by dagger. DO NOT EDIT.");

let types = schema
.types
.as_ref()
.ok_or(eyre::anyhow!("types not found on schema"))?;
let types = get_types(schema)?;
//let remaining: Vec<Option<String>> = types.into_iter().map(type_name).collect();

// 1. Get a list of all types and map it to handlers
let types: Vec<Option<&FullType>> = types
.iter()
.map(|t| t.as_ref().map(|t| &t.full_type))
.collect();
todo!()
}
}

fn get_types(schema: &Schema) -> eyre::Result<Vec<&FullType>> {
let types = schema
.types
.as_ref()
.ok_or(eyre::anyhow!("types not found on schema"))?;

for t in types.iter().flatten() {
println!("type: {:?}", t.name);
// 1. Get a list of all types and map it to handlers
let types: Vec<&FullType> = types
.iter()
.map(|t| t.as_ref().map(|t| &t.full_type))
.flatten()
.collect();

Ok(types)
}

fn type_name(t: &FullType) -> Option<String> {
let name = t.name.as_ref();
if let Some(name) = name {
if name.starts_with("_") || !is_custom_scalar_type(t) {
return None;
}

// 2. Filter all custom types and change so that they take input instead
return Some(name.replace("Query", "Client"));
}

None
}

todo!()
fn is_scalar_type(t: &FullType) -> bool {
if let Some(introspection_response::__TypeKind::SCALAR) = t.kind {
return true;
}
false
}

enum Scalars {
ID(String),
Int(usize),
String(String),
Float(f64),
Boolean(bool),
Date(String),
DateTime(String),
Time(String),
Decimal(f64),
}

fn is_custom_scalar_type(t: &FullType) -> bool {
if is_scalar_type(t) {
let _ = match t.name.as_ref().map(|s| s.as_str()) {
Some("ID") => Scalars::ID("ID".into()),
Some("Int") => Scalars::Int(0),
Some("String") => Scalars::String("ID".into()),
Some("Float") => Scalars::Float(0.0),
Some("Boolean") => Scalars::Boolean(false),
Some("Date") => Scalars::Date("ID".into()),
Some("DateTime") => Scalars::DateTime("ID".into()),
Some("Time") => Scalars::Time("ID".into()),
Some("Decimal") => Scalars::Decimal(0.0),
Some(_) => return true,
None => return false,
};
}
false
}

#[cfg(test)]
Expand Down

0 comments on commit 7433453

Please sign in to comment.