I try to use DataLoader with async-graphq.
But I got a lifetime problem that I do not understand.
The profile of the function load is :
/// Trait for batch loading.
#[cfg_attr(feature = "boxed-trait", async_trait::async_trait)]
pub trait Loader<K: Send + Sync + Hash + Eq + Clone + 'static>: Send + Sync + 'static {
/// type of value.
type Value: Send + Sync + Clone + 'static;
/// Type of error.
type Error: Send + Clone + 'static;
/// Load the data set specified by the `keys`.
#[cfg(feature = "boxed-trait")]
async fn load(&self, keys: &[K]) -> Result<HashMap<K, Self::Value>, Self::Error>;
And I try to use String or SomeStruct(String) as keys...
#[async_trait]
impl Loader<String> for ParameteByIdLoader {
type Value = Parameter;
type Error = async_graphql::Error;
async fn load(
&self,
ids: &[String],
) -> Result<HashMap<String, Self::Value>, Self::Error> {
...
I have the following error :
error[E0195]: lifetime parameters or bounds on method `load` do not match the trait declaration
...
|
49 | async fn load(
| ______________^
50 | | &self,
51 | | ids: &[String],
52 | | ) -> Result<HashMap<String, Self::Value>, Self::Error> {
| |_____^ lifetimes do not match method in trait
Can you help me on what usage I am wrong, please ?
I discover maybe I need to enable this feature : boxed-trait Enables async-trait for all traits.
So I update my Cargo.toml.
I continue, seeing other problem in this way.
But I am lost sometimes how to know that we need to enable or not some feature... Not so easy or I miss something in documentation...
/// Load the data set specified by the `keys`.
#[cfg(feature = "boxed-trait")]
async fn load(&self, keys: &[K]) -> Result<HashMap<K, Self::Value>, Self::Error>;
/// Load the data set specified by the `keys`.
#[cfg(not(feature = "boxed-trait"))]
fn load(
&self,
keys: &[K],
) -> impl Future<Output = Result<HashMap<K, Self::Value>, Self::Error>> + Send;
It's bad design and not your fault. Features are suppose to be additive, but in this case a feature changes the API. Programs with the feature are incompatible to programs without the feature. They seem marginally aware of the issue.
Have you figured out if you want to use the feature or not? And are you still getting an error? After some playing around, I think you want to use #[async_trait] if you're using boxed-trait, and don't if you're not.