Skip to content

Commit f6d79ab

Browse files
committed
Modernize execute function
1 parent f35c93c commit f6d79ab

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

graphql/execution/executor.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import functools
33
import logging
44
import sys
5+
import warnings
56
from rx import Observable
67

78
from six import string_types
@@ -28,9 +29,31 @@ def subscribe(*args, **kwargs):
2829
return execute(*args, allow_subscriptions=allow_subscriptions, **kwargs)
2930

3031

31-
def execute(schema, document_ast, root_value=None, context_value=None,
32-
variable_values=None, operation_name=None, executor=None,
33-
return_promise=False, middleware=None, allow_subscriptions=False):
32+
def execute(schema, document_ast, root=None, context=None,
33+
variables=None, operation_name=None, executor=None,
34+
return_promise=False, middleware=None, allow_subscriptions=False, **options):
35+
36+
if root is None and 'root_value' in options:
37+
warnings.warn(
38+
'root_value has been deprecated. Please use root=... instead.',
39+
category=DeprecationWarning,
40+
stacklevel=2
41+
)
42+
root = options['root_value']
43+
if context is None and 'context_value' in options:
44+
warnings.warn(
45+
'context_value has been deprecated. Please use context=... instead.',
46+
category=DeprecationWarning,
47+
stacklevel=2
48+
)
49+
context = options['context_value']
50+
if variables is None and 'variable_values' in options:
51+
warnings.warn(
52+
'variable_values has been deprecated. Please use values=... instead.',
53+
category=DeprecationWarning,
54+
stacklevel=2
55+
)
56+
variables = options['variable_values']
3457
assert schema, 'Must provide schema'
3558
assert isinstance(schema, GraphQLSchema), (
3659
'Schema must be an instance of GraphQLSchema. Also ensure that there are ' +
@@ -49,38 +72,38 @@ def execute(schema, document_ast, root_value=None, context_value=None,
4972
if executor is None:
5073
executor = SyncExecutor()
5174

52-
context = ExecutionContext(
75+
exe_context = ExecutionContext(
5376
schema,
5477
document_ast,
55-
root_value,
56-
context_value,
57-
variable_values,
78+
root,
79+
context,
80+
variables or {},
5881
operation_name,
5982
executor,
6083
middleware,
6184
allow_subscriptions
6285
)
6386

6487
def executor(v):
65-
return execute_operation(context, context.operation, root_value)
88+
return execute_operation(exe_context, exe_context.operation, root)
6689

6790
def on_rejected(error):
68-
context.errors.append(error)
91+
exe_context.errors.append(error)
6992
return None
7093

7194
def on_resolve(data):
7295
if isinstance(data, Observable):
7396
return data
7497

75-
if not context.errors:
98+
if not exe_context.errors:
7699
return ExecutionResult(data=data)
77100

78-
return ExecutionResult(data=data, errors=context.errors)
101+
return ExecutionResult(data=data, errors=exe_context.errors)
79102

80103
promise = Promise.resolve(None).then(executor).catch(on_rejected).then(on_resolve)
81104

82105
if not return_promise:
83-
context.executor.wait_until_finished()
106+
exe_context.executor.wait_until_finished()
84107
return promise.get()
85108

86109
return promise

graphql/graphql.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def graphql(*args, **kwargs):
3838
return execute_graphql(*args, **kwargs)
3939

4040

41-
def execute_graphql(schema, request_string='', root_value=None, context_value=None,
42-
variable_values=None, operation_name=None, executor=None,
43-
return_promise=False, middleware=None, allow_subscriptions=False):
41+
def execute_graphql(schema, request_string='', root=None, context=None,
42+
variables=None, operation_name=None, executor=None,
43+
return_promise=False, middleware=None, allow_subscriptions=False, **old_options):
4444
try:
4545
if isinstance(request_string, Document):
4646
ast = request_string
@@ -56,14 +56,15 @@ def execute_graphql(schema, request_string='', root_value=None, context_value=No
5656
return execute(
5757
schema,
5858
ast,
59-
root_value,
60-
context_value,
59+
root=root,
60+
context=context,
6161
operation_name=operation_name,
62-
variable_values=variable_values or {},
62+
variables=variables,
6363
executor=executor,
6464
middleware=middleware,
6565
return_promise=return_promise,
6666
allow_subscriptions=allow_subscriptions,
67+
**old_options
6768
)
6869
except Exception as e:
6970
return ExecutionResult(

0 commit comments

Comments
 (0)