{"action":"created","issue":{"url":"https://api.github.com/repos/webonyx/graphql-php/issues/66","repository_url":"https://api.github.com/repos/webonyx/graphql-php","labels_url":"https://api.github.com/repos/webonyx/graphql-php/issues/66/labels{/name}","comments_url":"https://api.github.com/repos/webonyx/graphql-php/issues/66/comments","events_url":"https://api.github.com/repos/webonyx/graphql-php/issues/66/events","html_url":"https://github.com/webonyx/graphql-php/issues/66","id":187289089,"number":66,"title":"Ability to defer field resolution (to solve N+1 problem)","user":{"login":"vladar","id":115628,"avatar_url":"https://avatars.githubusercontent.com/u/115628?v=3","gravatar_id":"","url":"https://api.github.com/users/vladar","html_url":"https://github.com/vladar","followers_url":"https://api.github.com/users/vladar/followers","following_url":"https://api.github.com/users/vladar/following{/other_user}","gists_url":"https://api.github.com/users/vladar/gists{/gist_id}","starred_url":"https://api.github.com/users/vladar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vladar/subscriptions","organizations_url":"https://api.github.com/users/vladar/orgs","repos_url":"https://api.github.com/users/vladar/repos","events_url":"https://api.github.com/users/vladar/events{/privacy}","received_events_url":"https://api.github.com/users/vladar/received_events","type":"User","site_admin":false},"labels":[{"id":236660793,"url":"https://api.github.com/repos/webonyx/graphql-php/labels/enhancement","name":"enhancement","color":"84b6eb","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":10,"created_at":"2016-11-04T09:37:13Z","updated_at":"2016-11-16T02:39:01Z","closed_at":null,"body":"There were several discussions about ways to solve N+1 problem in GraphQL - #42, #60. \r\n\r\nThere are two classes of solutions for this problem:\r\n\r\n1. Look-ahead soltion when we analyze the query and pre-fetch required data upfront (see #65)\r\n2. Defer field resolution (and thus enable batching queries using techniques like [DataLoader](https://github.com/facebook/dataloader))\r\n\r\nDeferring syntax:\r\n\r\n```php\r\n'resolve' => function($obj) {\r\n    $this->dataLoader->buffer($obj->someId);\r\n\r\n    return new GraphQL\\Deferred(function() use ($obj) {\r\n       // This will internally load all buffered ids (once) and return the one requested here:\r\n        return $this->dataLoader->fetch($obj->someId);\r\n    });\r\n}\r\n```\r\n\r\nOther possible syntax:\r\n```php\r\n'buffer' => function($obj) {\r\n    $this->dataLoader->buffer($obj->someId);\r\n},\r\n'resolve' => function($obj) {\r\n    return $this->dataLoader->fetch($obj->someId);\r\n}\r\n```\r\n\r\nTechnically it will require `Executor` algorith change from Depth-First to mix of Breath-First and Depth-First (requires researching and prototyping)."},"comment":{"url":"https://api.github.com/repos/webonyx/graphql-php/issues/comments/260837278","html_url":"https://github.com/webonyx/graphql-php/issues/66#issuecomment-260837278","issue_url":"https://api.github.com/repos/webonyx/graphql-php/issues/66","id":260837278,"user":{"login":"vladar","id":115628,"avatar_url":"https://avatars.githubusercontent.com/u/115628?v=3","gravatar_id":"","url":"https://api.github.com/users/vladar","html_url":"https://github.com/vladar","followers_url":"https://api.github.com/users/vladar/followers","following_url":"https://api.github.com/users/vladar/following{/other_user}","gists_url":"https://api.github.com/users/vladar/gists{/gist_id}","starred_url":"https://api.github.com/users/vladar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vladar/subscriptions","organizations_url":"https://api.github.com/users/vladar/orgs","repos_url":"https://api.github.com/users/vladar/repos","events_url":"https://api.github.com/users/vladar/events{/privacy}","received_events_url":"https://api.github.com/users/vladar/received_events","type":"User","site_admin":false},"created_at":"2016-11-16T02:39:01Z","updated_at":"2016-11-16T02:39:01Z","body":"@shadowhand Simple implementation with `JsonSerializable` won't work. After calling `resolve` value is passed to nested object resolvers. In this case - your deferred value with no data, which will actually emit `null` for all nested fields."}}
{"actor":{"display_login":"vladar"}}