mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-11 12:16:58 +03:00
Improved plugin execution
This commit is contained in:
parent
b3b440f837
commit
c8f4c13822
|
@ -119,24 +119,9 @@ class Schema(object):
|
|||
return self._types_names
|
||||
|
||||
def execute(self, request='', root=None, args=None, **kwargs):
|
||||
executor = kwargs
|
||||
executor['root'] = root
|
||||
executor['args'] = args
|
||||
executor['schema'] = self.schema
|
||||
executor['request'] = request
|
||||
contexts = []
|
||||
for plugin in self.plugins:
|
||||
if not hasattr(plugin, 'context_execution'):
|
||||
continue
|
||||
context = plugin.context_execution(executor)
|
||||
executor = context.__enter__()
|
||||
contexts.append((context, executor))
|
||||
result = self.executor.execute(
|
||||
**executor
|
||||
)
|
||||
for context, value in contexts[::-1]:
|
||||
context.__exit__(None, None, None)
|
||||
return result
|
||||
kwargs = dict(kwargs, request=request, root=root, args=args, schema=self.schema)
|
||||
with self.plugins.context_execution(**kwargs) as execute_kwargs:
|
||||
return self.executor.execute(**execute_kwargs)
|
||||
|
||||
def introspect(self):
|
||||
return self.execute(introspection_query).data
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
from contextlib import contextmanager
|
||||
from functools import partial, reduce
|
||||
|
||||
|
||||
|
@ -38,3 +39,15 @@ class PluginManager(object):
|
|||
|
||||
def __contains__(self, name):
|
||||
return name in self.PLUGIN_FUNCTIONS
|
||||
|
||||
@contextmanager
|
||||
def context_execution(self, **executor):
|
||||
contexts = []
|
||||
functions = self.get_plugin_functions('context_execution')
|
||||
for f in functions:
|
||||
context = f(executor)
|
||||
executor = context.__enter__()
|
||||
contexts.append((context, executor))
|
||||
yield executor
|
||||
for context, value in contexts[::-1]:
|
||||
context.__exit__(None, None, None)
|
||||
|
|
Loading…
Reference in New Issue
Block a user