Skip to content

Commit 3c65deb

Browse files
committed
In work version graphene new types
1 parent 9bab0d9 commit 3c65deb

18 files changed

+83
-277
lines changed

graphene/core/fields.py

Lines changed: 21 additions & 241 deletions
Original file line numberDiff line numberDiff line change
@@ -1,258 +1,38 @@
1-
import inspect
2-
from functools import total_ordering, wraps
1+
from .types.field import Field
2+
from .types.scalars import String, Int, Boolean, ID, Float
3+
from .types.definitions import List, NonNull
34

4-
import six
5-
6-
from graphene.core.scalars import GraphQLSkipField
7-
from graphene.core.types import BaseObjectType, InputObjectType
8-
from graphene.utils import ProxySnakeDict, enum_to_graphql_enum, to_camel_case
9-
from graphql.core.type import (GraphQLArgument, GraphQLBoolean, GraphQLField,
10-
GraphQLFloat, GraphQLID,
11-
GraphQLInputObjectField, GraphQLInt,
12-
GraphQLList, GraphQLNonNull, GraphQLString)
13-
14-
try:
15-
from enum import Enum
16-
except ImportError:
17-
class Enum(object):
18-
pass
19-
20-
21-
class Empty(object):
22-
pass
23-
24-
25-
@total_ordering
26-
class Field(object):
27-
SKIP = GraphQLSkipField
28-
creation_counter = 0
29-
required = False
30-
31-
def __init__(self, field_type, name=None, resolve=None, required=False, args=None, description='', default=None, **extra_args):
32-
self.field_type = field_type
33-
self.resolve_fn = resolve
34-
self.required = self.required or required
35-
self.args = args or {}
36-
self.extra_args = extra_args
37-
self._type = None
38-
self.name = name
39-
self.description = description or self.__doc__
40-
self.object_type = None
41-
self.default = default
42-
self.creation_counter = Field.creation_counter
43-
Field.creation_counter += 1
44-
45-
def get_default(self):
46-
return self.default
47-
48-
def contribute_to_class(self, cls, name, add=True):
49-
if not self.name:
50-
self.name = to_camel_case(name)
51-
self.attname = name
52-
self.object_type = cls
53-
if isinstance(self.field_type, Field) and not self.field_type.object_type:
54-
self.field_type.contribute_to_class(cls, name, False)
55-
if add:
56-
cls._meta.add_field(self)
57-
58-
def resolve(self, instance, args, info):
59-
schema = info and getattr(info.schema, 'graphene_schema', None)
60-
resolve_fn = self.get_resolve_fn(schema)
61-
if resolve_fn:
62-
return resolve_fn(instance, ProxySnakeDict(args), info)
63-
else:
64-
return getattr(instance, self.attname, self.get_default())
65-
66-
def get_resolve_fn(self, schema):
67-
object_type = self.get_object_type(schema)
68-
if object_type and object_type._meta.is_mutation:
69-
return object_type.mutate
70-
elif self.resolve_fn:
71-
return self.resolve_fn
72-
else:
73-
custom_resolve_fn_name = 'resolve_%s' % self.attname
74-
if hasattr(self.object_type, custom_resolve_fn_name):
75-
resolve_fn = getattr(self.object_type, custom_resolve_fn_name)
76-
77-
@wraps(resolve_fn)
78-
def custom_resolve_fn(instance, args, info):
79-
return resolve_fn(instance, args, info)
80-
return custom_resolve_fn
81-
82-
def get_object_type(self, schema):
83-
field_type = self.field_type
84-
if inspect.isfunction(field_type):
85-
field_type = field_type(self)
86-
_is_class = inspect.isclass(field_type)
87-
if isinstance(field_type, Field):
88-
return field_type.get_object_type(schema)
89-
if _is_class and issubclass(field_type, BaseObjectType):
90-
return field_type
91-
elif isinstance(field_type, six.string_types):
92-
if field_type == 'self':
93-
return self.object_type
94-
else:
95-
return schema.get_type(field_type)
96-
97-
def type_wrapper(self, field_type):
98-
if self.required:
99-
field_type = GraphQLNonNull(field_type)
100-
return field_type
101-
102-
def internal_type(self, schema):
103-
field_type = self.field_type
104-
_is_class = inspect.isclass(field_type)
105-
if isinstance(field_type, Field):
106-
field_type = self.field_type.internal_type(schema)
107-
elif _is_class and issubclass(field_type, Enum):
108-
field_type = enum_to_graphql_enum(field_type)
109-
else:
110-
object_type = self.get_object_type(schema)
111-
if object_type:
112-
field_type = schema.T(object_type)
113-
114-
field_type = self.type_wrapper(field_type)
115-
return field_type
116-
117-
def internal_field(self, schema):
118-
if not self.object_type:
119-
raise Exception(
120-
'Field could not be constructed in a non graphene.ObjectType or graphene.Interface')
121-
122-
extra_args = self.extra_args.copy()
123-
for arg_name, arg_value in self.extra_args.items():
124-
if isinstance(arg_value, GraphQLArgument):
125-
self.args[arg_name] = arg_value
126-
del extra_args[arg_name]
127-
128-
if extra_args != {}:
129-
raise TypeError("Field %s.%s initiated with invalid args: %s" % (
130-
self.object_type,
131-
self.attname,
132-
','.join(extra_args.keys())
133-
))
134-
135-
args = self.args
136-
137-
object_type = self.get_object_type(schema)
138-
if object_type and object_type._meta.is_mutation:
139-
assert not self.args, 'Arguments provided for mutations are defined in Input class in Mutation'
140-
args = object_type.get_input_type().fields_as_arguments(schema)
141-
142-
internal_type = self.internal_type(schema)
143-
if not internal_type:
144-
raise Exception("Internal type for field %s is None" % self)
145-
146-
description = self.description
147-
resolve_fn = self.get_resolve_fn(schema)
148-
if resolve_fn:
149-
description = resolve_fn.__doc__ or description
150-
151-
@wraps(resolve_fn)
152-
def resolver(*args):
153-
return self.resolve(*args)
154-
else:
155-
resolver = self.resolve
156-
157-
if issubclass(self.object_type, InputObjectType):
158-
return GraphQLInputObjectField(
159-
internal_type,
160-
description=description,
161-
)
162-
163-
return GraphQLField(
164-
internal_type,
165-
description=description,
166-
args=args,
167-
resolver=resolver,
168-
)
169-
170-
def __str__(self):
171-
""" Return "object_type.name". """
172-
return '%s.%s' % (self.object_type.__name__, self.attname)
173-
174-
def __repr__(self):
175-
"""
176-
Displays the module, class and name of the field.
177-
"""
178-
path = '%s.%s' % (self.__class__.__module__, self.__class__.__name__)
179-
name = getattr(self, 'attname', None)
180-
if name is not None:
181-
return '<%s: %s>' % (path, name)
182-
return '<%s>' % path
183-
184-
def __eq__(self, other):
185-
# Needed for @total_ordering
186-
if isinstance(other, Field):
187-
return self.creation_counter == other.creation_counter and \
188-
self.object_type == other.object_type
189-
return NotImplemented
190-
191-
def __lt__(self, other):
192-
# This is needed because bisect does not take a comparison function.
193-
if isinstance(other, Field):
194-
return self.creation_counter < other.creation_counter
195-
return NotImplemented
196-
197-
def __hash__(self):
198-
return hash((self.creation_counter, self.object_type))
199-
200-
def __copy__(self):
201-
# We need to avoid hitting __reduce__, so define this
202-
# slightly weird copy construct.
203-
obj = Empty()
204-
obj.__class__ = self.__class__
205-
obj.__dict__ = self.__dict__.copy()
206-
if self.field_type == 'self':
207-
obj.field_type = self.object_type
208-
return obj
209-
210-
211-
class LazyField(Field):
212-
213-
def inner_field(self, schema):
214-
return self.get_field(schema)
215-
216-
def internal_type(self, schema):
217-
return self.inner_field(schema).internal_type(schema)
218-
219-
def internal_field(self, schema):
220-
return self.inner_field(schema).internal_field(schema)
221-
222-
223-
class TypeField(Field):
2245

6+
class DeprecatedField(object):
2257
def __init__(self, *args, **kwargs):
226-
super(TypeField, self).__init__(self.field_type, *args, **kwargs)
8+
print("Using {} is not longer supported".format(self.__class__.__name__))
9+
kwargs['resolver'] = kwargs.pop('resolve', None)
10+
return super(DeprecatedField, self).__init__(*args, **kwargs)
22711

22812

229-
class StringField(TypeField):
230-
field_type = GraphQLString
231-
232-
233-
class IntField(TypeField):
234-
field_type = GraphQLInt
13+
class StringField(DeprecatedField, String):
14+
pass
23515

23616

237-
class BooleanField(TypeField):
238-
field_type = GraphQLBoolean
17+
class IntField(DeprecatedField, Int):
18+
pass
23919

24020

241-
class IDField(TypeField):
242-
field_type = GraphQLID
21+
class BooleanField(DeprecatedField, Boolean):
22+
pass
24323

24424

245-
class FloatField(TypeField):
246-
field_type = GraphQLFloat
25+
class IDField(DeprecatedField, ID):
26+
pass
24727

24828

249-
class ListField(Field):
29+
class FloatField(DeprecatedField, Float):
30+
pass
25031

251-
def type_wrapper(self, field_type):
252-
return GraphQLList(field_type)
25332

33+
class ListField(DeprecatedField, List):
34+
pass
25435

255-
class NonNullField(Field):
25636

257-
def type_wrapper(self, field_type):
258-
return GraphQLNonNull(field_type)
37+
class NonNullField(DeprecatedField, NonNull):
38+
pass

graphene/core/ntypes/tests/__init__.py

Whitespace-only changes.

graphene/core/schema.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import inspect
12
from collections import OrderedDict
23

34
from graphene import signals
@@ -6,6 +7,7 @@
67
SynchronousExecutionMiddleware
78
from graphql.core.type import GraphQLSchema as _GraphQLSchema
89
from graphql.core.utils.introspection_query import introspection_query
10+
from graphene.core.types.base import BaseType
911

1012

1113
class GraphQLSchema(_GraphQLSchema):
@@ -34,13 +36,15 @@ def __repr__(self):
3436
def T(self, object_type):
3537
if not object_type:
3638
return
37-
if object_type not in self._types:
38-
internal_type = object_type.internal_type(self)
39-
self._types[object_type] = internal_type
40-
name = getattr(internal_type, 'name', None)
41-
if name:
42-
self._types_names[name] = object_type
43-
return self._types[object_type]
39+
# if inspect.isclass(object_type) and issubclass(object_type, BaseType):
40+
if True:
41+
if object_type not in self._types:
42+
internal_type = object_type.internal_type(self)
43+
self._types[object_type] = internal_type
44+
name = getattr(internal_type, 'name', None)
45+
if name:
46+
self._types_names[name] = object_type
47+
return self._types[object_type]
4448

4549
@property
4650
def query(self):

graphene/core/types/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .objecttype import ObjectTypeMeta, BaseObjectType, ObjectType, Interface, Mutation, InputObjectType

graphene/core/ntypes/argument.py renamed to graphene/core/types/argument.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def to_arguments(*args, **kwargs):
3131
elif isinstance(arg, ArgumentType):
3232
argument = arg.as_argument()
3333
else:
34-
raise ValueError('Unknown argument value type %r' % arg)
34+
raise ValueError('Unknown argument %s=%r' % (name, arg))
3535

3636
if name:
3737
argument.name = to_camel_case(name)

graphene/core/ntypes/base.py renamed to graphene/core/types/base.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
from functools import total_ordering
2-
from ..types import BaseObjectType, InputObjectType
2+
3+
4+
class BaseType(object):
5+
@classmethod
6+
def internal_type(cls, schema):
7+
return getattr(cls, 'T', None)
38

49

510
@total_ordering
6-
class OrderedType(object):
11+
class OrderedType(BaseType):
712
creation_counter = 0
813

914
def __init__(self, _creation_counter=None):
@@ -38,10 +43,6 @@ def __init__(self, *args, **kwargs):
3843
self.args = args
3944
self.kwargs = kwargs
4045

41-
@classmethod
42-
def internal_type(cls, schema):
43-
return getattr(cls, 'T', None)
44-
4546

4647
class ArgumentType(MirroredType):
4748
def as_argument(self):
@@ -51,6 +52,7 @@ def as_argument(self):
5152

5253
class FieldType(MirroredType):
5354
def contribute_to_class(self, cls, name):
55+
from ..types import BaseObjectType, InputObjectType
5456
if issubclass(cls, InputObjectType):
5557
inputfield = self.as_inputfield()
5658
return inputfield.contribute_to_class(cls, name)
@@ -60,11 +62,11 @@ def contribute_to_class(self, cls, name):
6062

6163
def as_field(self):
6264
from .field import Field
63-
return Field(self.__class__, _creation_counter=self.creation_counter, *self.args, **self.kwargs)
65+
return Field(self, _creation_counter=self.creation_counter, *self.args, **self.kwargs)
6466

6567
def as_inputfield(self):
6668
from .field import InputField
67-
return InputField(self.__class__, _creation_counter=self.creation_counter, *self.args, **self.kwargs)
69+
return InputField(self, _creation_counter=self.creation_counter, *self.args, **self.kwargs)
6870

6971

7072
class MountedType(FieldType, ArgumentType):

0 commit comments

Comments
 (0)