mirror of
				https://github.com/graphql-python/graphene.git
				synced 2025-10-31 16:07:27 +03:00 
			
		
		
		
	Allow class methods to be used as a field source
This commit is contained in:
		
							parent
							
								
									02eb6856ed
								
							
						
					
					
						commit
						f728542ce7
					
				|  | @ -13,7 +13,7 @@ base_type = type | |||
| 
 | ||||
| def source_resolver(source, root, args, context, info): | ||||
|     resolved = getattr(root, source, None) | ||||
|     if inspect.isfunction(resolved): | ||||
|     if inspect.isfunction(resolved) or inspect.ismethod(resolved): | ||||
|         return resolved() | ||||
|     return resolved | ||||
| 
 | ||||
|  |  | |||
|  | @ -10,6 +10,9 @@ class MyInstance(object): | |||
|     value = 'value' | ||||
|     value_func = staticmethod(lambda: 'value_func') | ||||
| 
 | ||||
|     def value_method(self): | ||||
|         return 'value_method' | ||||
| 
 | ||||
| 
 | ||||
| def test_field_basic(): | ||||
|     MyType = object() | ||||
|  | @ -76,6 +79,12 @@ def test_field_source_func(): | |||
|     assert field.resolver(MyInstance(), {}, None, None) == MyInstance.value_func() | ||||
| 
 | ||||
| 
 | ||||
| def test_field_source_method(): | ||||
|     MyType = object() | ||||
|     field = Field(MyType, source='value_method') | ||||
|     assert field.resolver(MyInstance(), {}, None, None) == MyInstance().value_method() | ||||
| 
 | ||||
| 
 | ||||
| def test_field_source_as_argument(): | ||||
|     MyType = object() | ||||
|     field = Field(MyType, source=String()) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user