Replies: 2 comments 5 replies
-
scenario 1user: root query, dynamic-graphql: types 1- define fn main() {
#[derive(SimpleObject)]
struct Foo {
value: String,
}
struct Query;
impl Register for Query {
fn register(registry: Registry) -> Registry {
let registry = registry.register::<Foo>();
let object = dynamic::Object::new("Query");
let object = object.field(dynamic::Field::new(
"foo",
dynamic::TypeRef::named("Foo"),
|_ctx| {
dynamic::FieldFuture::new(async move {
Ok(Some(dynamic::FieldValue::owned_any(Foo {
value: "the foo".to_string(),
})))
})
},
));
registry.register_type(object).set_root("Query")
}
}
#[derive(App)]
struct App(Query);
let schema = App::create_schema().finish().unwrap();
} Alternative scenario 11- create a registry with use dynamic_graphql::dynamic;
use dynamic_graphql::internal::Registry;
use dynamic_graphql::SimpleObject;
async fn main() {
#[derive(SimpleObject)]
struct Foo {
value: String,
}
let registry = Registry::new().register::<Foo>();
let schema = dynamic::Schema::build("Query", None, None);
let schema = registry.apply_into_schema_builder(schema);
let query = dynamic::Object::new("Query");
let query = query.field(dynamic::Field::new(
"foo",
dynamic::TypeRef::named("Foo"),
|_ctx| {
dynamic::FieldFuture::new(async move {
Ok(Some(dynamic::FieldValue::owned_any(Foo {
value: "the foo".to_string(),
})))
})
},
));
let schema = schema.register(query);
let schema = schema.finish().unwrap();
} |
Beta Was this translation helpful? Give feedback.
-
scenario 2user: some types, dynamic-graphql: root query 1- define a custom type (Foo) use dynamic_graphql::internal::{
Object, OutputTypeName, ParentType, Register, Registry, ResolveOwned, ResolveRef, TypeName,
};
use dynamic_graphql::{dynamic, App, Context, SimpleObject, value, FieldValue};
use std::borrow::Cow;
use dynamic_graphql::dynamic::DynamicRequestExt;
#[tokio::test]
async fn define_custom_type() {
// define Foo type
struct Foo {
value: String,
}
impl TypeName for Foo {
fn get_type_name() -> Cow<'static, str> {
"Foo".into()
}
}
impl OutputTypeName for Foo {}
impl Register for Foo {
fn register(registry: Registry) -> Registry {
let object = dynamic::Object::new("Foo");
let object = object.field(dynamic::Field::new(
"value",
dynamic::TypeRef::named_nn("String"),
|ctx| {
let query: &Foo = ctx.parent_value.downcast_ref().unwrap();
dynamic::FieldFuture::new(async move {
Ok(Some(dynamic::FieldValue::value(query.value.clone())))
})
},
));
registry.register_type(object)
}
}
impl<'a> ResolveOwned<'a> for Foo {
fn resolve_owned(self, _ctx: &Context) -> dynamic_graphql::Result<Option<dynamic::FieldValue<'a>>> {
Ok(Some(dynamic::FieldValue::owned_any(self)))
}
}
impl<'a> ResolveRef<'a> for Foo {
fn resolve_ref(
&'a self,
_ctx: &Context,
) -> dynamic_graphql::Result<Option<dynamic::FieldValue<'a>>> {
Ok(Some(dynamic::FieldValue::borrowed_any(self)))
}
}
// use Foo type
#[derive(SimpleObject)]
#[graphql(root)]
struct Query {
foo: Foo,
}
#[derive(App)]
struct App(Query);
let schema = App::create_schema().finish().unwrap();
// use schema
let sdl = schema.sdl();
assert_eq!(
normalize_schema(&sdl),
normalize_schema(
r#"
type Foo {
value: String!
}
type Query {
foo: Foo!
}
schema {
query: Query
}
"#
),
);
let root = Query {
foo: Foo {
value: "the foo".to_string(),
},
};
let query = "{ foo { value } }";
let req = dynamic_graphql::Request::new(query).root_value(FieldValue::owned_any(root));
let res = schema.execute(req).await;
assert_eq!(res.data, value!({ "foo": { "value": "the foo" } }));
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
dynamic-graphql macros generate codes for structs and enums to create an async-graphql dynamic schema. but sometimes users may want to create async-graphql dynamic type by hand and combine it with dynamic-graphql generated types. 1248
There are several scenarios are possible:
1- The user wants to be responsible for creating a root query and wants to use dynamic-graphql generated types in it.
2- dynamic-graphql is responsible for creating the root query and the user wants to define some custom types and use them in it.
Please vote 👍 on which scenario you want to use and if you have any other scenario please let me know.
Beta Was this translation helpful? Give feedback.
All reactions