mirror of
				https://github.com/graphql-python/graphene.git
				synced 2025-10-31 16:07:27 +03:00 
			
		
		
		
	Improved docs for extending Option attrs
This commit is contained in:
		
							parent
							
								
									e86f73d30c
								
							
						
					
					
						commit
						28c987bdd1
					
				|  | @ -1,5 +1,10 @@ | ||||||
| # v1.0 Upgrade Guide | # v1.0 Upgrade Guide | ||||||
| 
 | 
 | ||||||
|  | * `ObjectType`, `Interface`, `InputObjectType`, `Scalar` and `Enum` implementations | ||||||
|  |   have been quite simplified, without the need of define a explicit Metaclass. | ||||||
|  |   The metaclasses threfore are now deleted as are no longer necessary, if your code was depending | ||||||
|  |   on this internal metaclass for creating custom attrs, please see an [example of how to do it now in 2.0](https://github.com/graphql-python/graphene/blob/master/graphene/tests/issues/test_425_graphene2.py). | ||||||
|  | 
 | ||||||
| ## Deprecations | ## Deprecations | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,53 +1,56 @@ | ||||||
| # https://github.com/graphql-python/graphene/issues/425 | # THIS IS THE OLD IMPLEMENTATION, for Graphene 1.0 | ||||||
| import six | # Keep here as reference for upgrade. | ||||||
| 
 | 
 | ||||||
| from graphene.utils.is_base_type import is_base_type | # # https://github.com/graphql-python/graphene/issues/425 | ||||||
|  | # import six | ||||||
| 
 | 
 | ||||||
| from graphene.types.objecttype import ObjectTypeMeta, ObjectType | # from graphene.utils.is_base_type import is_base_type | ||||||
| from graphene.types.options import Options |  | ||||||
| 
 | 
 | ||||||
| class SpecialObjectTypeMeta(ObjectTypeMeta): | # from graphene.types.objecttype import ObjectTypeMeta, ObjectType | ||||||
|  | # from graphene.types.options import Options | ||||||
| 
 | 
 | ||||||
|     @staticmethod | # class SpecialObjectTypeMeta(ObjectTypeMeta): | ||||||
|     def __new__(cls, name, bases, attrs): |  | ||||||
|         # Also ensure initialization is only performed for subclasses of |  | ||||||
|         # DjangoObjectType |  | ||||||
|         if not is_base_type(bases, SpecialObjectTypeMeta): |  | ||||||
|             return type.__new__(cls, name, bases, attrs) |  | ||||||
| 
 | 
 | ||||||
|         options = Options( | #     @staticmethod | ||||||
|             attrs.pop('Meta', None), | #     def __new__(cls, name, bases, attrs): | ||||||
|             other_attr='default', | #         # Also ensure initialization is only performed for subclasses of | ||||||
|         ) | #         # DjangoObjectType | ||||||
|  | #         if not is_base_type(bases, SpecialObjectTypeMeta): | ||||||
|  | #             return type.__new__(cls, name, bases, attrs) | ||||||
| 
 | 
 | ||||||
|         cls = ObjectTypeMeta.__new__(cls, name, bases, dict(attrs, _meta=options)) | #         options = Options( | ||||||
|         assert cls._meta is options | #             attrs.pop('Meta', None), | ||||||
|         return cls | #             other_attr='default', | ||||||
|  | #         ) | ||||||
|  | 
 | ||||||
|  | #         cls = ObjectTypeMeta.__new__(cls, name, bases, dict(attrs, _meta=options)) | ||||||
|  | #         assert cls._meta is options | ||||||
|  | #         return cls | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class SpecialObjectType(six.with_metaclass(SpecialObjectTypeMeta, ObjectType)): | # class SpecialObjectType(six.with_metaclass(SpecialObjectTypeMeta, ObjectType)): | ||||||
|     pass | #     pass | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def test_special_objecttype_could_be_subclassed(): | # def test_special_objecttype_could_be_subclassed(): | ||||||
|     class MyType(SpecialObjectType): | #     class MyType(SpecialObjectType): | ||||||
|         class Meta: | #         class Meta: | ||||||
|             other_attr = 'yeah!' | #             other_attr = 'yeah!' | ||||||
| 
 | 
 | ||||||
|     assert MyType._meta.other_attr == 'yeah!' | #     assert MyType._meta.other_attr == 'yeah!' | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def test_special_objecttype_could_be_subclassed_default(): | # def test_special_objecttype_could_be_subclassed_default(): | ||||||
|     class MyType(SpecialObjectType): | #     class MyType(SpecialObjectType): | ||||||
|         pass | #         pass | ||||||
| 
 | 
 | ||||||
|     assert MyType._meta.other_attr == 'default' | #     assert MyType._meta.other_attr == 'default' | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def test_special_objecttype_inherit_meta_options(): | # def test_special_objecttype_inherit_meta_options(): | ||||||
|     class MyType(SpecialObjectType): | #     class MyType(SpecialObjectType): | ||||||
|         pass | #         pass | ||||||
| 
 | 
 | ||||||
|     assert MyType._meta.name == 'MyType' | #     assert MyType._meta.name == 'MyType' | ||||||
|     assert MyType._meta.default_resolver == None | #     assert MyType._meta.default_resolver == None | ||||||
|     assert MyType._meta.interfaces == () | #     assert MyType._meta.interfaces == () | ||||||
|  |  | ||||||
							
								
								
									
										40
									
								
								graphene/tests/issues/test_425_graphene2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								graphene/tests/issues/test_425_graphene2.py
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,40 @@ | ||||||
|  | # https://github.com/graphql-python/graphene/issues/425 | ||||||
|  | # Adapted for Graphene 2.0 | ||||||
|  | import six | ||||||
|  | 
 | ||||||
|  | from graphene.types.objecttype import ObjectType, ObjectTypeOptions | ||||||
|  | 
 | ||||||
|  | class SpecialOptions(ObjectTypeOptions): | ||||||
|  |     other_attr = None | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class SpecialObjectType(ObjectType): | ||||||
|  |     @classmethod | ||||||
|  |     def __init_subclass_with_meta__(cls, other_attr='default', **options): | ||||||
|  |         _meta = SpecialOptions(cls) | ||||||
|  |         _meta.other_attr = other_attr | ||||||
|  |         super(SpecialObjectType, cls).__init_subclass_with_meta__(_meta=_meta, **options) | ||||||
|  |          | ||||||
|  | 
 | ||||||
|  | def test_special_objecttype_could_be_subclassed(): | ||||||
|  |     class MyType(SpecialObjectType): | ||||||
|  |         class Meta: | ||||||
|  |             other_attr = 'yeah!' | ||||||
|  | 
 | ||||||
|  |     assert MyType._meta.other_attr == 'yeah!' | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_special_objecttype_could_be_subclassed_default(): | ||||||
|  |     class MyType(SpecialObjectType): | ||||||
|  |         pass | ||||||
|  | 
 | ||||||
|  |     assert MyType._meta.other_attr == 'default' | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_special_objecttype_inherit_meta_options(): | ||||||
|  |     class MyType(SpecialObjectType): | ||||||
|  |         pass | ||||||
|  | 
 | ||||||
|  |     assert MyType._meta.name == 'MyType' | ||||||
|  |     assert MyType._meta.default_resolver == None | ||||||
|  |     assert MyType._meta.interfaces == () | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user