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