Dynamic Relationships
Dynamic relationships are similiar to Dynamic Props. Instead of returning values contained in the database, Dynamic relationships allows values to be computed at request time.
Usage
1. Mark rel as dynamic by setting the resolver field
model:
- name: User
props:
- name: name
type: String
- name: Project
props:
- name: name
type: String
rels:
- name: top_contributor
nodes: [User]
resolver: resolve_project_top_contributor
";
2. Define custom logic that resolve the prop value
facade: ResolverFacade<AppRequestContext>,
) -> BoxFuture<ExecutionResult> {
Box::pin(async move {
// create dynamic dst node
let mut top_contributor_props = HashMap::<String, Value>::new();
top_contributor_props.insert(
"id".to_string(),
Value::from(Uuid::new_v4().to_hyphenated().to_string()),
);
top_contributor_props.insert("name".to_string(), Value::from("user0".to_string()));
let top_contributor = facade.node("User", top_contributor_props);
// create dynamic rel
let rel_id = "1234567890".to_string();
let top_contributor_rel =
facade.create_rel_with_dst_node(Value::from(rel_id), None, top_contributor)?;
facade.resolve_rel(&top_contributor_rel).await
})
}
3. Add the custom relationship resolver to the engine
let mut resolvers = Resolvers::<AppRequestContext>::new();
resolvers.insert(
"resolve_project_top_contributor".to_string(),
Box::new(resolve_project_top_contributor),
);
// create warpgrapher engine
let engine: Engine<AppRequestContext> = Engine::new(config, db)
.with_resolvers(resolvers)
.build()
.expect("Failed to build engine");