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

Trouble Defining Lifetime on Context with Juniper and Rocket Async #850

Open
JonRCahill opened this issue Jan 18, 2021 · 2 comments
Open
Assignees
Labels
k::integration Related to integration with third-party libraries or systems lib::rocket Related to `rocket` crate integration support
Milestone

Comments

@JonRCahill
Copy link

JonRCahill commented Jan 18, 2021

I need to define a lifetime on my Context so I can gain access to Cookies in Rocket, but after defining the lifetime I get a compile error on the get_graphql_handler request handler.

A simplified version (without the Cookies) which has the same compile error:

pub struct Context<'a> {
    _marker: std::marker::PhantomData<&'a ()>,
}
impl<'a> Context<'a> {
    pub async fn get_something(&self) -> FieldResult<String> {
        Ok("Something".into())
    }
}
impl<'a> juniper::Context for Context<'a> {}

pub struct QueryRoot<'a> {
    _marker: std::marker::PhantomData<&'a ()>,
}

#[graphql_object(Context = Context<'a>)]
impl<'a> QueryRoot<'a> {
    async fn something(context: &Context<'a>) -> FieldResult<String> {
        context.get_something().await
    }
}

pub type Schema<'a> = juniper::RootNode<
  'static,
  QueryRoot<'a>,
  EmptyMutation<Context<'a>>,
  EmptySubscription<Context<'a>>,
>;

#[get("/graphql?<request>")]
pub async fn get_graphql_handler<'a>(
    request: juniper_rocket_async::GraphQLRequest,
    schema: State<'_, Schema<'a>>,
    context: Context<'a>,
) -> juniper_rocket_async::GraphQLResponse {
    request.execute(&schema, &context).await
}

Results in the following compile errors:


error[E0308]: mismatched types
  --> server/src/graphql/handlers.rs:12:1
   |
12 | #[get("/graphql?<request>")]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected type `context::Context<'_>`
            found struct `context::Context<'_>`
   = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0621]: explicit lifetime required in the type of `__req`
  --> server/src/graphql/handlers.rs:12:1
   |
12 | #[get("/graphql?<request>")]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   | |
   | lifetime `'_b` required
   | help: add explicit lifetime `'_b` to the type of `__req`: `&'_b rocket::Request<'_b>`
   |
   = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

I have created an example here: https://github.com/JonRCahill/juniper_context_lifetimes

Any ideas what I need to do to get this to compile?

@GavinMendelGleason
Copy link

GavinMendelGleason commented Oct 6, 2022

Presumably the macrology does not understand the lifetime parameter, but I have a similar need to carry a lifetime and would like to avoid having to do everything using the dynamic approach in order to make it work. Any known work-arounds here?

@tyranron tyranron self-assigned this Oct 7, 2022
@tyranron tyranron added this to the 0.16.0 milestone Oct 7, 2022
@tyranron tyranron added lib::rocket Related to `rocket` crate integration k::integration Related to integration with third-party libraries or systems labels Oct 7, 2022
@tyranron
Copy link
Member

tyranron commented Oct 7, 2022

@JonRCahill @GavinMendelGleason this topic has been araised multiple times already across issues (#143, #105, #364). Unfortunately, seems to be no handy way to have any lifetime parameters in Context type at the moment. As far as I recall, the issue is not that much with juniper macros expansion, but rather with some core type machinery or web-framework integration crates not being able to extract a Context with lifetime from web-framework's request context. Try to do the manual extraction Context<'a> inside the handler, not requiring it as a function argument.

@tyranron tyranron modified the milestones: 0.16.0, 0.17.0 Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
k::integration Related to integration with third-party libraries or systems lib::rocket Related to `rocket` crate integration support
Projects
None yet
Development

No branches or pull requests

3 participants