mirror of
				https://github.com/graphql-python/graphene.git
				synced 2025-10-31 16:07:27 +03:00 
			
		
		
		
	Added UUID type
This commit is contained in:
		
							parent
							
								
									ed4bcce0cf
								
							
						
					
					
						commit
						eabb9b202c
					
				|  | @ -27,6 +27,8 @@ if not __SETUP__: | ||||||
|         Schema, |         Schema, | ||||||
|         Scalar, |         Scalar, | ||||||
|         String, ID, Int, Float, Boolean, |         String, ID, Int, Float, Boolean, | ||||||
|  |         JSONString, | ||||||
|  |         UUID, | ||||||
|         List, NonNull, |         List, NonNull, | ||||||
|         Enum, |         Enum, | ||||||
|         Argument, |         Argument, | ||||||
|  | @ -63,6 +65,8 @@ if not __SETUP__: | ||||||
|         'Float', |         'Float', | ||||||
|         'Enum', |         'Enum', | ||||||
|         'Boolean', |         'Boolean', | ||||||
|  |         'JSONString', | ||||||
|  |         'UUID', | ||||||
|         'List', |         'List', | ||||||
|         'NonNull', |         'NonNull', | ||||||
|         'Argument', |         'Argument', | ||||||
|  |  | ||||||
|  | @ -5,6 +5,8 @@ from .objecttype import ObjectType | ||||||
| from .interface import Interface | from .interface import Interface | ||||||
| from .mutation import Mutation | from .mutation import Mutation | ||||||
| from .scalars import Scalar, String, ID, Int, Float, Boolean | from .scalars import Scalar, String, ID, Int, Float, Boolean | ||||||
|  | from .json import JSONString | ||||||
|  | from .uuid import UUID | ||||||
| from .schema import Schema | from .schema import Schema | ||||||
| from .structures import List, NonNull | from .structures import List, NonNull | ||||||
| from .enum import Enum | from .enum import Enum | ||||||
|  | @ -34,6 +36,8 @@ __all__ = [ | ||||||
|     'ID', |     'ID', | ||||||
|     'Int', |     'Int', | ||||||
|     'Float', |     'Float', | ||||||
|  |     'JSONString', | ||||||
|  |     'UUID', | ||||||
|     'Boolean', |     'Boolean', | ||||||
|     'List', |     'List', | ||||||
|     'NonNull', |     'NonNull', | ||||||
|  |  | ||||||
							
								
								
									
										34
									
								
								graphene/types/tests/test_uuid.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								graphene/types/tests/test_uuid.py
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,34 @@ | ||||||
|  | from ..uuid import UUID | ||||||
|  | from ..objecttype import ObjectType | ||||||
|  | from ..schema import Schema | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class Query(ObjectType): | ||||||
|  |     uuid = UUID(input=UUID()) | ||||||
|  | 
 | ||||||
|  |     def resolve_uuid(self, input): | ||||||
|  |         return input | ||||||
|  | 
 | ||||||
|  | schema = Schema(query=Query) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_uuidstring_query(): | ||||||
|  |     uuid_value = 'dfeb3bcf-70fd-11e7-a61a-6003088f8204' | ||||||
|  |     result = schema.execute('''{ uuid(input: "%s") }''' % uuid_value) | ||||||
|  |     assert not result.errors | ||||||
|  |     assert result.data == { | ||||||
|  |         'uuid': uuid_value | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_uuidstring_query_variable(): | ||||||
|  |     uuid_value = 'dfeb3bcf-70fd-11e7-a61a-6003088f8204' | ||||||
|  | 
 | ||||||
|  |     result = schema.execute( | ||||||
|  |         '''query Test($uuid: UUID){ uuid(input: $uuid) }''', | ||||||
|  |         variable_values={'uuid': uuid_value} | ||||||
|  |     ) | ||||||
|  |     assert not result.errors | ||||||
|  |     assert result.data == { | ||||||
|  |         'uuid': uuid_value | ||||||
|  |     } | ||||||
							
								
								
									
										27
									
								
								graphene/types/uuid.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								graphene/types/uuid.py
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,27 @@ | ||||||
|  | from __future__ import absolute_import | ||||||
|  | 
 | ||||||
|  | from uuid import UUID as _UUID | ||||||
|  | 
 | ||||||
|  | from graphql.language import ast | ||||||
|  | 
 | ||||||
|  | from .scalars import Scalar | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class UUID(Scalar): | ||||||
|  |     '''UUID''' | ||||||
|  | 
 | ||||||
|  |     @staticmethod | ||||||
|  |     def serialize(uuid): | ||||||
|  |         if isinstance(uuid, str): | ||||||
|  |             uuid = _UUID(uuid) | ||||||
|  |         assert isinstance(uuid, _UUID), "Expected UUID instance, received {}".format(uuid) | ||||||
|  |         return str(uuid) | ||||||
|  | 
 | ||||||
|  |     @staticmethod | ||||||
|  |     def parse_literal(node): | ||||||
|  |         if isinstance(node, ast.StringValue): | ||||||
|  |             return _UUID(node.value) | ||||||
|  | 
 | ||||||
|  |     @staticmethod | ||||||
|  |     def parse_value(value): | ||||||
|  |         return _UUID(value) | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user