Skip to content

Commit 27cd00b

Browse files
authored
Merge pull request #391 from graphql-python/features/mounted-refactor
Refactored mounted types logic
2 parents 7e5285d + 93dacda commit 27cd00b

File tree

7 files changed

+41
-52
lines changed

7 files changed

+41
-52
lines changed

graphene/types/argument.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from collections import OrderedDict
22
from itertools import chain
33

4-
from ..utils.orderedtype import OrderedType
4+
from .mountedtype import MountedType
55
from .structures import NonNull
66
from .dynamic import Dynamic
77

88

9-
class Argument(OrderedType):
9+
class Argument(MountedType):
1010

1111
def __init__(self, type, default_value=None, description=None, name=None, required=False, _creation_counter=None):
1212
super(Argument, self).__init__(_creation_counter=_creation_counter)
@@ -47,7 +47,7 @@ def to_arguments(args, extra_args=None):
4747
continue
4848

4949
if isinstance(arg, UnmountedType):
50-
arg = arg.Argument()
50+
arg = Argument.mount(arg)
5151

5252
if isinstance(arg, (InputField, Field)):
5353
raise ValueError('Expected {} to be Argument, but received {}. Try using Argument({}).'.format(

graphene/types/dynamic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import inspect
22

3-
from ..utils.orderedtype import OrderedType
3+
from .mountedtype import MountedType
44

55

6-
class Dynamic(OrderedType):
6+
class Dynamic(MountedType):
77
'''
88
A Dynamic Type let us get the type in runtime when we generate
99
the schema. So we can have lazy fields.

graphene/types/field.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from collections import Mapping, OrderedDict
33
from functools import partial
44

5-
from ..utils.orderedtype import OrderedType
65
from .argument import Argument, to_arguments
6+
from .mountedtype import MountedType
77
from .structures import NonNull
88
from .unmountedtype import UnmountedType
99

@@ -18,7 +18,7 @@ def source_resolver(source, root, args, context, info):
1818
return resolved
1919

2020

21-
class Field(OrderedType):
21+
class Field(MountedType):
2222

2323
def __init__(self, type, args=None, resolver=None, source=None,
2424
deprecation_reason=None, name=None, description=None,

graphene/types/inputfield.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from ..utils.orderedtype import OrderedType
1+
from .mountedtype import MountedType
22
from .structures import NonNull
33

44

5-
class InputField(OrderedType):
5+
class InputField(MountedType):
66

77
def __init__(self, type, name=None, default_value=None,
88
deprecation_reason=None, description=None,

graphene/types/mountedtype.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from ..utils.orderedtype import OrderedType
2+
from .unmountedtype import UnmountedType
3+
4+
5+
class MountedType(OrderedType):
6+
7+
@classmethod
8+
def mount(cls, unmounted): # noqa: N802
9+
'''
10+
Mount the UnmountedType instance
11+
'''
12+
assert isinstance(unmounted, UnmountedType), (
13+
'{} can\'t mount {}'
14+
).format(cls.__name__, repr(unmounted))
15+
16+
return cls(
17+
unmounted.get_type(),
18+
*unmounted.args,
19+
_creation_counter=unmounted.creation_counter,
20+
**unmounted.kwargs
21+
)

graphene/types/unmountedtype.py

+6-18
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,29 @@ def get_type(self):
2727
'''
2828
raise NotImplementedError("get_type not implemented in {}".format(self))
2929

30+
def mount_as(self, _as):
31+
return _as.mount(self)
32+
3033
def Field(self): # noqa: N802
3134
'''
3235
Mount the UnmountedType as Field
3336
'''
3437
from .field import Field
35-
return Field(
36-
self.get_type(),
37-
*self.args,
38-
_creation_counter=self.creation_counter,
39-
**self.kwargs
40-
)
38+
return self.mount_as(Field)
4139

4240
def InputField(self): # noqa: N802
4341
'''
4442
Mount the UnmountedType as InputField
4543
'''
4644
from .inputfield import InputField
47-
return InputField(
48-
self.get_type(),
49-
*self.args,
50-
_creation_counter=self.creation_counter,
51-
**self.kwargs
52-
)
45+
return self.mount_as(InputField)
5346

5447
def Argument(self): # noqa: N802
5548
'''
5649
Mount the UnmountedType as Argument
5750
'''
5851
from .argument import Argument
59-
return Argument(
60-
self.get_type(),
61-
*self.args,
62-
_creation_counter=self.creation_counter,
63-
**self.kwargs
64-
)
52+
return self.mount_as(Argument)
6553

6654
def __eq__(self, other):
6755
return (

graphene/types/utils.py

+5-25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from collections import OrderedDict
22

3-
from .dynamic import Dynamic
4-
from .field import Field
5-
from .inputfield import InputField
3+
from .mountedtype import MountedType
64
from .unmountedtype import UnmountedType
75

86

@@ -35,34 +33,16 @@ def get_base_fields(bases, _as=None):
3533
return fields
3634

3735

38-
def mount_as(unmounted_field, _as):
39-
'''
40-
Mount the UnmountedType dinamically as Field or InputField
41-
'''
42-
if _as is None:
43-
return unmounted_field
44-
45-
elif _as is Field:
46-
return unmounted_field.Field()
47-
48-
elif _as is InputField:
49-
return unmounted_field.InputField()
50-
51-
raise Exception(
52-
'Unmounted field "{}" cannot be mounted in {}.'.format(
53-
unmounted_field, _as
54-
)
55-
)
56-
57-
5836
def get_field_as(value, _as=None):
5937
'''
6038
Get type mounted
6139
'''
62-
if isinstance(value, (Field, InputField, Dynamic)):
40+
if isinstance(value, MountedType):
6341
return value
6442
elif isinstance(value, UnmountedType):
65-
return mount_as(value, _as)
43+
if _as is None:
44+
return value
45+
return _as.mount(value)
6646

6747

6848
def yank_fields_from_attrs(attrs, _as=None, delete=True, sort=True):

0 commit comments

Comments
 (0)