Skip to content

Commit 7ff9553

Browse files
authored
Merge pull request #13 from lucasrcosta/use-response-executor
Use Executor for getting responses
2 parents c781f01 + 786849b commit 7ff9553

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

graphql_server/__init__.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
from graphql import get_default_backend
1616
from graphql.error import format_error as default_format_error
1717
from graphql.execution import ExecutionResult
18+
from graphql.execution.executors.sync import SyncExecutor
1819
from graphql.type import GraphQLSchema
20+
from promise import Promise, is_thenable
1921

2022
from .error import HttpQueryError
2123

@@ -120,10 +122,20 @@ def run_http_query(
120122

121123
all_params = [get_graphql_params(entry, extra_data) for entry in data]
122124

123-
results = [
124-
get_response(schema, params, catch_exc, allow_only_query, **execute_options)
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+
)
125132
for params in all_params
126133
]
134+
response_executor.wait_until_finished()
135+
136+
results = [
137+
result.get() if is_thenable(result) else result for result in response_promises
138+
]
127139

128140
return ServerResults(results, all_params)
129141

tests/test_query.py

+33
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,36 @@ def test_batch_allows_post_with_operation_name():
530530
assert as_dicts(results) == [
531531
{"data": {"test": "Hello World", "shared": "Hello Everyone"}}
532532
]
533+
534+
535+
def test_get_reponses_using_executor():
536+
class TestExecutor(object):
537+
called = False
538+
waited = False
539+
cleaned = False
540+
541+
def wait_until_finished(self):
542+
TestExecutor.waited = True
543+
544+
def clean(self):
545+
TestExecutor.cleaned = True
546+
547+
def execute(self, fn, *args, **kwargs):
548+
TestExecutor.called = True
549+
return fn(*args, **kwargs)
550+
551+
query = "{test}"
552+
results, params = run_http_query(
553+
schema,
554+
"get",
555+
{},
556+
dict(query=query),
557+
executor=TestExecutor(),
558+
return_promise=True,
559+
)
560+
561+
assert as_dicts(results) == [{"data": {"test": "Hello World"}}]
562+
assert params == [RequestParams(query=query, variables=None, operation_name=None)]
563+
assert TestExecutor.called
564+
assert TestExecutor.waited
565+
assert TestExecutor.cleaned

0 commit comments

Comments
 (0)