Provides data structures to expose entity
objects via async-graphql
.
Leveraging macros
feature alongside the entity-inmemory
and futures
crates:
```rust use asyncgraphql::{Context, EmptyMutation, EmptySubscription, Object, Schema}; use entity::*; use entityasyncgraphql::*; use entityinmemory::InmemoryDatabase;
struct User { name: String, age: u8,
#[ent(edge)]
friends: Vec
struct RootQuery;
impl RootQuery {
async fn users(
&self,
ctx: &Context<'>,
filter: GqlUserFilter,
) -> asyncgraphql::Result
db.find_all_typed::<User>(filter.into())
.map_err(|x| async_graphql::Error::new(x.to_string()))
} }
fn main() { // Create an empty database and convert from InmemoryDatabase -> DatabaseRc let db = dbtorc(InmemoryDatabase::default());
// Make a user and write it into the database let user = User::build() .name(String::from("Fred Flintstone")) .age(38) .friends(Vec::new()) .finish() .expect("Built user"); let _ = db.insert_typed(user).expect("Wrote to database");
// Define our GraphQL schema and add our database as context data let schema = Schema::build(RootQuery, EmptyMutation, EmptySubscription) .data(db) .finish();
// Execute a GraphQL query and print the results let res = futures::executor::blockon(schema.execute(r#" { user(filter: { name: { textends_with: "Flintstone" } }) { id name } } "#));
println!("{:#?}", res); } ```
macros
- provides macro support for generating needed
async-graphql
code for new entities