Skip to content

Use Executor for getting responses #13

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

Merged
merged 1 commit into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions graphql_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
from graphql import get_default_backend
from graphql.error import format_error as default_format_error
from graphql.execution import ExecutionResult
from graphql.execution.executors.sync import SyncExecutor
from graphql.type import GraphQLSchema
from promise import Promise, is_thenable

from .error import HttpQueryError

Expand Down Expand Up @@ -120,10 +122,20 @@ def run_http_query(

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

results = [
get_response(schema, params, catch_exc, allow_only_query, **execute_options)
executor = execute_options.get("executor")
response_executor = executor if executor else SyncExecutor()

response_promises = [
response_executor.execute(
get_response, schema, params, catch_exc, allow_only_query, **execute_options
)
for params in all_params
]
response_executor.wait_until_finished()

results = [
result.get() if is_thenable(result) else result for result in response_promises
]

return ServerResults(results, all_params)

Expand Down
33 changes: 33 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,36 @@ def test_batch_allows_post_with_operation_name():
assert as_dicts(results) == [
{"data": {"test": "Hello World", "shared": "Hello Everyone"}}
]


def test_get_reponses_using_executor():
class TestExecutor(object):
called = False
waited = False
cleaned = False

def wait_until_finished(self):
TestExecutor.waited = True

def clean(self):
TestExecutor.cleaned = True

def execute(self, fn, *args, **kwargs):
TestExecutor.called = True
return fn(*args, **kwargs)

query = "{test}"
results, params = run_http_query(
schema,
"get",
{},
dict(query=query),
executor=TestExecutor(),
return_promise=True,
)

assert as_dicts(results) == [{"data": {"test": "Hello World"}}]
assert params == [RequestParams(query=query, variables=None, operation_name=None)]
assert TestExecutor.called
assert TestExecutor.waited
assert TestExecutor.cleaned