From dca435a22022ba1f92bd5cd2f2d175f98999c19d Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Wed, 9 Dec 2015 19:22:54 -0800 Subject: [PATCH 1/2] Improved plugin system --- graphene/core/schema.py | 21 ++++++------------- graphene/plugins/__init__.py | 4 ++-- graphene/plugins/base.py | 37 ++++++++++++++++++++++++++++++++-- graphene/plugins/camel_case.py | 3 +-- 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/graphene/core/schema.py b/graphene/core/schema.py index d3bff4bd..6c98371f 100644 --- a/graphene/core/schema.py +++ b/graphene/core/schema.py @@ -10,7 +10,7 @@ from graphql.core.utils.schema_printer import print_schema from graphene import signals -from ..plugins import CamelCase, Plugin +from ..plugins import CamelCase, PluginManager from .classtypes.base import ClassType from .types.base import InstanceType @@ -34,28 +34,19 @@ class Schema(object): self.subscription = subscription self.name = name self.executor = executor - self.plugins = [] plugins = plugins or [] if auto_camelcase: plugins.append(CamelCase()) - for plugin in plugins: - self.add_plugin(plugin) + self.plugins = PluginManager(self, plugins) signals.init_schema.send(self) def __repr__(self): return '' % (str(self.name), hash(self)) - def add_plugin(self, plugin): - assert isinstance(plugin, Plugin), 'A plugin need to subclass graphene.Plugin and be instantiated' - plugin.contribute_to_schema(self) - self.plugins.append(plugin) - - def get_default_namedtype_name(self, value): - for plugin in self.plugins: - if not hasattr(plugin, 'get_default_namedtype_name'): - continue - value = plugin.get_default_namedtype_name(value) - return value + def __getattr__(self, name): + if name in self.plugins: + return getattr(self.plugins, name) + return super(Schema, self).__getattr__(name) def T(self, _type): if not _type: diff --git a/graphene/plugins/__init__.py b/graphene/plugins/__init__.py index 5a9bf26b..160bffba 100644 --- a/graphene/plugins/__init__.py +++ b/graphene/plugins/__init__.py @@ -1,6 +1,6 @@ -from .base import Plugin +from .base import Plugin, PluginManager from .camel_case import CamelCase __all__ = [ - 'Plugin', 'CamelCase' + 'Plugin', 'PluginManager', 'CamelCase' ] diff --git a/graphene/plugins/base.py b/graphene/plugins/base.py index c881592e..8ab5668a 100644 --- a/graphene/plugins/base.py +++ b/graphene/plugins/base.py @@ -1,7 +1,40 @@ +from functools import partial, reduce + + class Plugin(object): def contribute_to_schema(self, schema): self.schema = schema - def transform_type(self, objecttype): - return objecttype + +def apply_function(a, b): + return b(a) + + +class PluginManager(object): + + PLUGIN_FUNCTIONS = ('get_default_namedtype_name', ) + + def __init__(self, schema, plugins=[]): + self.schema = schema + self.plugins = [] + for plugin in plugins: + self.add_plugin(plugin) + + def add_plugin(self, plugin): + if hasattr(plugin, 'contribute_to_schema'): + plugin.contribute_to_schema(self.schema) + self.plugins.append(plugin) + + def get_plugin_functions(self, function): + for plugin in self.plugins: + if not hasattr(plugin, function): + continue + yield getattr(plugin, function) + + def __getattr__(self, name): + functions = self.get_plugin_functions(name) + return partial(reduce, apply_function, functions) + + def __contains__(self, name): + return name in self.PLUGIN_FUNCTIONS diff --git a/graphene/plugins/camel_case.py b/graphene/plugins/camel_case.py index c8ebcd11..d9a9084f 100644 --- a/graphene/plugins/camel_case.py +++ b/graphene/plugins/camel_case.py @@ -1,8 +1,7 @@ from ..utils import to_camel_case -from .base import Plugin -class CamelCase(Plugin): +class CamelCase(object): def get_default_namedtype_name(self, value): return to_camel_case(value) From c8f4c138224b6e31f01be65a8c1131e2ecf2ed5d Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Wed, 9 Dec 2015 19:44:35 -0800 Subject: [PATCH 2/2] Improved plugin execution --- graphene/core/schema.py | 21 +++------------------ graphene/plugins/base.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/graphene/core/schema.py b/graphene/core/schema.py index dd0fce9e..f8f26dc1 100644 --- a/graphene/core/schema.py +++ b/graphene/core/schema.py @@ -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 diff --git a/graphene/plugins/base.py b/graphene/plugins/base.py index 8ab5668a..2347beba 100644 --- a/graphene/plugins/base.py +++ b/graphene/plugins/base.py @@ -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)