-
Notifications
You must be signed in to change notification settings - Fork 1.4k
How can I get the parent fields from ctx? #881
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
Comments
There's not a way to get parent objects from the resolve function :S Options I can think of are:
I can't think of any more :S |
@gottfrois I found that you can get an Array of single There's no way to get a true |
If |
def self.get_parent_argument_from_ctx(ctx, name)
arg = ctx.irep_node.parent.ast_node.arguments.find { |a| a.name == name }
arg ? arg.value : nil
end |
I don't recommend going through the AST -- for example, if a client uses a variable, you'll get the name of the variable instead of a value. You can backtrack def self.get_parent_argument_from_ctx(ctx, name)
parent_node = ctx.irep_node.parent
parent_args = parent_node.arguments
# ^ Arguments
parent_args[name]
end |
The above also does not solve the original issue since I'm looking for the resolved value for the Is there a way to get the resolved value from the ast? |
I can't think of any way to backtrack and get that value :( |
Thanks, @rmosolgo! |
#923 will add support for backtracking via context.parent.value
# #<User ...> |
@gottfrois until #923 fixes this issue, what I've done is create a simple wrapper object that allows me to easily pass parents down like so:
In your case, instead of grabbing an
The resulting Obviously the additional objects add some overhead, but depending on your use case, this could be an easy solution. |
Thanks @thefliik for the tip! |
This will be supported in 1.7.0, where you can access
|
Is it still possible to access |
No, those objects aren't passed in anymore. Since we're working on an object-by-object basis, there's no good way to inject those into each method without adding a lot of boilerplate. In the meantime, attributes of I'm not sure about the long-term future here, so if you want to share something about your use case, please do, and I'll keep it in mind! |
I think that will probably solve the issue, I'm also just trying to climb back up the chain to access my root object from a deeply nested one. The nested object has a computed value that's created from a combination of itself and the parent object, something kind of like this:
Where Thanks for responding so quickly and thanks for this great library! |
Also, based on this comment it sounds like maybe what I want to do is actually discouraged in GraphQL, but that I could also probably just use the |
Yeah, I agree here:
Another design would allow a query like this: user(id: 1) {
favoriteBooks {
isAuthor
book {
title
isbn
}
}
} where But of course, sometimes we really need |
That makes sense. I was hoping to do a drop-in replacement for our existing REST API, but in a lot of places it's really tailored to the client in ways that GraphQL probably can't or shouldn't be. The whole point of this exercise is to create something that works across multiple clients and multiple use-contexts though so it probably makes sense to revisit how some of the data is presented. Thanks again for the help 😄 |
Hey @rmosolgo, regarding
I'm experimenting with schema stitching in graphql-ruby using graphql-remote_loader (which you helpfully linked to in #1812 👍) and prisma. The second graphql API I'm sending queries to is a largely identical to my graphql-ruby schema, so for the fields I want to stitch together, I simply "foward" the query on to the second API. This is accomplished by building up the second query piece by piece, merging all the pieces together, and sending it off. i.e. Or, the more complex scenerio
And this works! But.... And maybe you can see where I'm going with this, but in order to accomplish this from a deeply nested field, I need to crawl up the query tree and build the query string. (i.e. for the friends At the moment, the If the context let me enter the query tree at the current, deeply nested location however, then I could crawl up it nice and easy. Hopefully this makes sense. Any suggestions? Definitely understand if this is just outside the scope of graphql-ruby at this point. UpdateAt the moment, I think the best way to handle this situation is to
Having tested this, it works. |
It's been a while, but #2634 adds "scoped" context, a hash of values which are only available to child selections of the field where they were added. For example, if you could set |
Hey guys,
I was looking for a way to get the parent objects from the
ctx
variable but could not find anything. Here is an example:The `pricing resolver look like that:
How can I get the
user
from theuser(id: 1)
root query?Thank you!
The text was updated successfully, but these errors were encountered: