{"action":"created","comment":{"url":"https://api.github.com/repos/Shopify/graphql-batch/pulls/comments/51937374","id":51937374,"diff_hunk":"@@ -1,57 +1,62 @@\n module GraphQL::Batch\n   class ExecutionStrategy < GraphQL::Query::SerialExecution\n-    attr_reader :batched_queries\n-\n-    def initialize\n-      @batched_queries = Hash.new{ |hash, key| hash[key] = [] }\n-    end\n-\n     class OperationResolution < GraphQL::Query::SerialExecution::OperationResolution\n       def result\n-        result = super\n-        until execution_strategy.batched_queries.empty?\n-          queries = execution_strategy.batched_queries.shift.last\n-          queries.first.class.execute(queries)\n-        end\n-        result\n+        GraphQL::Batch.wait(super)\n+      ensure\n+        GraphQL::Batch::Executor.current.clear\n       end\n     end\n \n     class SelectionResolution < GraphQL::Query::SerialExecution::SelectionResolution\n       def result\n-        result_hash = super\n-        result_hash.each do |key, value|\n-          if value.is_a?(FieldResolution)\n-            value.result_hash = result_hash\n+        lazy_resolve_object(super)\n+      end\n+\n+      private\n+\n+      def lazy_resolve_object(obj)\n+        queries = nil\n+        case obj\n+        when Array\n+          array = obj\n+          array.each_with_index do |value, i|\n+            if query = lazy_key_assign(array, i, value)\n+              (queries ||= []) << query\n+            end\n+          end\n+        when Hash\n+          hash = obj\n+          hash.each do |key, value|\n+            if query = lazy_key_assign(hash, key, value)\n+              (queries ||= []) << query\n+            end\n+          end\n+        end\n+        queries ? GraphQL::Batch.all(queries).then { obj } : obj\n+      end\n+\n+      def lazy_key_assign(obj, key, value)\n+        value = lazy_resolve_object(value)\n+        if value.is_a?(Promise)\n+          obj[key] = nil\n+          query = value.then do |result|\n+            obj[key] = result\n           end\n         end\n-        result_hash\n+        query\n       end\n     end\n \n     class FieldResolution < GraphQL::Query::SerialExecution::FieldResolution\n-      attr_accessor :result_hash\n-\n       def get_finished_value(raw_value)\n-        if raw_value.is_a?(QueryContainer)\n-          raw_value.query_listener = self\n-          register_queries(raw_value)\n-          self\n+        if raw_value.is_a?(Promise)\n+          raw_value.then(->(result) { super(result) },\n+                         ->(exc) { exc.is_a?(GraphQL::ExecutionError) ? super(exc) : raise(exc) })","path":"lib/graphql/batch/execution_strategy.rb","position":78,"original_position":78,"commit_id":"595c574eb7a7576f121ba718e2febb84ee3604d9","original_commit_id":"595c574eb7a7576f121ba718e2febb84ee3604d9","user":{"login":"eapache","id":1706826,"avatar_url":"https://avatars.githubusercontent.com/u/1706826?v=3","gravatar_id":"","url":"https://api.github.com/users/eapache","html_url":"https://github.com/eapache","followers_url":"https://api.github.com/users/eapache/followers","following_url":"https://api.github.com/users/eapache/following{/other_user}","gists_url":"https://api.github.com/users/eapache/gists{/gist_id}","starred_url":"https://api.github.com/users/eapache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eapache/subscriptions","organizations_url":"https://api.github.com/users/eapache/orgs","repos_url":"https://api.github.com/users/eapache/repos","events_url":"https://api.github.com/users/eapache/events{/privacy}","received_events_url":"https://api.github.com/users/eapache/received_events","type":"User","site_admin":false},"body":"exception-handling in promises makes my head hurt... the `raise` case will just re-set the error condition on the promise, while the `super` case will end up setting the exception itself in the response hash right? Why are we doing that?","created_at":"2016-02-04T21:07:10Z","updated_at":"2016-02-04T21:07:10Z","html_url":"https://github.com/Shopify/graphql-batch/pull/3#discussion_r51937374","pull_request_url":"https://api.github.com/repos/Shopify/graphql-batch/pulls/3","_links":{"self":{"href":"https://api.github.com/repos/Shopify/graphql-batch/pulls/comments/51937374"},"html":{"href":"https://github.com/Shopify/graphql-batch/pull/3#discussion_r51937374"},"pull_request":{"href":"https://api.github.com/repos/Shopify/graphql-batch/pulls/3"}}},"pull_request":{"url":"https://api.github.com/repos/Shopify/graphql-batch/pulls/3","id":58325996,"html_url":"https://github.com/Shopify/graphql-batch/pull/3","diff_url":"https://github.com/Shopify/graphql-batch/pull/3.diff","patch_url":"https://github.com/Shopify/graphql-batch/pull/3.patch","issue_url":"https://api.github.com/repos/Shopify/graphql-batch/issues/3","number":3,"state":"open","locked":false,"title":"Use Promise.rb to provide a standard promise based API.","user":{"login":"dylanahsmith","id":954402,"avatar_url":"https://avatars.githubusercontent.com/u/954402?v=3","gravatar_id":"","url":"https://api.github.com/users/dylanahsmith","html_url":"https://github.com/dylanahsmith","followers_url":"https://api.github.com/users/dylanahsmith/followers","following_url":"https://api.github.com/users/dylanahsmith/following{/other_user}","gists_url":"https://api.github.com/users/dylanahsmith/gists{/gist_id}","starred_url":"https://api.github.com/users/dylanahsmith/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dylanahsmith/subscriptions","organizations_url":"https://api.github.com/users/dylanahsmith/orgs","repos_url":"https://api.github.com/users/dylanahsmith/repos","events_url":"https://api.github.com/users/dylanahsmith/events{/privacy}","received_events_url":"https://api.github.com/users/dylanahsmith/received_events","type":"User","site_admin":false},"body":"@eapache for review\r\ncc @rmosolgo\r\nCloses #2 \r\n\r\nThis addresses the same issues as pull #2 by providing GraphQL::Batch.wait can be used to wait for and get the result of a query.  However, in this pull request I am using [promise.rb gem](https://rubygems.org/gems/promise.rb) to try to simplify the code and provide a more standard API.\r\n\r\nI have also switched away from using `CustomQuery.new(*args)` for creating a query object to using `CustomLoader.for(*group_args).load(key)`.  This is because we now need to register the query with an executor for grouping, which I didn't want to do in `GraphQL::Batch::Query.new`.  Instead, we get a loader from the executor using the group args, or create and register the loader if it doesn't already exist.\r\n\r\nIn the future we could split the batch loading into a separate gem, so graphql-batch only provides the integration with graphql-ruby.  We could also allow a custom executor be used so that data can be loaded asynchronously or concurrently.","created_at":"2016-02-04T18:12:21Z","updated_at":"2016-02-04T21:07:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b48fcf79f2d539d32ab0bcde81ef97052cc1b1bd","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/Shopify/graphql-batch/pulls/3/commits","review_comments_url":"https://api.github.com/repos/Shopify/graphql-batch/pulls/3/comments","review_comment_url":"https://api.github.com/repos/Shopify/graphql-batch/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Shopify/graphql-batch/issues/3/comments","statuses_url":"https://api.github.com/repos/Shopify/graphql-batch/statuses/595c574eb7a7576f121ba718e2febb84ee3604d9","head":{"label":"Shopify:promise","ref":"promise","sha":"595c574eb7a7576f121ba718e2febb84ee3604d9","user":{"login":"Shopify","id":8085,"avatar_url":"https://avatars.githubusercontent.com/u/8085?v=3","gravatar_id":"","url":"https://api.github.com/users/Shopify","html_url":"https://github.com/Shopify","followers_url":"https://api.github.com/users/Shopify/followers","following_url":"https://api.github.com/users/Shopify/following{/other_user}","gists_url":"https://api.github.com/users/Shopify/gists{/gist_id}","starred_url":"https://api.github.com/users/Shopify/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Shopify/subscriptions","organizations_url":"https://api.github.com/users/Shopify/orgs","repos_url":"https://api.github.com/users/Shopify/repos","events_url":"https://api.github.com/users/Shopify/events{/privacy}","received_events_url":"https://api.github.com/users/Shopify/received_events","type":"Organization","site_admin":false},"repo":{"id":42800785,"name":"graphql-batch","full_name":"Shopify/graphql-batch","owner":{"login":"Shopify","id":8085,"avatar_url":"https://avatars.githubusercontent.com/u/8085?v=3","gravatar_id":"","url":"https://api.github.com/users/Shopify","html_url":"https://github.com/Shopify","followers_url":"https://api.github.com/users/Shopify/followers","following_url":"https://api.github.com/users/Shopify/following{/other_user}","gists_url":"https://api.github.com/users/Shopify/gists{/gist_id}","starred_url":"https://api.github.com/users/Shopify/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Shopify/subscriptions","organizations_url":"https://api.github.com/users/Shopify/orgs","repos_url":"https://api.github.com/users/Shopify/repos","events_url":"https://api.github.com/users/Shopify/events{/privacy}","received_events_url":"https://api.github.com/users/Shopify/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Shopify/graphql-batch","description":"A query batching executor for the graphql gem","fork":false,"url":"https://api.github.com/repos/Shopify/graphql-batch","forks_url":"https://api.github.com/repos/Shopify/graphql-batch/forks","keys_url":"https://api.github.com/repos/Shopify/graphql-batch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Shopify/graphql-batch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Shopify/graphql-batch/teams","hooks_url":"https://api.github.com/repos/Shopify/graphql-batch/hooks","issue_events_url":"https://api.github.com/repos/Shopify/graphql-batch/issues/events{/number}","events_url":"https://api.github.com/repos/Shopify/graphql-batch/events","assignees_url":"https://api.github.com/repos/Shopify/graphql-batch/assignees{/user}","branches_url":"https://api.github.com/repos/Shopify/graphql-batch/branches{/branch}","tags_url":"https://api.github.com/repos/Shopify/graphql-batch/tags","blobs_url":"https://api.github.com/repos/Shopify/graphql-batch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Shopify/graphql-batch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Shopify/graphql-batch/git/refs{/sha}","trees_url":"https://api.github.com/repos/Shopify/graphql-batch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Shopify/graphql-batch/statuses/{sha}","languages_url":"https://api.github.com/repos/Shopify/graphql-batch/languages","stargazers_url":"https://api.github.com/repos/Shopify/graphql-batch/stargazers","contributors_url":"https://api.github.com/repos/Shopify/graphql-batch/contributors","subscribers_url":"https://api.github.com/repos/Shopify/graphql-batch/subscribers","subscription_url":"https://api.github.com/repos/Shopify/graphql-batch/subscription","commits_url":"https://api.github.com/repos/Shopify/graphql-batch/commits{/sha}","git_commits_url":"https://api.github.com/repos/Shopify/graphql-batch/git/commits{/sha}","comments_url":"https://api.github.com/repos/Shopify/graphql-batch/comments{/number}","issue_comment_url":"https://api.github.com/repos/Shopify/graphql-batch/issues/comments{/number}","contents_url":"https://api.github.com/repos/Shopify/graphql-batch/contents/{+path}","compare_url":"https://api.github.com/repos/Shopify/graphql-batch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Shopify/graphql-batch/merges","archive_url":"https://api.github.com/repos/Shopify/graphql-batch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Shopify/graphql-batch/downloads","issues_url":"https://api.github.com/repos/Shopify/graphql-batch/issues{/number}","pulls_url":"https://api.github.com/repos/Shopify/graphql-batch/pulls{/number}","milestones_url":"https://api.github.com/repos/Shopify/graphql-batch/milestones{/number}","notifications_url":"https://api.github.com/repos/Shopify/graphql-batch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Shopify/graphql-batch/labels{/name}","releases_url":"https://api.github.com/repos/Shopify/graphql-batch/releases{/id}","deployments_url":"https://api.github.com/repos/Shopify/graphql-batch/deployments","created_at":"2015-09-20T04:57:22Z","updated_at":"2016-01-20T18:32:10Z","pushed_at":"2016-02-04T18:12:22Z","git_url":"git://github.com/Shopify/graphql-batch.git","ssh_url":"git@github.com:Shopify/graphql-batch.git","clone_url":"https://github.com/Shopify/graphql-batch.git","svn_url":"https://github.com/Shopify/graphql-batch","homepage":null,"size":29,"stargazers_count":13,"watchers_count":13,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":2,"forks":2,"open_issues":2,"watchers":13,"default_branch":"master"}},"base":{"label":"Shopify:master","ref":"master","sha":"c8429fb0a086996a33093afb3326b763e8de65e2","user":{"login":"Shopify","id":8085,"avatar_url":"https://avatars.githubusercontent.com/u/8085?v=3","gravatar_id":"","url":"https://api.github.com/users/Shopify","html_url":"https://github.com/Shopify","followers_url":"https://api.github.com/users/Shopify/followers","following_url":"https://api.github.com/users/Shopify/following{/other_user}","gists_url":"https://api.github.com/users/Shopify/gists{/gist_id}","starred_url":"https://api.github.com/users/Shopify/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Shopify/subscriptions","organizations_url":"https://api.github.com/users/Shopify/orgs","repos_url":"https://api.github.com/users/Shopify/repos","events_url":"https://api.github.com/users/Shopify/events{/privacy}","received_events_url":"https://api.github.com/users/Shopify/received_events","type":"Organization","site_admin":false},"repo":{"id":42800785,"name":"graphql-batch","full_name":"Shopify/graphql-batch","owner":{"login":"Shopify","id":8085,"avatar_url":"https://avatars.githubusercontent.com/u/8085?v=3","gravatar_id":"","url":"https://api.github.com/users/Shopify","html_url":"https://github.com/Shopify","followers_url":"https://api.github.com/users/Shopify/followers","following_url":"https://api.github.com/users/Shopify/following{/other_user}","gists_url":"https://api.github.com/users/Shopify/gists{/gist_id}","starred_url":"https://api.github.com/users/Shopify/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Shopify/subscriptions","organizations_url":"https://api.github.com/users/Shopify/orgs","repos_url":"https://api.github.com/users/Shopify/repos","events_url":"https://api.github.com/users/Shopify/events{/privacy}","received_events_url":"https://api.github.com/users/Shopify/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Shopify/graphql-batch","description":"A query batching executor for the graphql gem","fork":false,"url":"https://api.github.com/repos/Shopify/graphql-batch","forks_url":"https://api.github.com/repos/Shopify/graphql-batch/forks","keys_url":"https://api.github.com/repos/Shopify/graphql-batch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Shopify/graphql-batch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Shopify/graphql-batch/teams","hooks_url":"https://api.github.com/repos/Shopify/graphql-batch/hooks","issue_events_url":"https://api.github.com/repos/Shopify/graphql-batch/issues/events{/number}","events_url":"https://api.github.com/repos/Shopify/graphql-batch/events","assignees_url":"https://api.github.com/repos/Shopify/graphql-batch/assignees{/user}","branches_url":"https://api.github.com/repos/Shopify/graphql-batch/branches{/branch}","tags_url":"https://api.github.com/repos/Shopify/graphql-batch/tags","blobs_url":"https://api.github.com/repos/Shopify/graphql-batch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Shopify/graphql-batch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Shopify/graphql-batch/git/refs{/sha}","trees_url":"https://api.github.com/repos/Shopify/graphql-batch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Shopify/graphql-batch/statuses/{sha}","languages_url":"https://api.github.com/repos/Shopify/graphql-batch/languages","stargazers_url":"https://api.github.com/repos/Shopify/graphql-batch/stargazers","contributors_url":"https://api.github.com/repos/Shopify/graphql-batch/contributors","subscribers_url":"https://api.github.com/repos/Shopify/graphql-batch/subscribers","subscription_url":"https://api.github.com/repos/Shopify/graphql-batch/subscription","commits_url":"https://api.github.com/repos/Shopify/graphql-batch/commits{/sha}","git_commits_url":"https://api.github.com/repos/Shopify/graphql-batch/git/commits{/sha}","comments_url":"https://api.github.com/repos/Shopify/graphql-batch/comments{/number}","issue_comment_url":"https://api.github.com/repos/Shopify/graphql-batch/issues/comments{/number}","contents_url":"https://api.github.com/repos/Shopify/graphql-batch/contents/{+path}","compare_url":"https://api.github.com/repos/Shopify/graphql-batch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Shopify/graphql-batch/merges","archive_url":"https://api.github.com/repos/Shopify/graphql-batch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Shopify/graphql-batch/downloads","issues_url":"https://api.github.com/repos/Shopify/graphql-batch/issues{/number}","pulls_url":"https://api.github.com/repos/Shopify/graphql-batch/pulls{/number}","milestones_url":"https://api.github.com/repos/Shopify/graphql-batch/milestones{/number}","notifications_url":"https://api.github.com/repos/Shopify/graphql-batch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Shopify/graphql-batch/labels{/name}","releases_url":"https://api.github.com/repos/Shopify/graphql-batch/releases{/id}","deployments_url":"https://api.github.com/repos/Shopify/graphql-batch/deployments","created_at":"2015-09-20T04:57:22Z","updated_at":"2016-01-20T18:32:10Z","pushed_at":"2016-02-04T18:12:22Z","git_url":"git://github.com/Shopify/graphql-batch.git","ssh_url":"git@github.com:Shopify/graphql-batch.git","clone_url":"https://github.com/Shopify/graphql-batch.git","svn_url":"https://github.com/Shopify/graphql-batch","homepage":null,"size":29,"stargazers_count":13,"watchers_count":13,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":2,"forks":2,"open_issues":2,"watchers":13,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Shopify/graphql-batch/pulls/3"},"html":{"href":"https://github.com/Shopify/graphql-batch/pull/3"},"issue":{"href":"https://api.github.com/repos/Shopify/graphql-batch/issues/3"},"comments":{"href":"https://api.github.com/repos/Shopify/graphql-batch/issues/3/comments"},"review_comments":{"href":"https://api.github.com/repos/Shopify/graphql-batch/pulls/3/comments"},"review_comment":{"href":"https://api.github.com/repos/Shopify/graphql-batch/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Shopify/graphql-batch/pulls/3/commits"},"statuses":{"href":"https://api.github.com/repos/Shopify/graphql-batch/statuses/595c574eb7a7576f121ba718e2febb84ee3604d9"}}}}