Skip to content

Commit b881789

Browse files
committed
Merge pull request #59 from graphql-python/features/promises
Use Promises in execute context
2 parents e54543d + 1dc8c87 commit b881789

33 files changed

+1259
-1987
lines changed

graphql/execution/__init__.py

+3-34
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,11 @@
1818
2) fragment "spreads" e.g. "...c"
1919
3) inline fragment "spreads" e.g. "...on Type { a }"
2020
"""
21-
21+
from .execute import execute as _execute
2222
from .base import ExecutionResult
23-
from .executor import Executor
24-
from .middlewares.sync import SynchronousExecutionMiddleware
2523

2624

2725
def execute(schema, root, ast, operation_name='', args=None):
28-
"""
29-
Executes an AST synchronously. Assumes that the AST is already validated.
30-
"""
31-
return get_default_executor().execute(schema, ast, root, args, operation_name, validate_ast=False)
32-
33-
34-
_default_executor = None
35-
36-
37-
def get_default_executor():
38-
"""
39-
Gets the default executor to be used in the `execute` function above.
40-
"""
41-
global _default_executor
42-
if _default_executor is None:
43-
_default_executor = Executor([SynchronousExecutionMiddleware()])
44-
45-
return _default_executor
46-
47-
48-
def set_default_executor(executor):
49-
"""
50-
Sets the default executor to be used in the `execute` function above.
51-
52-
If passed `None` will reset to the original default synchronous executor.
53-
"""
54-
assert isinstance(executor, Executor) or executor is None
55-
global _default_executor
56-
_default_executor = executor
57-
26+
return _execute(schema, ast, root, variable_values=args, operation_name=operation_name)
5827

59-
__all__ = ['ExecutionResult', 'Executor', 'execute', 'get_default_executor', 'set_default_executor']
28+
__all__ = ['execute', 'ExecutionResult']

graphql/execution/base.py

+19-43
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from ..error import GraphQLError
33
from ..language import ast
4-
from ..pyutils.defer import DeferredException
54
from ..type.definition import GraphQLInterfaceType, GraphQLUnionType
65
from ..type.directives import GraphQLIncludeDirective, GraphQLSkipDirective
76
from ..type.introspection import (SchemaMetaFieldDef, TypeMetaFieldDef,
@@ -18,10 +17,10 @@ class ExecutionContext(object):
1817
Namely, schema of the type system that is currently executing,
1918
and the fragments defined in the query document"""
2019

21-
__slots__ = 'schema', 'fragments', 'root', 'operation', 'variables', 'errors', 'request_context', \
22-
'argument_values_cache'
20+
__slots__ = 'schema', 'fragments', 'root_value', 'operation', 'variable_values', 'errors', 'context_value', \
21+
'argument_values_cache', 'executor'
2322

24-
def __init__(self, schema, root, document_ast, operation_name, args, request_context):
23+
def __init__(self, schema, document_ast, root_value, context_value, variable_values, operation_name, executor):
2524
"""Constructs a ExecutionContext object from the arguments passed
2625
to execute, which we will pass throughout the other execution
2726
methods."""
@@ -53,24 +52,25 @@ def __init__(self, schema, root, document_ast, operation_name, args, request_con
5352
else:
5453
raise GraphQLError('Must provide an operation.')
5554

56-
variables = get_variable_values(schema, operation.variable_definitions or [], args)
55+
variable_values = get_variable_values(schema, operation.variable_definitions or [], variable_values)
5756

5857
self.schema = schema
5958
self.fragments = fragments
60-
self.root = root
59+
self.root_value = root_value
6160
self.operation = operation
62-
self.variables = variables
61+
self.variable_values = variable_values
6362
self.errors = errors
64-
self.request_context = request_context
63+
self.context_value = context_value
6564
self.argument_values_cache = {}
65+
self.executor = executor
6666

6767
def get_argument_values(self, field_def, field_ast):
6868
k = field_def, field_ast
6969
result = self.argument_values_cache.get(k)
7070

7171
if not result:
7272
result = self.argument_values_cache[k] = get_argument_values(field_def.args, field_ast.arguments,
73-
self.variables)
73+
self.variable_values)
7474

7575
return result
7676

@@ -84,12 +84,6 @@ class ExecutionResult(object):
8484

8585
def __init__(self, data=None, errors=None, invalid=False):
8686
self.data = data
87-
if errors:
88-
errors = [
89-
error.value if isinstance(error, DeferredException) else error
90-
for error in errors
91-
]
92-
9387
self.errors = errors
9488

9589
if invalid:
@@ -190,6 +184,7 @@ def collect_fields(ctx, runtime_type, selection_set, fields, prev_fragment_names
190184
def should_include_node(ctx, directives):
191185
"""Determines if a field should be included based on the @include and
192186
@skip directives, where @skip has higher precidence than @include."""
187+
# TODO: Refactor based on latest code
193188
if directives:
194189
skip_ast = None
195190

@@ -202,7 +197,7 @@ def should_include_node(ctx, directives):
202197
args = get_argument_values(
203198
GraphQLSkipDirective.args,
204199
skip_ast.arguments,
205-
ctx.variables,
200+
ctx.variable_values,
206201
)
207202
return not args.get('if')
208203

@@ -217,7 +212,7 @@ def should_include_node(ctx, directives):
217212
args = get_argument_values(
218213
GraphQLIncludeDirective.args,
219214
include_ast.arguments,
220-
ctx.variables,
215+
ctx.variable_values,
221216
)
222217

223218
return bool(args.get('if'))
@@ -249,36 +244,17 @@ def get_field_entry_key(node):
249244

250245
class ResolveInfo(object):
251246

252-
def __init__(self, field_name, field_asts, return_type, parent_type, context):
247+
def __init__(self, field_name, field_asts, return_type, parent_type,
248+
schema, fragments, root_value, operation, variable_values):
253249
self.field_name = field_name
254250
self.field_asts = field_asts
255251
self.return_type = return_type
256252
self.parent_type = parent_type
257-
self.context = context
258-
259-
@property
260-
def schema(self):
261-
return self.context.schema
262-
263-
@property
264-
def fragments(self):
265-
return self.context.fragments
266-
267-
@property
268-
def root_value(self):
269-
return self.context.root
270-
271-
@property
272-
def operation(self):
273-
return self.context.operation
274-
275-
@property
276-
def variable_values(self):
277-
return self.context.variables
278-
279-
@property
280-
def request_context(self):
281-
return self.context.request_context
253+
self.schema = schema
254+
self.fragments = fragments
255+
self.root_value = root_value
256+
self.operation = operation
257+
self.variable_values = variable_values
282258

283259

284260
def default_resolve_fn(source, args, info):

0 commit comments

Comments
 (0)