Skip to content

Commit b3b440f

Browse files
committed
Merge branch 'refs/heads/features/plugins-autocamelcase' into features/django-debug
2 parents dd5b26e + dca435a commit b3b440f

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

graphene/core/schema.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from graphene import signals
1212

13-
from ..plugins import CamelCase, Plugin
13+
from ..plugins import CamelCase, PluginManager
1414
from .classtypes.base import ClassType
1515
from .types.base import InstanceType
1616

@@ -34,28 +34,19 @@ def __init__(self, query=None, mutation=None, subscription=None,
3434
self.subscription = subscription
3535
self.name = name
3636
self.executor = executor
37-
self.plugins = []
3837
plugins = plugins or []
3938
if auto_camelcase:
4039
plugins.append(CamelCase())
41-
for plugin in plugins:
42-
self.add_plugin(plugin)
40+
self.plugins = PluginManager(self, plugins)
4341
signals.init_schema.send(self)
4442

4543
def __repr__(self):
4644
return '<Schema: %s (%s)>' % (str(self.name), hash(self))
4745

48-
def add_plugin(self, plugin):
49-
assert isinstance(plugin, Plugin), 'A plugin need to subclass graphene.Plugin and be instantiated'
50-
plugin.contribute_to_schema(self)
51-
self.plugins.append(plugin)
52-
53-
def get_default_namedtype_name(self, value):
54-
for plugin in self.plugins:
55-
if not hasattr(plugin, 'get_default_namedtype_name'):
56-
continue
57-
value = plugin.get_default_namedtype_name(value)
58-
return value
46+
def __getattr__(self, name):
47+
if name in self.plugins:
48+
return getattr(self.plugins, name)
49+
return super(Schema, self).__getattr__(name)
5950

6051
def T(self, _type):
6152
if not _type:

graphene/plugins/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from .base import Plugin
1+
from .base import Plugin, PluginManager
22
from .camel_case import CamelCase
33

44
__all__ = [
5-
'Plugin', 'CamelCase'
5+
'Plugin', 'PluginManager', 'CamelCase'
66
]

graphene/plugins/base.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,40 @@
1+
from functools import partial, reduce
2+
3+
14
class Plugin(object):
25

36
def contribute_to_schema(self, schema):
47
self.schema = schema
58

6-
def transform_type(self, objecttype):
7-
return objecttype
9+
10+
def apply_function(a, b):
11+
return b(a)
12+
13+
14+
class PluginManager(object):
15+
16+
PLUGIN_FUNCTIONS = ('get_default_namedtype_name', )
17+
18+
def __init__(self, schema, plugins=[]):
19+
self.schema = schema
20+
self.plugins = []
21+
for plugin in plugins:
22+
self.add_plugin(plugin)
23+
24+
def add_plugin(self, plugin):
25+
if hasattr(plugin, 'contribute_to_schema'):
26+
plugin.contribute_to_schema(self.schema)
27+
self.plugins.append(plugin)
28+
29+
def get_plugin_functions(self, function):
30+
for plugin in self.plugins:
31+
if not hasattr(plugin, function):
32+
continue
33+
yield getattr(plugin, function)
34+
35+
def __getattr__(self, name):
36+
functions = self.get_plugin_functions(name)
37+
return partial(reduce, apply_function, functions)
38+
39+
def __contains__(self, name):
40+
return name in self.PLUGIN_FUNCTIONS

graphene/plugins/camel_case.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from ..utils import to_camel_case
2-
from .base import Plugin
32

43

5-
class CamelCase(Plugin):
4+
class CamelCase(object):
65

76
def get_default_namedtype_name(self, value):
87
return to_camel_case(value)

0 commit comments

Comments
 (0)