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
Access parent data from child resolver #52
Comments
I don't know much about sequelize, but in GraphQL the way to do this would be to pass the parent to the child by including it in the resolved value. Something like this:
|
Thanks @helfer |
@helfer so I tried your suggestion, but it did not work as expected. Children resolver is expected to return an array of I did manage to get it working with context though ...
Here's my solution in case someone else has similar question.
Is this OK, or is there a better way of doing it? BTW is this related to the Loader/Connector API I've been reading about in #21? |
@HriBB: Passing it through the context is probably not a great solution, because the context is shared by all resolvers, and you have no control over the order in which they run. Reading this again, I'm not sure why passing the location object is not enough? Aren't you just calling a function on the location? If you pass the sequelize model of that location, it should just work.
|
@helfer yeah I figured that the order of resolvers would be the problem with the context. I can attach parent to each child like this
It would be cool if things like this would be explained in the docs. Are there any examples? |
@HriBB I think this is basically how resolvers work. Maybe we can add a paragraph that explains that? I wrote a medium post about a week ago that could be used as a starting point: https://medium.com/apollo-stack/graphql-explained-5844742f195e |
@helfer great reading, we need more articles like this.
This is exactly what I need! Can't wait for another post :) And just to clarify the above quote ... by backend you mean database GraphQL server? I would love to see some more complex examples on this topic. I can help with that, but first I need to understand how things work. Problem with apollo-server is that it is so f***ing simple to set up, that I skipped the reading part and went straight to the implementation and now I wanna do complex stuff without understanding the basics :P Need to do some research ... Can you recommend some good articles? Let's say I have a simple GraphQL query like this:
First I fetch all posts, then for each post fetch the author. So if I have 100 posts, obviously I don't want to run 100 queries for each post author, I wanna batch them together, either with a Furthermore, what if some resolvers "depend" on other resolvers? I know that this is not possible, but in my case, I need it. I have a hierarchy like this
I want to be able to generate urls on the server, and this is where things get complicated. When I try to resolve the image url for example, I need all the players: Anyway, thanks for your help @helfer. I will stop bugging you now, as I know you have better work to do ;) |
This issue offers some good reading |
And there's the DataLoader :) |
@HriBB Unfortunately there's not that much good advanced reading material out there for GraphQL yet. We're working on that. The thing dataloader helps you do is not create queries with JOIN in them, but to batch togeter all requests at the same level. So say you want to get a list of N posts and info about all their authors. Potentially you could be doing N + 1 queries. With a JOIN you could do one (bigger and slower) query. With proper batching, you'll be doing two queries. One for the post and one for the N authors. Depending on what the latency of your database is, the 2 queries can actually be more efficient than the large JOIN query. It's hard to know for sure though and depends on the situation, which is why we're building a tool called tracer that will collect data and help people figure out how to make their server more efficient. |
Hello
I am trying out Apollostack and so far it's been great! I have a problem though :)
This question might be better asked on sequelize forums, but maybe there's some apollo feature I am not aware of ...
I have a sequelize model with parent->children relation on the same table, and I need to access parent data from inside the child's resolve functions or from inside sequelize's instance methods.
This is my sequelize model Location
And this are my resolve functions
What would be the best way to do this?
This is the solution I have come up with ... I manually inject parent data into child.
The text was updated successfully, but these errors were encountered: