mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-26 19:43:56 +03:00
Improved implementation
This commit is contained in:
parent
b68b1753bb
commit
6e0789bfcd
|
@ -20,18 +20,19 @@ class Field(OrderedType):
|
|||
deprecation_reason=None, name=None, description=None,
|
||||
required=False, _creation_counter=None, **extra_args):
|
||||
super(Field, self).__init__(_creation_counter=_creation_counter)
|
||||
self.name = name
|
||||
# self.attname = None
|
||||
# self.parent = None
|
||||
assert not args or isinstance(args, Mapping), (
|
||||
'Arguments in a field have to be a mapping, received "{}".'
|
||||
).format(args)
|
||||
assert not (source and resolver), (
|
||||
'A Field cannot have a source and a resolver in at the same time.'
|
||||
)
|
||||
|
||||
if required:
|
||||
type = NonNull(type)
|
||||
|
||||
self.name = name
|
||||
self._type = type
|
||||
if args:
|
||||
assert isinstance(args, Mapping), 'Arguments in a field have to be a mapping, received "{}".'.format(args)
|
||||
self.args = to_arguments(args or OrderedDict(), extra_args)
|
||||
# self.args = to_arguments(args, extra_args)
|
||||
assert not (source and resolver), ('A Field cannot have a source and a '
|
||||
'resolver in at the same time.')
|
||||
if source:
|
||||
resolver = partial(source_resolver, source)
|
||||
self.resolver = resolver
|
||||
|
|
|
@ -13,9 +13,7 @@ class Options(object):
|
|||
assert inspect.isclass(meta), (
|
||||
'Meta have to be a class, received "{}".'.format(repr(meta))
|
||||
)
|
||||
self.add_attrs_from_meta(meta, defaults)
|
||||
|
||||
def add_attrs_from_meta(self, meta, defaults):
|
||||
meta_attrs = props(meta) if meta else {}
|
||||
for attr_name, value in defaults.items():
|
||||
if attr_name in meta_attrs:
|
||||
|
|
|
@ -44,6 +44,12 @@ def test_field_source():
|
|||
assert field.resolver(MyInstance, {}, None, None) == MyInstance.value
|
||||
|
||||
|
||||
def test_field_with_lazy_type():
|
||||
MyType = object()
|
||||
field = Field(lambda: MyType)
|
||||
assert field.type == MyType
|
||||
|
||||
|
||||
def test_field_not_source_and_resolver():
|
||||
MyType = object()
|
||||
with pytest.raises(Exception) as exc_info:
|
||||
|
|
|
@ -28,6 +28,7 @@ class TypeMap(GraphQLTypeMap):
|
|||
def reducer(cls, map, type):
|
||||
if not type:
|
||||
return map
|
||||
|
||||
if is_graphene_type(type):
|
||||
return cls.graphene_reducer(map, type)
|
||||
return super(TypeMap, cls).reducer(map, type)
|
||||
|
@ -36,7 +37,6 @@ class TypeMap(GraphQLTypeMap):
|
|||
def graphene_reducer(cls, map, type):
|
||||
if isinstance(type, (List, NonNull)):
|
||||
return cls.reducer(map, type.of_type)
|
||||
return map
|
||||
if type._meta.name in map:
|
||||
_type = map[type._meta.name]
|
||||
if is_graphene_type(_type):
|
||||
|
|
Loading…
Reference in New Issue
Block a user