From 6e0789bfcd3f22ac0c27b980e1eec9822978c199 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Sat, 13 Aug 2016 13:16:52 -0700 Subject: [PATCH] Improved implementation --- graphene/new_types/field.py | 17 +++++++++-------- graphene/new_types/options.py | 2 -- graphene/new_types/tests/test_field.py | 6 ++++++ graphene/new_types/typemap.py | 2 +- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/graphene/new_types/field.py b/graphene/new_types/field.py index f5fba407..883bbfdd 100644 --- a/graphene/new_types/field.py +++ b/graphene/new_types/field.py @@ -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 diff --git a/graphene/new_types/options.py b/graphene/new_types/options.py index 38549df6..89c1eb5a 100644 --- a/graphene/new_types/options.py +++ b/graphene/new_types/options.py @@ -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: diff --git a/graphene/new_types/tests/test_field.py b/graphene/new_types/tests/test_field.py index ba537361..31fcf037 100644 --- a/graphene/new_types/tests/test_field.py +++ b/graphene/new_types/tests/test_field.py @@ -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: diff --git a/graphene/new_types/typemap.py b/graphene/new_types/typemap.py index e981dcf4..918f216a 100644 --- a/graphene/new_types/typemap.py +++ b/graphene/new_types/typemap.py @@ -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):