Skip to content

Commit c8f4c13

Browse files
committed
Improved plugin execution
1 parent b3b440f commit c8f4c13

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

graphene/core/schema.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,24 +119,9 @@ def types(self):
119119
return self._types_names
120120

121121
def execute(self, request='', root=None, args=None, **kwargs):
122-
executor = kwargs
123-
executor['root'] = root
124-
executor['args'] = args
125-
executor['schema'] = self.schema
126-
executor['request'] = request
127-
contexts = []
128-
for plugin in self.plugins:
129-
if not hasattr(plugin, 'context_execution'):
130-
continue
131-
context = plugin.context_execution(executor)
132-
executor = context.__enter__()
133-
contexts.append((context, executor))
134-
result = self.executor.execute(
135-
**executor
136-
)
137-
for context, value in contexts[::-1]:
138-
context.__exit__(None, None, None)
139-
return result
122+
kwargs = dict(kwargs, request=request, root=root, args=args, schema=self.schema)
123+
with self.plugins.context_execution(**kwargs) as execute_kwargs:
124+
return self.executor.execute(**execute_kwargs)
140125

141126
def introspect(self):
142127
return self.execute(introspection_query).data

graphene/plugins/base.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import contextmanager
12
from functools import partial, reduce
23

34

@@ -38,3 +39,15 @@ def __getattr__(self, name):
3839

3940
def __contains__(self, name):
4041
return name in self.PLUGIN_FUNCTIONS
42+
43+
@contextmanager
44+
def context_execution(self, **executor):
45+
contexts = []
46+
functions = self.get_plugin_functions('context_execution')
47+
for f in functions:
48+
context = f(executor)
49+
executor = context.__enter__()
50+
contexts.append((context, executor))
51+
yield executor
52+
for context, value in contexts[::-1]:
53+
context.__exit__(None, None, None)

0 commit comments

Comments
 (0)