Skip to content

How to get parent resolved object in deeply nested resolver? #1244

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
AndrewRayCode opened this issue Jan 26, 2018 · 8 comments
Closed

How to get parent resolved object in deeply nested resolver? #1244

AndrewRayCode opened this issue Jan 26, 2018 · 8 comments

Comments

@AndrewRayCode
Copy link

AndrewRayCode commented Jan 26, 2018

I have a query like:

user(id: 1) {
    company {
        userProfile
    }
}

The company resolver looks something like:

class CompanyResolver
   def call(user_obj, args, ctx)
       return user_obj.company
    end
end

Because it's a child of the :user field directly, it can access the resolved user (which is good, but...)

The userProfile resolver also needs the user model.

class UserProfileResolver
   def call(company_obj, args, ctx)
       return company_obj.some_method(user.id).profile
       # How do I access the resolved user in the above line?
   end
end

obj in this second resolver is the company, which won't have a reference to the user. ctx.ast_node seems to have no way to walk up to the parent (there's no .parent method) so I'm not sure how to use a higher resolved obj in a lower resolver?

@rmosolgo
Copy link
Owner

TL;DR:

parent_ctx = ctx.parent 
parent_obj = parent_ctx.object 
# => company 

ctx is part of the response tree, so you can use it to backtrack to the previous response node and get the object from it. It's an instance of GraphQL::Query::FieldResolutionContext.

@laasem
Copy link

laasem commented Mar 11, 2019

@rmosolgo How can one access the parent object in the new class-based API?

@laasem
Copy link

laasem commented Mar 11, 2019

@rmosolgo Never mind, found my answer in #881 (comment), thanks!

@rmosolgo
Copy link
Owner

1.9.17 will introduce "scoped" context, a part of context which is added for child fields only #2634

You could ctx.scoped_set(:found_user, user), and then ctx[:found_user] would return that user, but only for subselections of the user(id: 1) field (including "grandchildren")!

@laasem
Copy link

laasem commented Jan 30, 2020

Awesome, thank you!

@ethomson1
Copy link

@rmosolgo Scoped context is marked as @api private. Generally, the best practice is to shy away from using private APIs because new versions are more likely to introduce breaking changes. How stable is this feature? Are there plans to make scoped context part of the public API?

@rmosolgo
Copy link
Owner

Sorry, it really shouldn't be marked @api private. It's 100% stable (added by Shopify and I think they depend on it, also 100% generally useful). If you don't mind, a PR to remove that tag (or just a link to where you saw it) would be great. Also so some docs for graphql-ruby.org, maybe one of these days 😅 !

@ethomson1
Copy link

@rmosolgo The tag is here, and it is also marked as private in the Ruby docs.

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