Skip to content

Get context in interface field resolvers #748

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

Closed
stijnfrishert opened this issue Aug 29, 2020 · 4 comments
Closed

Get context in interface field resolvers #748

stijnfrishert opened this issue Aug 29, 2020 · 4 comments

Comments

@stijnfrishert
Copy link
Contributor

One of my interface's fields resolves to another type. I'd like to store that field as an ID and retrieve it from my context upon resolution. Whenever I try this, I get an error saying the trait bound FromInputValue is not satisfied for my context.

struct Event {
    parent: Option<ID>,
}

juniper::graphql_interface!(Event: Context |&self| {
    field parent(context: &Context) -> Option<Event> { ... }

    // ...
}

This leads me to believe this is simply not supported in the current 0.14.2 release. Is this something I should also wait for in the new interface release that's coming up, or am I missing something?

@LegNeato
Copy link
Member

Hmm, I think something similar should work but haven't looked at release version in a long time. After #682 lands this should be fixed on master.

@davidpdrsn
Copy link
Contributor

I don't exactly remember but I don't think you should specify the context argument on the field itself:

juniper::graphql_interface!(Event: Context |&self| {
    field parent() -> Option<Event> { ... }

    // ...
}

That is at least what I do in juniper-from-schema here and here.

@stijnfrishert
Copy link
Contributor Author

stijnfrishert commented Sep 14, 2020

Oh nice, thanks, @davidpdrsn! I'll give that a try :)


@davidpdrsn: Sorry, it was late yesterday, but am I missing something or isn't that the exact same code as in the original post?

In any case, with that code trying to use a context inside the field body I get cannot find value *context* in scope.

@tyranron
Copy link
Member

tyranron commented Oct 6, 2020

This is now possible in master. See description in Book.

@tyranron tyranron closed this as completed Oct 6, 2020
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

No branches or pull requests

4 participants