add collection of types

This commit is contained in:
Kasper Juul Hermansen 2023-01-29 01:14:18 +01:00
parent 2eb5d98c8a
commit 7433453c6d
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912

View File

@ -1,5 +1,5 @@
use graphql_introspection_query::introspection_response::{
FullType, IntrospectionResponse, Schema, SchemaContainer,
self, FullType, IntrospectionResponse, Schema, SchemaTypes,
};
pub struct CodeGeneration;
@ -20,27 +20,80 @@ 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"))?;
// 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();
for t in types.iter().flatten() {
println!("type: {:?}", t.name);
}
// 2. Filter all custom types and change so that they take input instead
let types = get_types(schema)?;
//let remaining: Vec<Option<String>> = types.into_iter().map(type_name).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"))?;
// 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;
}
return Some(name.replace("Query", "Client"));
}
None
}
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)]
mod tests {
use graphql_introspection_query::introspection_response::IntrospectionResponse;