Skip to content

Commit 12e4e2c

Browse files
committed
Added Plugins structure. Added option in schema for auto camel case or not.
1 parent ec3f292 commit 12e4e2c

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

graphene/core/schema.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from .classtypes.base import ClassType
1414
from .types.base import BaseType
15+
from ..plugins import Plugin, CamelCase
1516

1617

1718
class GraphQLSchema(_GraphQLSchema):
@@ -25,20 +26,33 @@ class Schema(object):
2526
_executor = None
2627

2728
def __init__(self, query=None, mutation=None, subscription=None,
28-
name='Schema', executor=None):
29+
name='Schema', executor=None, plugins=None, auto_camelcase=True):
2930
self._types_names = {}
3031
self._types = {}
3132
self.mutation = mutation
3233
self.query = query
3334
self.subscription = subscription
3435
self.name = name
3536
self.executor = executor
37+
self.plugins = []
38+
plugins = plugins or []
39+
if auto_camelcase:
40+
plugins.append(CamelCase())
41+
for plugin in plugins:
42+
self.add_plugin(plugin)
3643
signals.init_schema.send(self)
3744

3845
def __repr__(self):
3946
return '<Schema: %s (%s)>' % (str(self.name), hash(self))
4047

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+
4153
def get_internal_type(self, objecttype):
54+
for plugin in self.plugins:
55+
objecttype = plugin.transform_type(objecttype)
4256
return objecttype.internal_type(self)
4357

4458
def T(self, object_type):

graphene/core/types/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def __init__(self, *types):
140140
self.types = types
141141

142142
def get_named_type(self, schema, type):
143-
name = type.name or to_camel_case(type.attname)
143+
name = type.name or type.attname
144144
return name, schema.T(type)
145145

146146
def internal_type(self, schema):

graphene/plugins/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from .base import Plugin
2+
from .camel_case import CamelCase
3+
4+
__all__ = [
5+
'Plugin', 'CamelCase'
6+
]

graphene/plugins/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Plugin(object):
2+
def contribute_to_schema(self, schema):
3+
self.schema = schema
4+
5+
def transform_type(self, objecttype):
6+
return objecttype

graphene/plugins/camel_case.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from .base import Plugin
2+
3+
from ..core.types.base import GroupNamedType
4+
from ..utils import memoize, to_camel_case
5+
6+
7+
def camelcase_named_type(schema, type):
8+
name = type.name or to_camel_case(type.attname)
9+
return name, schema.T(type)
10+
11+
12+
class CamelCase(Plugin):
13+
@memoize
14+
def transform_group(self, _type):
15+
new_type = _type.__class__(*_type.types)
16+
setattr(new_type, 'get_named_type', camelcase_named_type)
17+
return new_type
18+
19+
def transform_type(self, _type):
20+
if isinstance(_type, GroupNamedType):
21+
return self.transform_group(_type)
22+
return _type

0 commit comments

Comments
 (0)