Crates.io | klub_models |
lib.rs | klub_models |
version | 0.0.4 |
source | src |
created_at | 2021-10-23 04:34:47.180355 |
updated_at | 2021-11-01 09:27:43.924083 |
description | Klub core models and controllers |
homepage | |
repository | |
max_upload_size | |
id | 469752 |
size | 46,899 |
Rust library crate for sharing model traits, structs and interfaces.
Models are used to describe entities and relationships. Each model has a unique schema and is a mod in this crate.
The advantages of making each model a mod in a centralized lib are:
The crate has been published and it publicly available. This not ideal. We need to make the crate private eventually before going to production.
make build
rust klub_models = { path = "../../core/models", version = "0.0.3" }
to your Cargo.toml
You should now be able to make changes and use your IDE to run tests.
make release-models
cargo install https
- to run cargo doc
on localhost:3000
npm install -g browser-sync
- to live reload your docs as you make changesmake docs
in the root directory of the repo to start the rust docs serverlocalhost:3000/klub_models
to view docsEasily construct a model
let problem = Problem::new(id, name);
Since Problem
implements the Topic
trait. You can use .into()
to up-cast Problem
to Topic
.
let problem = Problem::new(id, name);
let topic = Some(problem.clone().into()); // topic is problem's super class
Every model mode with sub classes declares an enum deriving strum crate macros. Strum makes it easy to cast strings to enums and vice versa. This make is easy to match strings against emums.
let model_name_as_enum = TopicType::from_str(&model_name).unwrap();
match model_name_as_enum {
TopicType::Problem => {
let problem = Problem::new(id_str, name_str, &["2000"], &["8000"]);
return Some(problem.clone().into());
}
TopicType::Discussion => {
let discussion = Discussion::new(id_str, name_str, &["2000"], &["8000"]);
return Some(discussion.clone().into());
}
}
In this example we are getting a topic from Neo4J graph database.
We then serialize the query response neo4rs::Node
to a GraphQL TopicValue
interface manually.
We then cast the Neo4J node's model_name to a TopicType enum.
We then match against the TopicTypes and construct either a Problem or a
Discussion. Lastly we up cast the Problem/Discussion to being a TopicValue
since that is what
the GraphQL field is expecting as the return type.
use klub_models::topic::topic::{TopicType, TopicValue, TOPIC_MODEL_ALIAS, TOPIC_MODEL_NAME};
...
// Example GraphQL topic field resolver
async fn topic(
#[graphql(context)] ctx: &GraphContext,
#[graphql(description = "id of topic")] id: String,
) -> Option<TopicValue> {
let model_params = ModelParams {
model_name: TOPIC_MODEL_NAME,
model_alias: TOPIC_MODEL_ALIAS,
};
let id_str = &id[..];
// Query neo4j for a topic by id
let topic_node = &ctx.find_one_by_id(model_params, id_str).await.unwrap();
// Convert Neo4J fields to GraphQL fields
let name: String = topic_node.get("name").unwrap();
let name_str = &name[..];
let model_name: String = topic_node.get("model_name").unwrap();
let model_name_as_enum = TopicType::from_str(&model_name).unwrap();
// Cast generic topic to either a Problem or Discussion
match model_name_as_enum {
TopicType::Problem => {
let problem = Problem::new(id_str, name_str, &["2000"], &["8000"]);
// Cast back to TopicValue
return Some(problem.clone().into());
}
TopicType::Discussion => {
let discussion = Discussion::new(id_str, name_str, &["2000"], &["8000"]);
// Cast back to TopicValue
return Some(discussion.clone().into());
}
}
}