Skip to content

Get context in interface field resolvers #748

Description

@stijnfrishert

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?

Activity

  1. LegNeato commented on Sep 10, 2020

    @LegNeato
    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.

  2. davidpdrsn commented on Sep 14, 2020

    @davidpdrsn
    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.

  3. stijnfrishert commented on Sep 14, 2020

    @stijnfrishert
    ContributorAuthor

    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.

  4. tyranron commented on Oct 6, 2020

    @tyranron
    Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions