Skip to content

Commit 845ea85

Browse files
committed
Don't use executor in run_http_query when return_promise=True
1 parent 63c393c commit 845ea85

File tree

2 files changed

+58
-15
lines changed

2 files changed

+58
-15
lines changed

graphql_server/__init__.py

+26-14
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,32 @@ def run_http_query(
122122

123123
all_params = [get_graphql_params(entry, extra_data) for entry in data]
124124

125-
executor = execute_options.get("executor")
126-
response_executor = executor if executor else SyncExecutor()
127-
128-
response_promises = [
129-
response_executor.execute(
130-
get_response, schema, params, catch_exc, allow_only_query, **execute_options
131-
)
132-
for params in all_params
133-
]
134-
response_executor.wait_until_finished()
135-
136-
results = [
137-
result.get() if is_thenable(result) else result for result in response_promises
138-
]
125+
if execute_options.get("return_promise"):
126+
results = [
127+
get_response(schema, params, catch_exc, allow_only_query, **execute_options)
128+
for params in all_params
129+
]
130+
else:
131+
executor = execute_options.get("executor")
132+
response_executor = executor if executor else SyncExecutor()
133+
134+
response_promises = [
135+
response_executor.execute(
136+
get_response,
137+
schema,
138+
params,
139+
catch_exc,
140+
allow_only_query,
141+
**execute_options
142+
)
143+
for params in all_params
144+
]
145+
response_executor.wait_until_finished()
146+
147+
results = [
148+
result.get() if is_thenable(result) else result
149+
for result in response_promises
150+
]
139151

140152
return ServerResults(results, all_params)
141153

tests/test_query.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22

33
from graphql.error import GraphQLError
4+
from promise import Promise
45

56
from graphql_server import (
67
HttpQueryError,
@@ -546,6 +547,34 @@ def execute(self, fn, *args, **kwargs):
546547

547548
query = "{test}"
548549
results, params = run_http_query(
550+
schema, "get", {}, dict(query=query), executor=TestExecutor(),
551+
)
552+
553+
assert as_dicts(results) == [{"data": {"test": "Hello World"}}]
554+
assert params == [RequestParams(query=query, variables=None, operation_name=None)]
555+
assert TestExecutor.called
556+
assert TestExecutor.waited
557+
assert not TestExecutor.cleaned
558+
559+
560+
def test_get_reponses_using_executor_return_promise():
561+
class TestExecutor(object):
562+
called = False
563+
waited = False
564+
cleaned = False
565+
566+
def wait_until_finished(self):
567+
TestExecutor.waited = True
568+
569+
def clean(self):
570+
TestExecutor.cleaned = True
571+
572+
def execute(self, fn, *args, **kwargs):
573+
TestExecutor.called = True
574+
return fn(*args, **kwargs)
575+
576+
query = "{test}"
577+
result_promises, params = run_http_query(
549578
schema,
550579
"get",
551580
{},
@@ -554,8 +583,10 @@ def execute(self, fn, *args, **kwargs):
554583
return_promise=True,
555584
)
556585

586+
results = Promise.all(result_promises).get()
587+
557588
assert as_dicts(results) == [{"data": {"test": "Hello World"}}]
558589
assert params == [RequestParams(query=query, variables=None, operation_name=None)]
559590
assert TestExecutor.called
560-
assert TestExecutor.waited
591+
assert not TestExecutor.waited
561592
assert TestExecutor.cleaned

0 commit comments

Comments
 (0)