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

new 'object' macro: specify lifetimes for context object #364

Closed
5o50 opened this issue May 21, 2019 · 1 comment
Closed

new 'object' macro: specify lifetimes for context object #364

5o50 opened this issue May 21, 2019 · 1 comment
Labels
duplicate This issue or pull request already exists support

Comments

@5o50
Copy link

5o50 commented May 21, 2019

I'm trying to specify a lifetime for a Context object using the new juniper::object proc macro.

pub struct Context<'a> {
    pub datastore: &'a Datastore
}

impl<'a> juniper::Context for Context<'a> {}

impl<'a> Context<'a> {

    pub fn new(repo: &'a Repo) -> Context<'a> {
        Context {
            datastore: &repo.datastore
        }
    }
}

pub struct Query;

#[juniper::object(context = Context)] // ^^^^^^^ expected lifetime parameter under Context
impl Query {

    fn datastore_version(ctx: &Context) -> String {
        ctx.datastore.get_version()
    }
}

The error is on #[juniper::object(context = Context)] a lifetime specifier is expected for Context.
When trying #[juniper::object(context = Context<'a>)] a ^^ undeclared lifetime for <`a> is returned.

@5o50 5o50 added bug Something isn't working needs-triage labels May 21, 2019
@theduke
Copy link
Member

theduke commented May 21, 2019

This is the same issue as #143. (See especially the very last comment there, and the whole discussion for an explanation)

Below is a solution that works with the new macro.

Note that this might break in the future if you want to switch to async.

pub struct DataStore;

pub struct Context<'a> {
    pub datastore: &'a DataStore
}

impl<'a> juniper::Context for Context<'a> {}

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

#[juniper::object(context = Context<'a>)]
impl<'a> Query<'a> {

    fn datastore_version(ctx: &Context<'a>) -> String {
        "lala".into()
    }
}


fn main() {
  let query = Query{ marker: std::marker::PhantomData };
}

@theduke theduke added duplicate This issue or pull request already exists support and removed bug Something isn't working needs-triage labels May 21, 2019
@theduke theduke closed this as completed May 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists support
Projects
None yet
Development

No branches or pull requests

2 participants