Skip to content

Commit 25fd60d

Browse files
committed
Improved plugins
1 parent b431bfe commit 25fd60d

File tree

6 files changed

+52
-41
lines changed

6 files changed

+52
-41
lines changed

graphene/contrib/django/debug/plugin.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from contextlib import contextmanager
22

33
from django.db import connections
4+
from graphene import with_context
45

56
from ....core.schema import GraphQLSchema
67
from ....core.types import Field
@@ -10,11 +11,14 @@
1011
from .types import DjangoDebug
1112

1213

13-
class WrappedRoot(object):
14+
class EmptyContext(object):
15+
pass
1416

15-
def __init__(self, root):
17+
18+
class DjangoDebugContext(object):
19+
20+
def __init__(self):
1621
self._recorded = []
17-
self._root = root
1822

1923
def record(self, **log):
2024
self._recorded.append(DjangoDebugSQL(**log))
@@ -24,17 +28,9 @@ def debug(self):
2428

2529

2630
class WrapRoot(object):
27-
28-
@property
29-
def _root(self):
30-
return self._wrapped_root.root
31-
32-
@_root.setter
33-
def _root(self, value):
34-
self._wrapped_root = value
35-
36-
def resolve_debug(self, args, info):
37-
return self._wrapped_root.debug()
31+
@with_context
32+
def resolve_debug(self, args, context, info):
33+
return context.django_debug.debug()
3834

3935

4036
def debug_objecttype(objecttype):
@@ -72,8 +68,10 @@ def wrap_schema(self, schema_type):
7268

7369
@contextmanager
7470
def context_execution(self, executor):
75-
executor['root_value'] = WrappedRoot(root=executor.get('root_value'))
71+
context_value = executor.get('context_value') or EmptyContext()
72+
context_value.django_debug = DjangoDebugContext()
73+
executor['context_value'] = context_value
7674
executor['schema'] = self.wrap_schema(executor['schema'])
77-
self.enable_instrumentation(executor['root_value'])
75+
self.enable_instrumentation(context_value.django_debug)
7876
yield executor
7977
self.disable_instrumentation()

graphene/core/schema.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
from .types.base import InstanceType
1313

1414

15+
class ACI(object):
16+
def __init__(self, args, context, info):
17+
self.args = args
18+
self.context = context
19+
self.info = info
20+
21+
def __repr__(self):
22+
return "ACI(args={}, context={}, info={})".format(repr(self.args), repr(self.context), repr(self.info))
23+
24+
1525
class GraphQLSchema(_GraphQLSchema):
1626

1727
def __init__(self, schema, *args, **kwargs):
@@ -111,6 +121,26 @@ def get_type(self, type_name):
111121
raise KeyError('Type %r not found in %r' % (type_name, self))
112122
return self._types_names[type_name]
113123

124+
def resolve(self, resolver, root, args, context, info):
125+
aci = ACI(args, context, info)
126+
plugins_process_aci = self.plugins.get_plugin_functions('process_aci')
127+
plugins_process_response = self.plugins.get_plugin_functions('process_response')
128+
for process_aci in plugins_process_aci:
129+
processed_aci = process_aci(aci)
130+
if processed_aci is None:
131+
continue
132+
return processed_aci
133+
134+
response = resolver(root, aci.args, aci.context, aci.info)
135+
136+
for process_response in plugins_process_response:
137+
processed_response = process_response(response, aci)
138+
if processed_response is None:
139+
continue
140+
return processed_response
141+
142+
return response
143+
114144
@property
115145
def types(self):
116146
return self._types_names

graphene/core/types/argument.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
from functools import wraps
21
from itertools import chain
32

43
from graphql.type import GraphQLArgument
54

6-
from ...utils import ProxySnakeDict
75
from .base import ArgumentType, GroupNamedType, NamedType, OrderedType
86

97

@@ -53,11 +51,3 @@ def to_arguments(*args, **kwargs):
5351
arguments[name] = argument
5452

5553
return sorted(arguments.values())
56-
57-
58-
def snake_case_args(resolver):
59-
@wraps(resolver)
60-
def wrapped_resolver(instance, args, context, info):
61-
return resolver(instance, ProxySnakeDict(args), context, info)
62-
63-
return wrapped_resolver

graphene/core/types/field.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections import OrderedDict
2-
from functools import wraps
2+
from functools import wraps, partial
33

44
import six
55
from graphql.type import GraphQLField, GraphQLInputObjectField
@@ -10,7 +10,7 @@
1010
from ..classtypes.inputobjecttype import InputObjectType
1111
from ..classtypes.mutation import Mutation
1212
from ..exceptions import SkipField
13-
from .argument import Argument, ArgumentsGroup, snake_case_args
13+
from .argument import Argument, ArgumentsGroup
1414
from .base import (ArgumentType, GroupNamedType, LazyType, MountType,
1515
NamedType, OrderedType)
1616
from .definitions import NonNull
@@ -89,9 +89,6 @@ def get_type(self, schema):
8989
return NonNull(self.type)
9090
return self.type
9191

92-
def decorate_resolver(self, resolver):
93-
return snake_case_args(resolver)
94-
9592
def internal_type(self, schema):
9693
if not self.object_type:
9794
raise Exception('The field is not mounted in any ClassType')
@@ -119,7 +116,7 @@ def wrapped_func(instance, args, context, info):
119116

120117
assert type, 'Internal type for field %s is None' % str(self)
121118
return GraphQLField(type, args=schema.T(arguments),
122-
resolver=self.decorate_resolver(resolver),
119+
resolver=partial(schema.resolve, resolver),
123120
deprecation_reason=self.deprecation_reason,
124121
description=description,)
125122

graphene/core/types/tests/test_argument.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from graphene.core.schema import Schema
55
from graphene.core.types import ObjectType
66

7-
from ..argument import Argument, snake_case_args, to_arguments
7+
from ..argument import Argument, to_arguments
88
from ..scalars import String
99

1010

@@ -45,10 +45,3 @@ def test_to_arguments_wrong_type():
4545
p=3
4646
)
4747
assert 'Unknown argument p=3' == str(excinfo.value)
48-
49-
50-
def test_snake_case_args():
51-
def resolver(instance, args, context, info):
52-
return args['my_arg']['inner_arg']
53-
r = snake_case_args(resolver)
54-
assert r(None, {'myArg': {'innerArg': 3}}, None, None) == 3

graphene/plugins/camel_case.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
from ..utils import to_camel_case
1+
from ..utils import to_camel_case, ProxySnakeDict
22

33

44
class CamelCase(object):
55

66
def get_default_namedtype_name(self, value):
77
return to_camel_case(value)
8+
9+
def process_aci(self, aci):
10+
aci.args = ProxySnakeDict(aci.args)

0 commit comments

Comments
 (0)