Skip to content

Commit a0dbce7

Browse files
committed
Add support for request_context to be passed into Executor. This way we can propegate specific values to the resolver, i.e. "logged in user"... etc.
1 parent 820e88a commit a0dbce7

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

graphql/core/execution/base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ExecutionContext(object):
2727
Namely, schema of the type system that is currently executing,
2828
and the fragments defined in the query document"""
2929

30-
def __init__(self, schema, root, document_ast, operation_name, args):
30+
def __init__(self, schema, root, document_ast, operation_name, args, request_context):
3131
"""Constructs a ExecutionContext object from the arguments passed
3232
to execute, which we will pass throughout the other execution
3333
methods."""
@@ -58,6 +58,7 @@ def __init__(self, schema, root, document_ast, operation_name, args):
5858
self.operation = operation
5959
self.variables = variables
6060
self.errors = errors
61+
self.request_context = request_context
6162

6263

6364
class ExecutionResult(object):
@@ -200,6 +201,10 @@ def operation(self):
200201
def variable_values(self):
201202
return self.context.variables
202203

204+
@property
205+
def request_context(self):
206+
return self.context.request_context
207+
203208

204209
def default_resolve_fn(source, args, info):
205210
"""If a resolve function is not given, then a default resolve behavior is used which takes the property of the source object

graphql/core/execution/executor.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515

1616

1717
class Executor(object):
18-
def __init__(self, schema, execution_middlewares=None):
18+
def __init__(self, schema, execution_middlewares=None, default_resolver=default_resolve_fn):
1919
self.execution_middlewares = execution_middlewares or []
20+
self.default_resolve_fn = default_resolver
2021
self.schema = schema
2122

2223
def execute(self, request='', root=None, args=None, operation_name=None, execute_serially=False, validate_ast=True):
@@ -35,8 +36,8 @@ def execute(self, request='', root=None, args=None, operation_name=None, execute
3536

3637
return curried_execution_function()
3738

38-
def _execute(self, request='', root=None, args=None, operation_name=None, execute_serially=False,
39-
validate_ast=True):
39+
def _execute(self, request='', root=None, args=None, operation_name=None, request_context=None,
40+
execute_serially=False, validate_ast=True):
4041
if not isinstance(request, ast.Document):
4142
if not isinstance(request, Source):
4243
request = Source(request, 'GraphQL request')
@@ -56,10 +57,11 @@ def _execute(self, request='', root=None, args=None, operation_name=None, execut
5657
request,
5758
operation_name,
5859
args or {},
60+
request_context or {},
5961
execute_serially)
6062

61-
def _execute_graphql_query(self, root, ast, operation_name, args, execute_serially=False):
62-
ctx = ExecutionContext(self.schema, root, ast, operation_name, args)
63+
def _execute_graphql_query(self, root, ast, operation_name, args, request_context, execute_serially=False):
64+
ctx = ExecutionContext(self.schema, root, ast, operation_name, args, request_context)
6365

6466
return defer(self._execute_operation, ctx, root, ctx.operation, execute_serially) \
6567
.add_errback(
@@ -127,7 +129,7 @@ def _resolve_field(self, execution_context, parent_type, source, field_asts):
127129
return Undefined
128130

129131
return_type = field_def.type
130-
resolve_fn = field_def.resolver or default_resolve_fn
132+
resolve_fn = field_def.resolver or self.default_resolve_fn
131133

132134
# Build a dict of arguments from the field.arguments AST, using the variables scope to
133135
# fulfill any variable references.

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ deps =
66
pytest>=2.7.2
77
gevent==1.1b5
88
commands =
9-
py{27,33,34,py}: py.test tests
10-
py35: py.test tests tests_py35
9+
py{27,33,34,py}: py.test tests {posargs}
10+
py35: py.test tests tests_py35 {posargs}
1111

1212

1313
[testenv:flake8]

0 commit comments

Comments
 (0)