mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-09 08:00:39 +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
|
return self._types_names
|
||||||
|
|
||||||
def execute(self, request='', root=None, args=None, **kwargs):
|
def execute(self, request='', root=None, args=None, **kwargs):
|
||||||
executor = kwargs
|
kwargs = dict(kwargs, request=request, root=root, args=args, schema=self.schema)
|
||||||
executor['root'] = root
|
with self.plugins.context_execution(**kwargs) as execute_kwargs:
|
||||||
executor['args'] = args
|
return self.executor.execute(**execute_kwargs)
|
||||||
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
|
|
||||||
|
|
||||||
def introspect(self):
|
def introspect(self):
|
||||||
return self.execute(introspection_query).data
|
return self.execute(introspection_query).data
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
from contextlib import contextmanager
|
||||||
from functools import partial, reduce
|
from functools import partial, reduce
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,3 +39,15 @@ class PluginManager(object):
|
||||||
|
|
||||||
def __contains__(self, name):
|
def __contains__(self, name):
|
||||||
return name in self.PLUGIN_FUNCTIONS
|
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