wasmcloud_actor_graphdb/lib.rs
1//! GraphDB wasmCloud Actor Interface
2//!
3//! This crate provides an abstraction over the `wasmcloud:graphdb` contract. This
4//! allows actors to interact with a graph database, such as RedisGraph or Neo4j.
5//!
6//! Example:
7//!
8//! ```rust
9//! use serde_json::json;
10//! extern crate wapc_guest as guest;
11//! use wasmcloud_actor_graphdb as graph;
12//! use graph::*;
13//! use wasmcloud_actor_http_server as http;
14//! use wasmcloud_actor_core as actor;
15//!
16//! use guest::prelude::*;
17//!
18//! #[actor::init]
19//! pub fn init() {
20//! http::Handlers::register_handle_request(handle_http_request);
21//! }
22//!
23//! fn handle_http_request(_: http::Request) -> HandlerResult<http::Response> {
24//! let (name, birth_year): (String, u32) = graph::default().query_graph(
25//! "MotoGP".to_string(),
26//! "MATCH (r:Rider)-[:rides]->(t:Team) WHERE t.name = 'Yamaha' RETURN r.name, r.birth_year"
27//! .to_string(),
28//! )?;
29//!
30//! assert_eq!(name, "Alice Rider".to_string());
31//! assert_eq!(birth_year, 1985);
32//!
33//! let result = json!({
34//! "name": name,
35//! "birth_year": birth_year
36//! });
37//!
38//! Ok(http::Response::json(result, 200, "OK"))
39//! }
40//!
41//! ```
42
43pub mod generated;
44#[allow(unused_imports)]
45pub use generated::*;
46#[macro_use]
47#[cfg(feature = "guest")]
48extern crate serde_derive;
49#[cfg(feature = "guest")]
50mod results;
51#[cfg(feature = "guest")]
52pub use results::FromTable;
53#[cfg(feature = "guest")]
54mod conversions;
55#[cfg(feature = "guest")]
56mod errors;
57#[cfg(feature = "guest")]
58type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Sync + Send>>;
59
60#[doc(hidden)]
61#[macro_export]
62#[cfg(feature = "guest")]
63macro_rules! client_type_error {
64 ($($arg:tt)*) => {
65 Err($crate::errors::GraphError::ClientTypeError(format!($($arg)*)))
66 };
67}
68
69#[cfg(feature = "guest")]
70impl Host {
71 pub fn query_graph<T: FromTable>(
72 &self,
73 graph_name: String,
74 query: String,
75 ) -> ::wapc_guest::HandlerResult<T> {
76 let res = self
77 ._query_graph(graph_name.into(), query.into())
78 .map(|res| T::from_table(&res.result_set));
79 match res {
80 Ok(Ok(l)) => Ok(l),
81 _ => Err("Graph conversion error".into()),
82 }
83 }
84}
85
86/// The operation to request a query of graph data
87pub const OP_QUERY: &str = "QueryGraph";
88/// The operation to request the deletion of a graph
89pub const OP_DELETE: &str = "DeleteGraph";