mirror of
				https://github.com/graphql-python/graphene.git
				synced 2025-11-04 09:57:41 +03:00 
			
		
		
		
	Fixed Union resolve_type. Fixed #313
This commit is contained in:
		
							parent
							
								
									2e58f53f18
								
							
						
					
					
						commit
						0efee6be5d
					
				
							
								
								
									
										0
									
								
								graphene/tests/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								graphene/tests/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										0
									
								
								graphene/tests/issues/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								graphene/tests/issues/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										52
									
								
								graphene/tests/issues/test_313.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								graphene/tests/issues/test_313.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,52 @@
 | 
				
			||||||
 | 
					# https://github.com/graphql-python/graphene/issues/313
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import graphene
 | 
				
			||||||
 | 
					from graphene import resolve_only_args
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Success(graphene.ObjectType):
 | 
				
			||||||
 | 
					    yeah = graphene.String()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Error(graphene.ObjectType):
 | 
				
			||||||
 | 
					    message = graphene.String()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class CreatePostResult(graphene.Union):
 | 
				
			||||||
 | 
					    class Meta:
 | 
				
			||||||
 | 
					        types = [Success, Error]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class CreatePost(graphene.Mutation):
 | 
				
			||||||
 | 
					    class Input:
 | 
				
			||||||
 | 
					        text = graphene.String(required=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    result = graphene.Field(CreatePostResult)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @resolve_only_args
 | 
				
			||||||
 | 
					    def mutate(self, text):
 | 
				
			||||||
 | 
					        result = Success(yeah='yeah')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return CreatePost(result=result)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Mutations(graphene.ObjectType):
 | 
				
			||||||
 | 
					    create_post = CreatePost.Field()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# tests.py
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_create_post():
 | 
				
			||||||
 | 
					    query_string = '''
 | 
				
			||||||
 | 
					    mutation {
 | 
				
			||||||
 | 
					      createPost(text: "Try this out") {
 | 
				
			||||||
 | 
					        result {
 | 
				
			||||||
 | 
					          __typename
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    schema = graphene.Schema(mutation=Mutations)
 | 
				
			||||||
 | 
					    result = schema.execute(query_string)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert not result.errors
 | 
				
			||||||
 | 
					    assert result.data['createPost']['result']['__typename'] == 'Success'
 | 
				
			||||||
| 
						 | 
					@ -39,7 +39,11 @@ class Union(six.with_metaclass(UnionMeta)):
 | 
				
			||||||
    to determine which type is actually used when the field is resolved.
 | 
					    to determine which type is actually used when the field is resolved.
 | 
				
			||||||
    '''
 | 
					    '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    resolve_type = None
 | 
					    @classmethod
 | 
				
			||||||
 | 
					    def resolve_type(cls, instance, context, info):
 | 
				
			||||||
 | 
					        from .objecttype import ObjectType
 | 
				
			||||||
 | 
					        if isinstance(instance, ObjectType):
 | 
				
			||||||
 | 
					            return type(instance)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, *args, **kwargs):
 | 
					    def __init__(self, *args, **kwargs):
 | 
				
			||||||
        raise Exception("A Union cannot be intitialized")
 | 
					        raise Exception("A Union cannot be intitialized")
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user