|
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 |
3 | 4 |
|
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): |
224 | 5 |
|
| 6 | +class DeprecatedField(object): |
225 | 7 | 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) |
227 | 11 |
|
228 | 12 |
|
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 |
235 | 15 |
|
236 | 16 |
|
237 |
| -class BooleanField(TypeField): |
238 |
| - field_type = GraphQLBoolean |
| 17 | +class IntField(DeprecatedField, Int): |
| 18 | + pass |
239 | 19 |
|
240 | 20 |
|
241 |
| -class IDField(TypeField): |
242 |
| - field_type = GraphQLID |
| 21 | +class BooleanField(DeprecatedField, Boolean): |
| 22 | + pass |
243 | 23 |
|
244 | 24 |
|
245 |
| -class FloatField(TypeField): |
246 |
| - field_type = GraphQLFloat |
| 25 | +class IDField(DeprecatedField, ID): |
| 26 | + pass |
247 | 27 |
|
248 | 28 |
|
249 |
| -class ListField(Field): |
| 29 | +class FloatField(DeprecatedField, Float): |
| 30 | + pass |
250 | 31 |
|
251 |
| - def type_wrapper(self, field_type): |
252 |
| - return GraphQLList(field_type) |
253 | 32 |
|
| 33 | +class ListField(DeprecatedField, List): |
| 34 | + pass |
254 | 35 |
|
255 |
| -class NonNullField(Field): |
256 | 36 |
|
257 |
| - def type_wrapper(self, field_type): |
258 |
| - return GraphQLNonNull(field_type) |
| 37 | +class NonNullField(DeprecatedField, NonNull): |
| 38 | + pass |
0 commit comments