Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic schema: add boxed_any function #1179

Merged
merged 3 commits into from Dec 21, 2022

Conversation

smmoosavi
Copy link
Contributor

Sometimes we have Box<dyn Any + Send + Sync>. in these cases the boxed_any function can help.

also borrowed_any changed from borrowed_any(obj: &'a (impl Any + Send + Sync)) to borrowed_any(obj: &'a (dyn Any + Send + Sync)) to remove sized constraint.

my use case:

pub enum AnyBox<'a> {
    Owned(Box<dyn Any + Send + Sync>, String),
    Borrowed(&'a (dyn Any + Send + Sync), String),
}

impl<'a> AnyBox<'a> {
    pub fn new_owned<T: Any + Send + Sync>(value: T, ty: String) -> Self {
        Self::Owned(Box::new(value), ty)
    }
    pub fn new_borrowed<T: Any + Send + Sync + Sized>(value: &'a T, ty: String) -> Self {
        Self::Borrowed(value, ty)
    }
}

impl<'a> ResolveOwned<'a> for AnyBox<'a> {
    fn resolve_owned(self, _ctx: &Context) -> async_graphql::Result<Option<FieldValue<'a>>> {
        match self {
            AnyBox::Owned(obj, name) => Ok(Some(FieldValue::boxed_any(obj).with_type(name))),
            AnyBox::Borrowed(obj, name) => Ok(Some(FieldValue::borrowed_any(obj).with_type(name))),
        }
    }
}

you can see example in here and here

@sunli829 sunli829 merged commit d3e12fc into async-graphql:master Dec 21, 2022
@sunli829
Copy link
Collaborator

Thanks! 🙂

@smmoosavi smmoosavi deleted the boxed-any branch December 21, 2022 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants