-
-
Notifications
You must be signed in to change notification settings - Fork 565
Use of map() where parent type is a node #6
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
Hey, nice to see that you tried to use This feature was trying to solve the N+1 problem in generic way, so that you could do bulk operations, like Redis But as you discovered So at this point I can say that this feature failed to solve original problem. And I will probably revert it until better generic solution reveals itself. Other GraphQL implementations also discuss this problem. Check out graphql/graphql-js#111 and graphql-dotnet/graphql-dotnet#21 for example. For now you can work around it in the user-land with regular class Value {
/** @var Value[] */
public $parentList;
} Then in your 'fieldName' => [
'resolve' => function (Value $value, $args, $context) {
if (!isset($value->fieldName) {
// Resolve field for all items in list on first resolve call:
$list = $value->parentList ?: [$value];
foreach ($list as $item) {
$item->fieldName = 'something';
}
}
return $value->fieldName;
}
] This is way more ugly than I realize that When good-enough solution will be ready - I will add separate section to README for it. If you have any thoughts on how to make it work for all use-cases - please share your thoughts! |
Thanks for the comment! It seems like it's a rather tough problem to tackle - there really should be a standardized solution to this in GraphQL. For now, your hack works pretty well and solved my problems. I've even experimented with passing down a query builder instance (from my ORM) alongside the parent list - this actually makes it possible to perform |
Closing this as |
Resolving fields with
map
works really well (and fast!) when the parent type is a list. However, when using Relay connections, it's impossible to usemap
on the "connected" type, since its parent is an edge and not the list of edges.An example:
UserType -> todos -> TodoConnection -> edges -> EdgeType -> node -> TodoType -> items*
I want to use
map
onitems
but I cant since the root argument will contain an array with only one element - the edge.Is there any way to work around this, or is it perhaps something you would consider improving?
The text was updated successfully, but these errors were encountered: